在mongodb中使用$ not [英] Using $not in mongodb
本文介绍了在mongodb中使用$ not的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做这样的事情:
I'm trying to do something like this :
select * from table where not (a=3 and b=4 and c=3 or x=4)
我希望它能起作用:
db.table.find({
$not: {
$or: [
{
$and: [
{ a: 3 },
{ b: 4 },
{ c: 3 }
]
},
{ x: 4 }
]
}
})
但这给我一个错误:
error: { "$err" : "invalid operator: $and", "code" : 10068 }
还有另一种在mongodb中表达它的方法吗?
Is there another way to express it in mongodb?
推荐答案
使用{ $not : { $and : []} }
将不起作用($not
与其他运算符不同,只能用于取消对其他运算符的检查).
Using { $not : { $and : []} }
will not work ($not
is not like other operators, can only be applied to negate the check of other operators).
$ and在这里不是问题,这也不起作用(尽管没有报告任何错误):
$and is not the problem here, this also doesn't work (though without reporting any errors):
{ $not : { a : {$gt : 14} }
您必须将其重写为
{ a : { $not : {$gt : 14} }
返回您的查询:
`not (a=3 and b=4 and c=3 or x=4)`
等效于:
a!=3 and b!=4 and c!=3 or x!=4
您可以在mongo中完成
and that you can do in mongo:
{a : { $ne : 3}, b : { $ne : 4} ...}
这篇关于在mongodb中使用$ not的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文