【问题标题】:Using mix of AND and OR clause in sails and waterline在风帆和水线中混合使用 AND 和 OR 子句
【发布时间】:2014-04-16 21:13:26
【问题描述】:

如何在 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”吗?
  • 是的,Book A、Book B 和 Book D 都符合此条件。然而另一个条件是作者必须是作者 1,这对于书 D 失败。所以结果应该是书 A 和书 B,在我的问题中我输入错误我编辑了这个。

标签: sails.js waterline


【解决方案1】:

以下查询也应该有效:

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

【讨论】:

  • 如果您想要在Author-1Author-2 上执行or 怎么办?
  • 数组也可以作为“或”语句,所以你可以这样做:author: ['Author-1', 'Author-2'] 在 where 语句中
【解决方案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={...} 。但吉姆的解决方案奏效了。
【解决方案3】:
  let booksResult=await Book.find().
                        where({author: "Author-1", 
                           or: [{ free: true  }, { public: true  }]
                        });

【讨论】:

    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多