问题描述
在我的应用中,我查询:
In my app I have the query:
Model.where(flagged: true, saved: false)
或者我可以使用Model.all(:conditions => {flagged: true, saved: false})
没关系.
现在如何获得该查询的否定?即所有不满足flagged: true
和saved: false
(相当于flagged != true
或saved != false
)的Model实例
Now how do I get the negation of that query? i.e. all the instances of Model which do not satisfy flagged: true
AND saved: false
(which is the equivalent of flagged != true
OR saved != false
)
Model.excludes(flagged: true, saved: false)
向我返回满足flagged != true
和saved != false
)的实例
Model.excludes(flagged: true, saved: false)
returns me the instances which satisfy flagged != true
AND saved != false
)
推荐答案
我发现我可以使用 JavaScript表达式语法
所以Model.where(flagged: true, saved: false)
的取反-可以表示为Model.where("this.flagged == true && this.saved == false")
So the negation of Model.where(flagged: true, saved: false)
- which can be expressed as Model.where("this.flagged == true && this.saved == false")
是
Model.where("!(this.flagged == true && this.saved == false)")
正如罗素(Russell)的评论所暗示的那样,这比使用本机查询语言效率低,但是出于我的目的,这不是问题,因此我现在将其标记为正确-但对于未来的读者,请记住性能的影响.
As Russell's comment suggests this is less efficient than using the native query language, but for my purposes that is not a problem so I am marking this correct for now - but for future readers bear in mind the performance implications.
如果某人可以给出仅使用本机查询语言的答案,而无需进行显式的布尔逻辑扩展,则我将其标记为正确(也许在将来的Mongodb/Mongoid版本中可以这样做-如果可以的话)现在就完成.)
If someone can give an answer that uses only the native query language, without doing an explicit boolean logic expansion, I'll mark that correct (maybe that will be possible in a future version of Mongodb/Mongoid - if it can't be done now.)
这篇关于如何获得蒙古式查询的否定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!