MongoDB检查$ switch语句中的空字段

时间:2018-10-09 00:17:15

标签: javascript node.js mongodb mongoose aggregation-framework

我正在尝试检查$ switch语句中的null / missing字段,但无法正常工作。这是我的代码

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

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

1 个答案:

答案 0 :(得分:1)

应该是这样

"$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"
}