How to search for documents in a collection that are missing a certain field in MongoDB?
相关问题
- MongoDB can not create unique sparse index (duplic
- What means in Dart static type and why it differs
- Spring Data MongoDB - lazy access to some fields
- Golang mongodb aggregation
- How to convert from Timestamp to Mongo ObjectID
相关文章
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- mongodb error: how do I make sure that your journa
- How to track MongoDB requests from a console appli
- Notice: Undefined property - how do I avoid that m
- MongoError: cannot infer query fields to set, path
- Pymongo $in Query Not Working
- django.core.exceptions.ImproperlyConfigured: '
If you don't care if the field is missing or
null
(or if it's nevernull
) then you can use the slightly shorter and safer:It's safer because
$exists
will returntrue
even if the field is null, which often is not the desired result and can lead to an NPE.Yeah, it's possible using $exists:
When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.