- MongoDB CRUD Operations >
- Query Documents >
- Query for Null or Missing Fields
Query for Null or Missing Fields¶
This page provides examples in:
Different query operators in MongoDB treat null
values differently.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
This page provides examples of operations that query for null
values using the
db.collection.find()
method in the
mongo
shell. The examples on this page use the
inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using
MongoDB Compass. The examples on this
page use the inventory
collection. Populate the
inventory
collection with the following documents:
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. To populate the inventory
collection, run the
following:
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. To populate the inventory
collection, run the
following:
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. To
populate the inventory
collection, run the following:
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. To
populate the inventory
collection, run the following:
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. To populate the inventory
collection, run the
following:
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. To populate the inventory
collection, run the
following:
This page provides examples of operations that query for null
values using the
MongoCollection.Find()
method in the
MongoDB C# Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the
MongoDB::Collection::find() method
in the
MongoDB Perl Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
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. To
populate the inventory
collection, run the following:
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. To
populate the inventory
collection, run the following:
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. To
populate the inventory
collection, run the following:
- Python
- Motor
- C#
- Perl
- Ruby
- Other
Important
Use None
with the PyMongo Python driver to
query for null
or missing fields in MongoDB.
Important
Use None
with the Motor driver to
query for null
or missing fields in MongoDB.
Important
Use BsonNull.Value
with the MongoDB C# driver to
query for null
or missing fields in MongoDB.
Important
Use undef
with the MongoDB Perl driver to
query for null
or missing fields in MongoDB.
Important
Use nil
with the MongoDB Ruby driver to
query for null
or missing fields in MongoDB.
Important
Use BsonNull()
with the MongoDB Scala driver to query
for null
or missing fields in MongoDB.
Important
Use nil
with the MongoDB Go driver to
query for null
or missing fields in MongoDB.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
You can run the operation in the web shell below:
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
Equality Filter¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The { item : null }
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The { item : null }
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The { item : None }
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The eq("item", null)
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The { item : null }
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The [ item => undef ]
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The { item : None }
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The eq("item", null)
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The Eq("item", BsonNull.Value)
query using the FilterDefinitionBuilder.Eq() method
matches documents that either contain the item
field whose
value is null
or that do not contain the item
field.
The { item => undef }
query matches documents that either
contain the item
field whose value is null
or that
do not contain the item
field.
The { item => nil }
query matches documents that either
contain the item
field whose value is nil
or that
do not contain the item
field.
The equal("item", BsonNull)
query matches documents that
either contain the item
field whose value is null
or
that do not contain the item
field.
The item => nil
query matches documents that either
contain the item
field whose value is nil
or that
do not contain the item
field.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The query returns both documents in the collection.
Type Check¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The { item : { $type: 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The type("item", BsonType.NULL)
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The [ item => [ $type => 10 ] ]
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The type("item", BsonType.NULL)
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The Type("item", BsonType.Null)
query using the
FilterDefinitionBuilder.Type()
method matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The { item => { $type => 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The { item => { $type => 10 } }
query matches only
documents that contain the item
field whose value is
null
; i.e. the value of the item
field is of
BSON Type Null
(type number 10
) :
The following query matches only
documents that contain the item
field whose value is of
BSON Type Null
(type number 10
) :
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
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. [1]
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The { item : { $exists: false } }
query matches documents
that do not contain the item
field:
The { item : { $exists: false } }
query matches documents
that do not contain the item
field:
The { item : { $exists: False } }
query matches documents
that do not contain the item
field:
The exists("item", false)
query matches documents that
do not contain the item
field:
The { item : { $exists: false } }
query matches documents
that do not contain the item
field:
The [ item => [ $exists => false ] ]
query matches documents
that do not contain the item
field:
The { item : { $exists: False } }
query matches documents
that do not contain the item
field:
The exists("item", false)
query matches documents that do
not contain the item
field:
The Exists("item", false)
query using the FilterDefinitionBuilder.Exists()
method matches documents that do not contain the item
field:
The { item => { $exists => false } }
query matches documents
that do not contain the item
field:
The { item => { $exists => false } }
query matches documents
that do not contain the item
field:
The exists("item", exists = false)
query matches documents
that do not contain the item
field:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The query only returns the document that does not contain the
item
field.
[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. |