Mongoid:如何查询所有值为nil的对象? [英] Mongoid: how to query for all objects where value is nil?

查看:165
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