MongoDB $exists

In MongoDB, you can use the $exists element query operator to match documents that contain a specific field.

You can also use it to match documents that don’t contain a specific field.

You can also use it in conjunction with other operators such $nin to match documents where a given field exists, but it doesn’t contain a specific value.

Example

Suppose we have a collection called cats that contains the following documents:

{ "_id" : 1, "name" : "Scratch", "born" : "March, 2020" }
{ "_id" : 2, "name" : "Meow", "weight" : 30 }
{ "_id" : 3, "name" : "Fluffy", "height" : 15 }
{ "_id" : 4, "name" : "Sox", "weight" : 40 }
{ "_id" : 5, "name" : null, "weight" : 20 }
{ "_id" : 6, "height" : 20, "born" : ISODate("2021-01-03T23:30:15.123Z") }

These documents are slightly inconsistent with regards to the fields that they have. Some have a weight field, others have a height field, some have a born field, etc

We can use the $exists operator to return documents from that collection that have a specific field.

Example:

db.cats.find( { weight: { $exists: true } } )

Result:

{ "_id" : 2, "name" : "Meow", "weight" : 30 }
{ "_id" : 4, "name" : "Sox", "weight" : 40 }
{ "_id" : 5, "name" : null, "weight" : 20 }

We can see that only those documents that contain a weight field are returned.

Fields that Contain null

The $exists operator includes fields that contain null. It doesn’t discriminate between null and non-null values.

Example:

db.cats.find( { name: { $exists: true } } )

Result:

{ "_id" : 1, "name" : "Scratch", "born" : "March, 2020" }
{ "_id" : 2, "name" : "Meow", "weight" : 30 }
{ "_id" : 3, "name" : "Fluffy", "height" : 15 }
{ "_id" : 4, "name" : "Sox", "weight" : 40 }
{ "_id" : 5, "name" : null, "weight" : 20 }

We can see that document 5 was returned, even though its name field is null.

Exists Without a Specific Value

You can combine $exists with other operators to return documents that contain the field, but that field doesn’t contain a specific value.

Example:

db.cats.find( { weight: { $exists: true, $nin: [20,30] } } )

Result:

{ "_id" : 4, "name" : "Sox", "weight" : 40 }

This is a different result to the one we would have seen if we’d simply used $nin without the $exists operator.

Here’s what that would have looked like:

db.cats.find( { weight: { $nin: [20,30] } } )

Result:

{ "_id" : 1, "name" : "Scratch", "born" : "March, 2020" }
{ "_id" : 3, "name" : "Fluffy", "height" : 15 }
{ "_id" : 4, "name" : "Sox", "weight" : 40 }
{ "_id" : 6, "height" : 20, "born" : ISODate("2021-01-03T23:30:15.123Z") }

Documents that Don’t Contain a Specific Field

You can use $exists: false to return documents that don’t contain the specified field.

Example:

db.cats.find( { name: { $exists: false } } )

Result:

{ "_id" : 6, "height" : 20, "born" : ISODate("2021-01-03T23:30:15.123Z") }

In this case, one document in the collection doesn’t contain the name field.

Check for Multiple Fields

You can check for the existence of multiple fields by separating them with a comma.

Example:

db.cats.find( { 
  name: { $exists: true }, 
  height: { $exists: true } 
} )

Result:

{ "_id" : 3, "name" : "Fluffy", "height" : 15 }

This example returns all documents that contain both a name field and a height field.