Mongoid:如何查询value为nil的所有对象?

我很难做一些事情,比如:

Something.where(:field => nil) 

要么

 Something.where(:field => { '$eq' => nil }) 

什么是在Mongoid中处理这个问题的正确方法?

这是正确的方法。 例如,要找到发动机nil汽车,请使用:

 # Cars that have a _nil_ engine. Car.where(:engine => nil) 

如果您正在尝试查找缺少字段(而不是设置为nil的字段),请使用$exists谓词:

 # Cars that lack an engine entirely. Car.where(:engine.exists => false) 

请注意,将字段foo设置为nil并且缺少名为foo的字段是两个不同的事情。