首页 > 解决方案 > MongoDB 检查 $switch 语句中的空字段

问题描述

我试图检查我的 $switch 语句中的空/缺失字段,但它不起作用。这是我的代码

$switch: {
                branches: [
                    {
                        case: {
                            $and: [
                                { $gte: ["$SmartPriority", 6] },
                                { $ne: ["$FlashTRFPromotionDate", null] },
                                { $ne: ["$FlashTRFPromotionDate", ""] },
                                { $ne: ["$FlashTRFPromotionDate", false] }
                            ]
                        },
                        then: "Greater than"
                    }
                ],
                default: "EMPTY"
            }

即使 $ne 为“null”或“false”,它也不会显示 EMPTY(默认值)。我的表情应该是什么?

标签: javascriptnode.jsmongodbmongooseaggregation-framework

解决方案


它应该是这样的

"$switch": {
   "branches": [
    { "case": { "$gte": ["$SmartPriority", 6] }, "then": "Greater than" },
    { "case": { "$ne": ["$FlashTRFPromotionDate", null] }, "then": "EMPTY" },
    { "case": { "$ne": ["$FlashTRFPromotionDate", ""] }, "then": "Greater than" },
    { "case": { "$ne": ["$FlashTRFPromotionDate", false] }, "then": "EMPTY" }
  ],
  "default": "EMPTY"
}

推荐阅读