【发布时间】: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.4 或 3.6 因为在 3.4$switch /b>
标签: mongodb mongoose mongodb-query aggregation-framework