Mongodb Aggregation $ group,$ sum和$ sort

时间:2013-08-27 16:29:37

标签: mongodb aggregation-framework nosql

这是我的文档架构:

{
    "_id" : ObjectId("5203c8b91afdd7160c6ecfd3"),
    "answer_calls" : "",
    "start" : "10:00",
    "end" : "10:30",
    "VDN_name" : "SP_SYNDICATED_7",
    "total_calls" : "1",
    "date" : "01/07/2013",
    "abandoned_calls" : "",
    "voicemail_calls" : ""
}

我通过聚合框架尝试:

> dir.aggregate( 
[  
 { $group: 
    {_id: {fecha:"$date", hora: "$start"}, 
    llamadas :{ $sum:"$total_calls"}, 
    abandoned: {$sum:"$abandoned_calls"}, 
    mail: {$sum:"$voicemail_calls"} 
    } 
 },
 { $sort: {fecha:1,hora:1} }
] )

以下是结果示例:

{
    "_id" : {
        "fecha" : "16/07/2013",
        "hora" : "18:30"
    },
    "llamadas" : 0,
    "abandoned" : 0,
    "mail" : 0
}

问题是它没有 $ sum ,也没有 $ sort

有人知道为什么吗?

1 个答案:

答案 0 :(得分:6)

解决方案是:

dir.aggregate( 
[  
 { $group: 
   {_id: {fecha:"$date", hora: "$start"}, 
   llamadas :{$sum:"$total_calls"}, 
   answer :{ $sum:"$answer_calls"}, 
   abandoned: {$sum:"$abandoned_calls"}, 
   mail: {$sum:"$voicemail_calls"} } 
 }, 
 { $sort: 
   {'_id.fecha':1 , '_id.hora':1} }
] )

再次感谢Sammaye和JohnnyHK