Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$ifNull
The $ifNull operator is used to return a specified value if the input expression evaluates to null or undefined. This operator can be useful in scenarios where you want to provide a default value or handle null/undefined cases.
Parameters
Example (MongoDB Shell)
The following example demonstrates the usage of the $ifNull operator to provide a default value when the name field is null or undefined.
Create sample documents
db.users.insertMany([
{ _id: 1, name: "John" },
{ _id: 2, name: null },
{ _id: 3 }
]);
Query example
db.users.aggregate([
{
$project: {
_id: 1,
name: { $ifNull: ["$name", "No Name"] }
}
}
]);
Output
[
{ "_id": 1, "name": "John" },
{ "_id": 2, "name": "No Name" },
{ "_id": 3, "name": "No Name" }
]
Code examples
To view a code example for using the $ifNull command, choose the tab for the language that you want to use:
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
const db = client.db('test');
const collection = db.collection('users');
const pipeline = [
{
$project: {
_id: 1,
name: { $ifNull: ["$name", "No Name"] }
}
}
];
const cursor = await collection.aggregate(pipeline);
await cursor.forEach(doc => {
console.log(doc);
});
await client.close();
}
example();
- Python
-
from pymongo import MongoClient
def example():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client.test
collection = db.users
pipeline = [
{
"$project": {
"_id": 1,
"name": { "$ifNull": ["$name", "No Name"] }
}
}
]
result = collection.aggregate(pipeline)
for doc in result:
print(doc)
client.close()
example()