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).
$cond
The $cond operator in Amazon DocumentDB is used to evaluate a conditional expression and return one of two possible result expressions.
Parameters
-
if: The boolean expression to evaluate.
-
then: The expression to return if the if expression is true.
-
else: The expression to return if the if expression is false.
Example (MongoDB Shell)
The following example demonstrates the use of the $cond operator to return a value based on the age of a person.
Create sample documents
db.people.insertMany([
{ _id: 1, name: "John Doe", age: 35 },
{ _id: 2, name: "Jane Doe", age: 25 },
{ _id: 3, name: "Bob Smith", age: 65 }
]);
Query example
db.people.aggregate([
{
$project: {
name: 1,
ageGroup: {
$cond: {
if: { $lt: ["$age", 30] },
then: "young",
else: {
$cond: {
if: { $lt: ["$age", 65] },
then: "middle-aged",
else: "elderly"
}
}
}
}
}
}
])
Output
[
{ "_id" : 1, "name" : "John Doe", "ageGroup" : "middle-aged" },
{ "_id" : 2, "name" : "Jane Doe", "ageGroup" : "young" },
{ "_id" : 3, "name" : "Bob Smith", "ageGroup" : "elderly" }
]
Code examples
To view a code example for using the $cond 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('people');
const result = await collection.aggregate([
{ $project: {
name: 1,
ageGroup: {
$cond: {
if: { $lt: ["$age", 30] },
then: "young",
else: {
$cond: {
if: { $lt: ["$age", 65] },
then: "middle-aged",
else: "elderly"
}
}
}
}
}
}
]).toArray();
console.log(result);
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.people
result = list(collection.aggregate([
{
'$project': {
'name': 1,
'ageGroup': {
'$cond': {
'if': { '$lt': ["$age", 30]},
'then': "young",
'else': {
'$cond': {
'if': { '$lt': ["$age", 65]},
'then': "middle-aged",
'else': "elderly"
}
}
}
}
}
}
]))
print(result)
client.close()
example()