For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Query for Null or Missing Fields

Query for null or missing fields in MongoDB using the following methods:


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.


Query operators in MongoDB treat null values differently.

This page provides examples of operations that query for null values using the db.collection.find() method in mongosh.

Important

Use null with the MongoDB Shell to query for null or missing fields in MongoDB.

The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

The { metacritic : null } query matches documents that contain the metacritic field with a null value or do not contain the metacritic field.

db.movies.find( { metacritic: null } )

The query returns all documents in the movies collection where the metacritic field contains a null value or does not exist.

To query for fields that exist and are not null, use the { $ne : null } filter.

The { metacritic : { $ne : null } } query matches documents where the metacritic field exists and has a non-null value.

db.movies.find( { metacritic: { $ne: null } } )

The { metacritic : { $type: 10 } } query matches only documents that contain the metacritic field with a null value. The value of the metacritic field is of BSON Type Null (BSON Type 10):

db.movies.find( { metacritic: { $type: 10 } } )

The query returns only the documents from the movies collection where the metacritic field has a null value.

The following example queries for documents that do not contain a field.

The { metacritic : { $exists: false } } query matches documents that do not contain the metacritic field:

db.movies.find( { metacritic: { $exists: false } } )

The query returns only the documents from the movies collection that do not contain the metacritic field.

Note

Expressions do not support the $exists operator. To check for the existence of a field in an expression, you can use the $type aggregation operator to check if a field has a type of missing.

For more information, see $type Existence Check.

Tip

Reference documentation for the $type and $exists operators.

The example in this section uses the sample training dataset. To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.

To query for a null or missing field in MongoDB Atlas, follow these steps:

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Clusters under the Database heading.

    The Clusters page displays.

2
  1. For the cluster that contains the sample data, click Browse Collections.

  2. In the left navigation pane, select the sample_training database.

  3. Select the companies collection.

3

Click the Insert Document button to display the dialog box, and then click Insert to insert a document with only the _id field.

4

To find a document that contains a null or missing value, specify a query filter document in the Filter field. A query filter document uses query operators to specify search conditions.

Query operators in MongoDB treat null values differently. To apply a query filter, copy each of the following documents into the Filter search bar and click Apply.

Use the following query filter to match documents that contain a description field with a null value or do not contain the description field:

{ description : null }

Use the following query filter to match only documents that contain a description field with a null value. This filter specifies that the value of the field must be BSON Type Null (BSON Type 10):

{ description : { $type: 10 } }

Use the following query filter to match only documents that do not contain the description field. Only the document that you inserted earlier should appear:

{ description : { $exists: false } }
Select your language
Rate this page