演算子は $cond 、条件を評価し、結果に基づいて 2 つの式のいずれかを返すために使用されます。 これは、多くのプログラミング言語の三項演算子に似ています。 この演算子は $cond 、集計パイプライン内でクエリに条件付きロジックを追加するために使用できます。
構文
{
$cond: {
if: <boolean-expression>,
then: <true-case>,
else: <false-case>
}
}
パラメーター
| パラメーター | Description |
|---|---|
| もし | 評価されるブール式。 |
| then | 条件が true に評価された場合に if 返す式。 |
| その他 | 条件が false に評価された場合に if 返す式。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
例 1: 高売上カテゴリを決定する
このクエリでは、しきい値 $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
}
])
このクエリによって返される最初の 3 つの結果は次のとおりです。
[
{
"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"
}
]
例 2: フルタイムまたはパートタイムの支配を決定する
このクエリは、店舗がより多くのフルタイムスタッフとパートタイムスタッフのどちらを採用しているかを決定します。
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
}
])
このクエリによって返される最初の 3 つの結果は次のとおりです。
[
{
"_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"
}
]