Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Query for Null or Missing Fields

On this page

  • Equality Filter
  • Non-Equality Filter
  • Type Check
  • Existence Check
  • Query for Null or Missing Fields with MongoDB Atlas

You can query for null or missing fields in MongoDB using the following methods:

  • Your programming language's driver.

  • The MongoDB Atlas UI. To learn more, see Query for Null or Missing Fields with MongoDB Atlas.

  • MongoDB Compass.


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


Different query operators in MongoDB treat null values differently.

The query returns both documents in the collection.

To query for fields that exist and are not null, use the { $ne : null } filter. The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.

The query returns only the document where the item field has a value of null.

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

The query only returns the document that does not contain the item field.

Tip

See also:

Reference documentation for the $type and $exists operators.

[1] Starting in MongoDB 4.2, users can no longer use the query filter $type: 0 as a synonym for $exists:false. To query for null or missing fields, see Query for Null or Missing Fields.

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. In the Atlas UI, click Database in the sidebar.

  2. For the database deployment that contains the sample data, click Browse Collections.

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

  4. Select the companies collection.

2

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

3

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.

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

Use the following query filter to match documents that either 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 } }
←  Project Fields to Return from QueryPerform Long-Running Snapshot Queries →