Mongoid:如何查询所有值为nil的对象?
我很难做一些事情,例如:
Something.where(:field => nil)
或者
Something.where(:field => { '$eq' => nil })
在 Mongoid 中处理这个问题的正确方法是什么?
I'm having a difficult time doing something such as:
Something.where(:field => nil)
or
Something.where(:field => { '$eq' => nil })
What's the right way to handle this in Mongoid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的做法。例如,要查找引擎为
nil
的汽车,请使用:如果您尝试查找不存在的字段(而不是设置为
的字段) nil
),使用$exists
谓词:请注意,将字段
foo
设置为nil
并且缺少名为的字段>foo
是两个不同的东西。That's the right way to do it. To find cars whose engine is
nil
, for example, use:If you're trying to look for the absence of a field (rather than one that's set to
nil
), use the$exists
predicate:Note that setting a field
foo
to benil
and lacking a field namedfoo
are two different things.