【问题标题】:Aggregate Invalid operator '$switch'聚合无效运算符'$switch'
【发布时间】:2018-06-15 10:51:17
【问题描述】:

将 mongoose 与 Mongodb 和 node.js 一起使用,我想执行一个需要确定一个值是否基于时间的聚合,因此如果是,我可以转换为秒,如果不是基于时间,则保持原样。

我在除法运算符中添加了一个 $switch 运算符来执行此操作。出于某种原因,我不断收到一条错误消息,指出“MongoError: invalid operator '$switch'”。

代码如下:

$divide: [{
    $switch: {
        branches: [
            {
                case: { $eq: [ "$timeType", "min"]},
                then: { $multiply: [ "$route.completedTotal", "60" ] }
            },
            {
                case: { $eq: [ "$timeType", "sec"]},
                then: "$route.completedTotal"
            },
            {
                case: { $eq: [ "$timeType", "hr"]},
                then: { $multiply: [ "$route.completedTotal", "3600" ] }
            }
        ],
        default: "$route.completedTotal"
    }
}, {
    $switch: {
        branches: [
            {
                case: { $eq: [ "$timeType", "min"]},
                then: { $multiply: [ "$route.total", "60" ] }
            },
            {
                case: { $eq: [ "$timeType", "sec"]},
                then: "$route.total"
            },
            {
                case: { $eq: [ "$timeType", "hr"]},
                then: { $multiply: [ "$route.total", "3600" ] }
            }
        ],
        default: "$route.total"
    }
}]

如何修复我的 $switch 运算符以使其正常工作?如果我不能在这种情况下使用 $switch 运算符,那么解决此逻辑的替代方法是什么?

【问题讨论】:

  • 你的mongodb版本?
  • 版本:3.2.11
  • 您需要将您的 mongodb 版本更新到 3.43.6 因为在 3.4$switch /b>

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

如前所述,$switch 是在 MongoDB 3.4 中引入的。虽然在可能的情况下升级到那个版本可能是一个好主意,但自从使用 $cond 发布聚合框架以来,同样的声明已经成为可能:

"$divide": [
  {
    "$cond": {
      "if": { "$eq": [ "$timeType", "min" ] },
      "then" { "$multiply": [ "$route.completedTotal", "60" ] },
      "else": {
        "$cond": {
          "if": { "$eq": [ "$timeType", "sec"] },
          "then": "$route.completedTotal",
          "else": {
            "$cond": {
              "if": { "$eq": [ "$timeType", "hr"] },
              "then": { "$multiply": [ "$route.completedTotal", "3600" ] },
              "else": "$route.completedTotal"
            }
          }
        }
      }
    }
  },
  {
    "$cond": {
      "if": { "$eq": [ "$timeType", "min" ] },
      "then": { "$multiply": [ "$route.total", "60" ] },
      "else": {
        "$cond": {
          "if": { "$eq": [ "$timeType", "sec" ] },
          "then": "$route.total",
          "else": {
            "$cond": {
              "if": { "$eq": [ "$timeType", "hr" ] },
              "then": { "$multiply": [ "$route.total", "3600" ] },
              "else": "$route.total"
            }
          }
        }
      }
    }
  }
]

这是使用if/then/else“keys”语法进行澄清,但早期的数组语法仍然有效,基本上意味着该语句可以一直写回 MongoDB 2.2。

这应该是有道理的,因为任何其他编程语言实现中的“switch”语句只是连续编写if/then/else if 的一种“更干净”的方式,所以任何做同样事情的事情也会产生同样的事情是有道理的结果,但只是语法不同。

【讨论】:

    猜你喜欢
    • 2016-11-26
    • 2017-05-21
    • 1970-01-01
    • 2020-11-05
    • 2021-06-16
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多