Fix This Page
Navigation

Query for Null or Missing Fields

Different 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 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 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 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 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:

Important

You must use None with the PyMongo Python driver to query for null or missing fields in MongoDB.

Important

You must use undef with the MongoDB Perl driver to query for null or missing fields in MongoDB.

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])

You can run the operation in the web shell below:

db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
$insertManyResult = $db->inventory->insertMany([
    ['_id' => 1, 'item' => null],
    ['_id' => 2],
]);
$db->coll("inventory")->insert_many( [ { _id => 1, item => undef }, { _id => 2 } ] );

Equality Filter

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 [ 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 => undef } query matches documents that either contain the item field whose value is null or that do not contain the item field.

For example, the following query returns both documents:

db.inventory.find( { item: null } )
cursor = db.inventory.find({"item": None})
$cursor = $db->inventory->find(['item' => null]);
$cursor = $db->coll("inventory")->find( { item => undef } );

Type Check

The { item : { $type: 10 } } query matches documents that contains the item field whose value is null only; i.e. the value of the item field is of BSON Type Null (i.e. 10) :

The { item : { $type: 10 } } query matches documents that contains the item field whose value is null only; i.e. the value of the item field is of BSON Type Null (i.e. 10) :

The [ item => [ $type => 10 ] ] query matches documents that contains the item field whose value is null only; i.e. the value of the item field is of BSON Type Null (i.e. 10) :

The { item => { $type => 10 } } query matches documents that contains the item field whose value is null only; i.e. the value of the item field is of BSON Type Null (i.e. 10) :

db.inventory.find( { item : { $type: 10 } } )
cursor = db.inventory.find({"item": {"$type": 10}})
$cursor = $db->inventory->find(['item' => ['$type' => 10]]);
$cursor = $db->coll("inventory")->find( { item => { '$type' => 10 } } );

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

Existence Check

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 { item => { $exists => false } } query matches documents that do not contain the item field:

db.inventory.find( { item : { $exists: false } } )
cursor = db.inventory.find({"item": {"$exists": False}})
$cursor = $db->inventory->find(['item' => ['$exists' => false]]);
# For boolean values, use boolean.pm for 'true' and 'false'
$cursor = $db->coll("inventory")->find( { item => { '$exists' => false } } );

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

See also

The reference documentation for the $type and $exists operators.