Mongoid:如何查询所有值为nil的对象? [英] Mongoid: how to query for all objects where value is nil?
本文介绍了Mongoid:如何查询所有值为nil的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做诸如以下的事情时遇到了困难:
I'm having a difficult time doing something such as:
Something.where(:field => nil)
或
Something.where(:field => { '$eq' => nil })
在Mongoid中处理此问题的正确方法是什么?
What's the right way to handle this in Mongoid?
推荐答案
这是正确的方法.例如,要查找引擎为nil
的汽车,请使用:
That's the right way to do it. To find cars whose engine is nil
, for example, use:
# Cars that have a _nil_ engine.
Car.where(:engine => nil)
如果您要查找字段的缺少(而不是设置为nil
的字段),请使用$exists
谓词:
If you're trying to look for the absence of a field (rather than one that's set to nil
), use the $exists
predicate:
# Cars that lack an engine entirely.
Car.where(:engine.exists => false)
请注意,将字段foo
设置为nil
并缺少名为foo
的字段是两件事.
Note that setting a field foo
to be nil
and lacking a field named foo
are two different things.
这篇关于Mongoid:如何查询所有值为nil的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文