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.
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.
Equality Filter
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.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { metacritic : { $ne : null } } query matches documents where the metacritic field exists and has a non-null value.
db.movies.find( { metacritic: { $ne: null } } )
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
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.
Existence Check
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.
This page provides examples of operations that query for null values using the pymongo.collection.Collection.find method in the PyMongo Python driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use None with the PyMongo Python driver to query for null or missing fields in MongoDB.
db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
Equality Filter
The { item : None } query matches documents that contain the item field with a null value or do not contain the item field.
cursor = db.inventory.find({"item": None})
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
cursor = db.inventory.find( { "item": { "$ne": None } } )
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The { item : { $type: 10 } } query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
cursor = db.inventory.find({"item": {"$type": 10}})
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The { item : { $exists: False } } query matches documents that do not contain the item field:
cursor = db.inventory.find({"item": {"$exists": False}})
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the motor.motor_asyncio.AsyncIOMotorCollection.find method in the Motor driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use None with the Motor driver to query for null or missing fields in MongoDB.
await db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
Equality Filter
The { item : None } query matches documents that contain the item field with a null value or do not contain the item field.
cursor = db.inventory.find({"item": None})
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
cursor = db.inventory.find( { "item": { "$ne": None } } )
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The { item : { $type: 10 } } query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
cursor = db.inventory.find({"item": {"$type": 10}})
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The { item : { $exists: False } } query matches documents that do not contain the item field:
cursor = db.inventory.find({"item": {"$exists": False}})
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use null with the Java driver to query for null or missing fields in MongoDB.
collection.insertMany(asList( Document.parse("{'_id': 1, 'item': null}"), Document.parse("{'_id': 2}") ));
Equality Filter
The eq("item", null) query matches documents that contain the item field with a null value or do not contain the item field.
FindIterable<Document> findIterable = collection.find(eq("item", null));
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
collection.find(ne("item", null));
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The type("item", BsonType.NULL) query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
findIterable = collection.find(type("item", BsonType.NULL));
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The exists("item", false) query matches documents that do not contain the item field:
findIterable = collection.find(exists("item", false));
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use null with the Java Reactive Streams driver to query for null or missing fields in MongoDB.
Publisher<Success> insertManyPublisher = collection.insertMany(asList( Document.parse("{'_id': 1, 'item': null}"), Document.parse("{'_id': 2}") ));
Equality Filter
The eq("item", null) query matches documents that contain the item field with a null value or do not contain the item field.
FindPublisher<Document> findPublisher = collection.find(eq("item", null));
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
db.inventory.find( { item: { $ne : null} } )
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The type("item", BsonType.NULL) query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
findPublisher = collection.find(type("item", BsonType.NULL));
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The exists("item", false) query matches documents that do not contain the item field:
findPublisher = collection.find(exists("item", false));
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values by using the MongoCollection.find() method in the MongoDB Kotlin Coroutine Driver.
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use null with the Kotlin Coroutine driver to query for null or missing fields in MongoDB.
collection.insertMany( listOf( Document("_id", 1) .append("item", null), Document("_id", 2) ) )
Equality Filter
The eq("item", null) query matches documents that contain the item field with a null value or do not contain the item field.
val findFlow = collection .find(eq("item", null))
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
collection.find(ne("item", null))
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The type("item", BsonType.NULL) query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
val findFlow = collection .find(type("item", BsonType.NULL))
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The exists("item", false) query matches documents that do not contain the item field:
val findFlow = collection .find(exists("item", false))
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the Collection.find() method in the MongoDB Node.js Driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use null with the Node.js driver to query for null or missing fields in MongoDB.
await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);
Equality Filter
The { item : null } query matches documents that contain the item field with a null value or do not contain the item field.
const cursor = db.collection('inventory').find({ item: null });
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
const cursor = db.collection('inventory') .find({ item: { $ne : null } });
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The { item : { $type: 10 } } query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
const cursor = db.collection('inventory').find({ item: { $type: 10 } });
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The { item : { $exists: false } } query matches documents that do not contain the item field:
const cursor = db.collection('inventory').find({ item: { $exists: false } });
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the MongoDB\\Collection::find() method in the MongoDB PHP Library.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use null with the MongoDB PHP driver to query for null or missing fields in MongoDB.
$insertManyResult = $db->inventory->insertMany([ ['_id' => 1, 'item' => null], ['_id' => 2], ]);
Equality Filter
The [ item => undef ] query matches documents that contain the item field with a null value or do not contain the item field.
$cursor = $db->inventory->find(['item' => null]);
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
$cursor = $db->inventory->find(['item' => ['$ne' => null ]]);
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The [ item => [ $type => 10 ] ] query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
$cursor = $db->inventory->find(['item' => ['$type' => 10]]);
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The [ item => [ $exists => false ] ] query matches documents that do not contain the item field:
$cursor = $db->inventory->find(['item' => ['$exists' => false]]);
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the Mongo::Collection#find() method in the MongoDB Ruby Driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use nil with the MongoDB Ruby driver to query for null or missing fields in MongoDB.
client[:inventory].insert_many([ { _id: 1, item: nil }, { _id: 2 } ])
Equality Filter
The { item => nil } query matches documents that contain the item field with a nil value or do not contain the item field.
client[:inventory].find(item: nil)
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
client[:inventory].find(item: { '$ne' => nil })
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The { item => { $type => 10 } } query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
client[:inventory].find(item: { '$type' => 10 })
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The { item => { $exists => false } } query matches documents that do not contain the item field:
client[:inventory].find(item: { '$exists' => false })
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the collection.find() method in the MongoDB Scala Driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use BsonNull() with the MongoDB Scala driver to query for null or missing fields in MongoDB.
collection.insertMany(Seq( Document("""{"_id": 1, "item": null}"""), Document("""{"_id": 2}""") )).execute()
Equality Filter
The equal("item", BsonNull) query matches documents that contain the item field with a null value or do not contain the item field.
var findObservable = collection.find(equal("item", BsonNull()))
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
collection.find($ne("item", null));
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The type("item", BsonType.NULL) query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
findObservable = collection.find(bsonType("item", BsonType.NULL))
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The exists("item", exists = false) query matches documents that do not contain the item field:
findObservable = collection.find(exists("item", exists = false))
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the MongoCollection.Find() method in the MongoDB .NET/C# Driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use BsonNull.Value with the MongoDB .NET/C# Driver to query for null or missing fields in MongoDB.
var documents = new[] { new BsonDocument { { "_id", 1 }, { "item", BsonNull.Value } }, new BsonDocument { { "_id", 2 } } }; collection.InsertMany(documents);
Equality Filter
The Eq("item", BsonNull.Value) query using the FilterDefinitionBuilder.Eq() method matches documents that contain the item field with a null value or do not contain the item field.
var filter = Builders<BsonDocument>.Filter.Eq("item", BsonNull.Value); var result = collection.Find(filter).ToList();
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
var filter = Builders<BsonDocument>.Filter.Ne("item", BsonNull.Value); var result = collection.Find(filter).ToList();
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The Type("item", BsonType.Null) query using the FilterDefinitionBuilder.Type() method matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
var filter = Builders<BsonDocument>.Filter.Type("item", BsonType.Null); var result = collection.Find(filter).ToList();
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The Exists("item", false) query using the FilterDefinitionBuilder.Exists() method matches documents that do not contain the item field:
var filter = Builders<BsonDocument>.Filter.Exists("item", false); var result = collection.Find(filter).ToList();
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using mongoc_collection_find_with_opts.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use BCON_NULL with the MongoDB C driver to query for null or missing fields in MongoDB.
mongoc_collection_t *collection; mongoc_bulk_operation_t *bulk; bson_t *doc; bool r; bson_error_t error; bson_t reply; collection = mongoc_database_get_collection (db, "inventory"); bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL); doc = BCON_NEW ( "_id", BCON_INT64 (1), "item", BCON_NULL); r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error); bson_destroy (doc); if (!r) { MONGOC_ERROR ("%s\n", error.message); goto done; } doc = BCON_NEW ("_id", BCON_INT64 (2)); r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error); bson_destroy (doc); if (!r) { MONGOC_ERROR ("%s\n", error.message); goto done; } /* "reply" is initialized on success or error */ r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error); if (!r) { MONGOC_ERROR ("%s\n", error.message); }
Equality Filter
The { item, BCON_NULL } query matches documents that contain the item field with a null value or do not contain the item field.
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ("item", BCON_NULL); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
filter = BCON_NEW ("item", BCON_NULL); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The { item, { $type, BCON_NULL } } query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "item", "{", "$type", BCON_INT64 (10), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The { item, { $exists, BCON_BOOL (false) } } query matches documents that do not contain the item field:
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "item", "{", "$exists", BCON_BOOL (false), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
Clean up any open resources by calling the following methods, as appropriate:
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using the Collection.Find function in the MongoDB Go Driver.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use nil with the MongoDB Go driver to query for null or missing fields in MongoDB.
docs := []any{ bson.D{ {"_id", 1}, {"item", nil}, }, bson.D{ {"_id", 2}, }, } result, err := coll.InsertMany(context.TODO(), docs)
Equality Filter
The item => nil query matches documents that contain the item field with a nil value or do not contain the item field.
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", nil}, })
The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", bson.D{"$ne": nil}}, })
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The following query matches only documents that contain the item field with a value of BSON Type Null (BSON Type 10):
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", bson.D{ {"$type", 10}, }}, })
The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The bson.D{{"item", bson.D{{"$exists", false}}}} query matches documents that do not contain the item field:
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", bson.D{ {"$exists", false}, }}, })
The query only returns the document that does not contain the item 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.
This page provides examples of operations that query for null values using MongoDB Compass.
The examples on this page use the inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:
Important
Use null with MongoDB Compass to query for null or missing fields in MongoDB.
[ { "_id": 1, "item": null }, { "_id": 2 } ]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
Equality Filter
The { item : null } query matches documents that contain the item field with a null value or do not contain the item field.
Copy the following query filter document into the query bar and click Find:
{ item: null }

