在风帆和水线中使用AND和OR子句的混合

问题描述 投票:3回答:3

如何在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编写此查询

sails.js waterline
3个回答
5
投票

以下查询也应该有效:

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

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
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.