$not (布尔表达式)

适用对象: MongoDB vCore

运算符 $not 返回表达式的相反布尔值。 它在单个表达式上执行逻辑 NOT作。 如果表达式的 true计算结果为 , $notfalse返回 。 如果表达式的 false计算结果为 , $nottrue返回 。 此运算符可用于否定条件并查找不符合特定条件的文档。

语法

$not 运算符的语法如下:

{
  $not: <expression>
}

参数

DESCRIPTION
<expression> 要否定的单个表达式。 运算符 $not 返回与此表达式的布尔值相反的逻辑。

示例:

示例 1:识别非大容量存储

该示例查找销售量不超过 50,000 的商店。

db.stores.aggregate([
  {
    $project: {
      name: 1,
      totalSales: "$sales.salesByCategory.totalSales",
      isNotHighVolume: {
        $not: { $gt: ["$sales.salesByCategory.totalSales", 20000] }
      },
      storeCategory: {
        $cond: [
          { $not: { $gt: ["$sales.salesByCategory.totalSales", 20000] } },
          "Small-Medium Store",
          "High Volume Store"
        ]
      }
    }
  },
  { $limit: 2 }
])

该查询标识非大容量作的存储。

 {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "totalSales": [ 37978 ],
    "isNotHighVolume": false,
    "storeCategory": "High Volume Store"
  },
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "totalSales": [ 25731 ],
    "isNotHighVolume": false,
    "storeCategory": "High Volume Store"
  }

示例 2:验证存储而不显示不足问题

该示例标识没有人员不足问题的商店(不超过 10 名员工总数)。

db.stores.aggregate([
  { $match: {"_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6"} },
  {
    $project: {
      name: 1,
      totalStaff: { $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] },
      isNotUnderstaffed: {
        $not: { $lt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 10] }
      },
      staffingStatus: {
        $cond: [
          { $not: { $lt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 10] } },
          "Adequately Staffed",
          "Understaffed"
        ]
      }
    }
  }
])

查询确定存储是否未使用。

{
  "_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6",
  "name": "VanArsdel, Ltd. | Picture Frame Store - Port Clevelandton",
  "totalStaff": 14,
  "isNotUnderstaffed": true,
  "staffingStatus": "Adequately Staffed"
}