The query returns both documents in the collection.
Note
Dotted Paths That Traverse Arrays
A dotted path can produce results you might not expect when the path traverses an array. For details, see Null Comparisons on Array Fields.
Non-Equality Filter
To query for fields that exist and are not null, use the { $ne : null } filter.
Note
Dotted Paths That Traverse Arrays
The { $ne : null } filter on a dotted path can match a document even when { path : { $exists: true } } does not, when the path traverses an array. For details, see Null Comparisons on Array Fields.
The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.
{ item: { $ne : null } }
Null Comparisons on Array Fields
When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to null on array fields produce results that you might not expect. The examples in this section use mongosh syntax.
Consider a collection that contains the following documents:
{ _id: 1, a: null } { _id: 2, a: [ ] } { _id: 3, a: [ 1, "string", 4 ] } { _id: 4, a: [ 1, null, 4 ] } { _id: 5, a: [ { b: 1 }, { b: 3 } ] } { _id: 6, a: [ { b: 3 }, { } ] } { _id: 7 }
Equality on an Array Field
The { a: null } query matches a document when the a field is null, when the a field is missing, or when the a array contains at least one null element. The query matches the documents with _id values 1, 4, and 7.
An empty array and an array without a null element do not match. The a field exists in both cases, and neither array contains a null element, so the documents with _id values 2 and 3 are not returned.
The { a: { $ne: null } } query returns the complement, which is every document that { a: null } does not match. The query matches the documents with _id values 2, 3, 5, and 6, including the document that holds an empty array.
Equality on a Dotted Path
The { "a.b": null } query matches a document in any of these cases:
The
afield is missing, or holds a value that is neither an object nor an array.The
afield holds an object wherebis missing ornull.The
afield holds an array with at least one object wherebis missing ornull.
Against the sample documents, the query matches the _id values 1, 6, and 7. The document with _id: 6 matches because one of its array elements, { }, is missing b, even though the other element has a non-null b value.
An empty array, an array of scalar values, and an array that contains a literal null do not match, because none of these array shapes contains an object for b to be missing from. The documents with _id values 2, 3, and 4 do not match for this reason. The document with _id: 5 does not match either, because every object in its array has a non-null b value.
The { "a.b": { $ne: null } } query returns the complement, which is every document that { "a.b": null } does not match: the documents with _id values 2, 3, 4, and 5. That result is not the same as the set of documents that have a non-null value for a.b. Neither _id: 2 nor _id: 3 has any value at a.b, yet the { $ne: null } filter still matches them. A { $ne: null } filter on a dotted path does not necessarily return the same documents as an $exists check on that path.
Type Check
The { item : { $type: 10 } } query matches only documents that contain the item field with a null value. The value of the item field is of BSON Type Null (BSON Type 10):
Copy the following query filter document into the query bar and click Find:
{ item : { $type: 10 } }

The query returns only the document where the item field has a value of null.
Existence Check
The following example queries for documents that do not contain a field.
The { item : { $exists: false } } query matches documents that do not contain the item field:
Copy the following query filter document into the query bar and click Find:
{ item : { $exists: false } }

The query only returns the document that does not contain the item 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.
Query for Null or Missing Fields with MongoDB Atlas
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:
In the MongoDB Atlas UI, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Clusters under the Database heading.
The Clusters page displays.
Specify a query filter document.
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 } }