【发布时间】:2014-04-12 23:42:32
【问题描述】:
假设我有一个包含以下文档的集合:
{ "_id": 1, name: "Apple" }
{ "_id": 2, name: "Banana", "is_reported": null }
{ "_id": 3, name: "Cherry", "is_reported": false }
{ "_id": 4, name: "Kiwi", "is_reported": true }
是否有更简单的查询来选择“is_reported”处于虚假状态的所有文档;也就是说,要么不存在,要么为空,要么为假?也就是说,查询选择 Apple、Banana 和 Cherry,但不选择 Kiwi?
According to the MongoDB FAQ、{ "is_reported": null } 将选择“is_reported”为空或不存在的文档,但仍不会选择“is_reported”为假的文档。
现在我有以下查询,它工作正常,但它看起来不是很优雅。如果我需要选择多个字段,它会很快变得混乱。是否有更好的查询可以达到相同的最终结果?
db.fruits.find({ $or: [ { "is_reported": null }, { "is_reported": false } ] })
【问题讨论】:
标签: mongodb mongodb-query database