$literal
The $literal operator in Amazon DocumentDB is used to represent a literal value within an aggregation pipeline stage. It allows you to include a specific value, such as a number, string, or boolean, without interpreting it as a field reference or expression.
This operator is particularly useful when you need to include a literal value as part of a more complex aggregation pipeline, such as when building dynamic query filters or performing calculations.
Parameters
None
Example (MongoDB Shell)
The following example demonstrates the use of the $literal operator to include a literal value in an aggregation pipeline. The $literal operator is used to include the value 18 as a literal value in the $gt expression. This allows the aggregation pipeline to compare the age field against the literal value 18 to determine if the person is an adult.
Create sample documents
db.collection.insertMany([ { "name": "John Doe", "age": 30, "city": "New York" }, { "name": "Jane Doe", "age": 25, "city": "Los Angeles" }, { "name": "Bob Smith", "age": 35, "city": "Chicago" } ]);
Query example
db.collection.aggregate([ { $project: { name: 1, age: 1, city: 1, isAdult: { $gt: ["$age", { $literal: 18 }] } } } ]);
Output
[
{
"_id": ObjectId("601234567890abcdef012345"),
"name": "John Doe",
"age": 30,
"city": "New York",
"isAdult": true
},
{
"_id": ObjectId("601234567890abcdef012346"),
"name": "Jane Doe",
"age": 25,
"city": "Los Angeles",
"isAdult": true
},
{
"_id": ObjectId("601234567890abcdef012347"),
"name": "Bob Smith",
"age": 35,
"city": "Chicago",
"isAdult": true
}
]
Code examples
To view a code example for using the $literal command, choose the tab for the language that you want to use: