javascript Waterline ORM (sails.js) 查询中的“where or”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20567746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Waterline ORM (sails.js) "where or" in query
提问by Igor
I would like to know how to add "OR" condition in waterline query. Should look like:
我想知道如何在水线查询中添加“OR”条件。应该看起来像:
User.find().where({
score: { '>': req.params.score},
status: 'user'
OR
status: 'admin'
}).exec(function(err, data){
...
});
So we have 2 conditions:
所以我们有两个条件:
1) Score > specific number
1)分数>具体数字
And 2) status = user
和 2) 状态 = 用户
OR
或者
1) Status = admin.
1) 状态 = 管理员。
回答by JohnGalt
There was an issuewith the development database used by sails (waterline-criteria). The issue was the way strings and integers were handled in sails-disk. In the query criteria below, theScore, was being treated as a string. This has been resolved, so you just need to update sails-disk. You can do this by using npm install sails-disk --force --save
. After that the example below should work fine.
风帆使用的开发数据库存在问题(水线标准)。问题是在sails-disk 中处理字符串和整数的方式。在下面的查询条件中,theScore 被视为字符串。这已经解决了,所以你只需要更新sails-disk。您可以使用npm install sails-disk --force --save
. 之后,下面的示例应该可以正常工作。
You can try this (Updated):
你可以试试这个(更新):
foo: function(req, res, next) {
var theScore = req.param('id') || 0;
User.find().where({
or: [{
score: {
'>': parseInt(theScore),
},
status: 'user'
},
{ status: 'admin'}]
}).exec(function(err, data) {
if (err) return next(err);
res.json(data);
});
},
回答by Garach Ankur
let userResult=await User.find().where({
score: { '>': parseInt(req.params.score)},
or: [{ status: 'user'},{status: 'admin'}]})