如何使用$ split运算符和聚合

时间:2017-09-19 19:58:53

标签: node.js mongodb mongoose aggregation-framework

我想在日期字段中使用 $ 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'”,

     

“code”:15999,

1 个答案:

答案 0 :(得分:2)

//$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 ] }
              }
          }
       ]
    )