On this page
Query for Null or Missing Fields
Different query operators in MongoDB treat null
values differently.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
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:
- Python
- Motor
- C#
- Perl
- Ruby
- Other
- Scala
Important
You must use None
with the PyMongo Python driver to query for null
or missing fields in MongoDB.
Important
You must use None
with the Motor driver to query for null
or missing fields in MongoDB.
Important
You must use BsonNull.Value
with the MongoDB C# 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.
Important
You must use nil
with the MongoDB Ruby driver to query for null
or missing fields in MongoDB.
Important
You must use BsonNull()
with the MongoDB Scala driver to query for null
or missing fields in MongoDB.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
You can run the operation in the web shell below:
https://mws.mongodb.com/?version=3.6[
{ _id: 1, item: null },
{ _id: 2 }
]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
collection.insertMany(asList(
Document.parse("{'_id': 1, 'item': null}"),
Document.parse("{'_id': 2}")
));
await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);
$insertManyResult = $db->inventory->insertMany([
['_id' => 1, 'item' => null],
['_id' => 2],
]);
await db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{'_id': 1, 'item': null}"),
Document.parse("{'_id': 2}")
));
var documents = new[]
{
new BsonDocument { { "_id", 1 }, { "item", BsonNull.Value } },
new BsonDocument { { "_id", 2 } }
};
collection.InsertMany(documents);