Using mix of AND and OR clause in sails and waterline

一笑奈何 提交于 2019-12-01 07:59:39

问题


How can I use OR and AND clause in Sailsjs and its ORM Waterline? For example I have a table of books

 _______________________________________
| book_name | author   | free  | public |  
 _______________________________________
| Book-A    | Author-1 | false | true   |
 ---------------------------------------
| Book-B    | Author-1 | true  | true   |
 ---------------------------------------
| Book-C    | Author-1 | false | false  |
 ---------------------------------------
| Book-D    | Author-2 | true  | true   |
 ---------------------------------------

I want to get Books by Author-1 which are either public or free, which is Book-A and Book-B. How do I write this query with Sails.js


回答1:


The following query should also work:

const books = yield Book.find().where({
  author: 'Author-1',
  or: [{
    free: true,
  }, {
    public: true,
  }],
});



回答2:


For doing so we can use the where api of Waterline, following is an example

Book.find().where(  { or : [ { free  : true }, {   public: true  } ] })
            .where( { author : "Author-1" }  )
            .exec( function (err, books) {
            //Books will be an array containing all the books that matches this criteria
            //Some code here
    }



回答3:


  let booksResult=await Book.find().
                        where({author: "Author-1", 
                           or: [{ free: true  }, { public: true  }]
                        });



来源:https://stackoverflow.com/questions/23120258/using-mix-of-and-and-or-clause-in-sails-and-waterline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!