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

发布于 2024-12-28 11:40:55 字数 202 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2025-01-04 11:40:55

这是正确的做法。例如,要查找引擎为 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 是两个不同的东西。

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)

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)

Note that setting a field foo to be nil and lacking a field named foo are two different things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文