Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Aggregation $group, $sum and $sort

This is my document schema:

{
    "_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" : ""
}

I try, by aggregation-framework, this:

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

And here is an example of the result:

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

The problem is that it does not $sum and neither $sort

Does anybody know why?

like image 678
Willem Avatar asked Aug 27 '13 16:08

Willem


1 Answers

The solution was:

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

Thank you again to Sammaye and JohnnyHK

like image 194
Willem Avatar answered Oct 27 '22 19:10

Willem