Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The $cond
operator is used to evaluate a condition and return one of two expressions based on the result. It's similar to the ternary operator in many programming languages. The $cond
operator can be used within aggregation pipelines to add conditional logic to your queries.
Syntax
The basic syntax for the $cond
operator is:
{
$cond: {
if: <boolean-expression>,
then: <true-case>,
else: <false-case>
}
}
Parameters
Description | |
---|---|
if | A boolean expression that is evaluated. |
then | The expression to return if the if condition evaluates to true. |
else | The expression to return if the if condition evaluates to false. |
Examples
Let's understand the usage with the following sample json.
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
}
],
"tag": [
"#ShopLocal",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
Example 1: Determine high sales categories
To determine if the sales for each category are considered "high" or "low" based on a threshold value of $250,000.
db.stores.aggregate([
{
$project: {
_id: 0,
storeId: "$storeId",
category: "$sales.salesByCategory.categoryName",
sales: "$sales.salesByCategory.totalSales",
salesCategory: {
$cond: {
if: { $gte: ["$sales.salesByCategory.totalSales", 250000] },
then: "High",
else: "Low"
}
}
}
},
// Limit the result to the first 3 documents
{ $limit: 3 }
])
This query would return the following document.
[
{
"sales": [ 35921, 1000 ],
"category": [ "DJ Headphones", "DJ Cables" ],
"salesCategory": "High"
},
{ "sales": [ 4760 ], "category": [ "Guitars" ], "salesCategory": "High" },
{
"sales": [
14697, 44111,
37854, 46211,
7269, 25451,
21083
],
"category": [
"Washcloths",
"Innerspring Mattresses",
"Microfiber Towels",
"Shower Curtains",
"Bathrobes",
"Tablecloths",
"Bath Accessories"
],
"salesCategory": "High"
}
]
Example 2: Determine full-time or part-time dominance
To determine whether a store employs more full-time or part-time staff.
db.stores.aggregate([
{
$project: {
name: 1,
staffType: {
$cond: {
if: { $gte: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] },
then: "More Full-Time",
else: "More Part-Time"
}
}
}
},
// Limit the result to the first 3 documents
{ $limit: 3 }
])
This query would return the following document.
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"staffType": "More Full-Time"
},
{
"_id": "649626c9-eda1-46c0-a27f-dcee19d97f41",
"name": "VanArsdel, Ltd. | Musical Instrument Outlet - East Cassie",
"staffType": "More Full-Time"
},
{
"_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34",
"name": "Northwind Traders | Bed and Bath Place - West Oraland",
"staffType": "More Part-Time"
}
]
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB