$cond(条件表达式)

$cond 运算符用于计算条件,并根据结果返回两个表达式其中一个。 其类似于许多编程语言中的三元运算符。 $cond 运算符可在聚合管道中使用,以将条件逻辑添加至查询。

语法

$cond 运算符的基本语法为:

{
   $cond: {
      if: <boolean-expression>,
      then: <true-case>,
      else: <false-case>
   }
}

参数

说明
if 求值的布尔表达式。
然后在受影响的域控制器上,运行 如果 if 条件的计算结果为 true,则返回的表达式。
else 如果 if 条件的计算结果为 false,则返回的表达式。

示例

让我们通过以下示例 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"
  ]
}

示例 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 }  
])

此查询将返回以下文档。

[
  {
    "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 }  
])

此查询将返回以下文档。

[
  {
    "_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"
  }
]