在Sails和Waterline中使用AND和OR语句混合

3
我该如何在Sailsjs和其ORM Waterline中使用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   |
 ---------------------------------------

我希望获取作者为Author-1的公共或免费图书,其中包括Book-A和Book-B。如何使用Sails.js编写此查询?

你的意思是“其中publicfreetrue吗?”。 - sgress454
是的,书籍A、B和D都符合条件。然而,另一个条件是作者必须是作者1,这个条件对于书籍D来说不符合。因此,结果应该是书籍A和B。在我的问题中,我打错了,我已经进行了编辑。 - Muhammad Raihan Muhaimin
3个回答

5
以下查询也应该可以工作:
const books = yield Book.find().where({
  author: 'Author-1',
  or: [{
    free: true,
  }, {
    public: true,
  }],
});

如果您还想对“作者1”和“作者2”进行或运算,该怎么办? - Ashish
1
数组也可以作为“或”语句,因此您可以在where语句中使用author: ['Author-1', 'Author-2'] - Jim Geurts

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
    }

嗨,只是提供信息, 在蓝图上多个where对我不起作用?where={...}&where={...}。但是Jim的解决方案有效。 - Gregory Kapustin

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接