MongoDB – Find Documents Missing Fields

Examples

icon

In MongoDB it is possible that some documents will not contain fields other documents have. The following code shows how to find these documents using the mongo shell.

db.collectionName.find("function() { return this.fieldName == null }")
db.collectionName.find("function() { return this.fieldName == null }").count()

The second example shows how to only get the count.

In this example. you would replace “collectionName” with the name of the collection you are interested in querying. Additionally, you would change “fieldName” to the name of the field that interests you.

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>