我在做以下事情时遇到了困难:
Something.where(:field => nil)
或
Something.where(:field => { '$eq' => nil })
在 Mongoid 中处理这个问题的正确方法是什么?
最佳答案
这是正确的做法。例如,要查找引擎为 nil
的汽车,请使用:
# Cars that have a _nil_ engine.
Car.where(:engine => nil)
如果您要查找字段的 absence(而不是设置为 nil
的字段),请使用 $exists
谓词:
# Cars that lack an engine entirely.
Car.where(:engine.exists => false)
请注意,将字段 foo
设置为 nil
和缺少名为 foo
的字段是两件事。
关于ruby-on-rails - Mongoid:如何查询值为 nil 的所有对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8963054/