如何在Sailsjs及其ORM水线中使用OR和AND子句?例如,我有一张书桌
_______________________________________
| 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 |
---------------------------------------
我希望获得作者-1的书籍,这些书籍是公开的或免费的,即Book-A和Book-B。如何使用Sails.js
编写此查询答案 0 :(得分:5)
以下查询也应该有效:
const books = yield Book.find().where({
author: 'Author-1',
or: [{
free: true,
}, {
public: true,
}],
});
答案 1 :(得分:2)
为此,我们可以使用Waterline的where api,以下是一个例子
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
}
答案 2 :(得分:0)
let booksResult=await Book.find().
where({author: "Author-1",
or: [{ free: true }, { public: true }]
});