【问题标题】:How to use $split operator with aggregation如何在聚合中使用 $split 运算符
【发布时间】:2017-09-19 19:58:53
【问题描述】:

我想在日期字段上使用 $split 运算符。

[{
 "status": true,
 "beaconData" :{
    "date" : "15/07/2017, 11:00",
    "charge" : 15,
 },
 {
 "status": true,
 "beaconData" :{
    "date" : "2/07/2017, 15:00",
    "charge" : 35,
 },
 {
 "status": true,
 "beaconData" :{
    "date" : "2/07/2017, 11:05",
    "charge" : 5,
 }]

我是这样用的

db.orders.aggregate( [ { $project:{data:{$split:["$beaconData.date", ", "] }, 'status':1 }}])

但错误返回像

"errmsg" : "异常:无效的操作符'$split'",

“代码”:15999,

【问题讨论】:

  • $split 运算符从 3.4 版本开始可用。
  • 有没有办法根据日期计算总费用,例如 [{'status': true,''beaconData':{'date':'2/07/2017', 'totalCharge': 40}}, {'status': true,''beaconData':{'date':'15/07/2017', 'totalCharge':15}},

标签: node.js mongodb mongoose aggregation-framework


【解决方案1】:
//$split works in 3.4 u can use $substr in ur case if ur date is always in the same format you asked
 db.orders.aggregate(
       [
         {
           $project:
              {
                data: { $substr: [ "$beaconData.date", 0, 9 ] }
              }
          }
       ]
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    相关资源
    最近更新 更多