Details
-
Improvement
-
Resolution: Won't Do
-
Major - P3
-
None
-
None
Description
I really don't know if its a bug (because the documentation its no clear about the behavior). However it is not 'intuitive' and it takes a long time to realize where is the problem.
note: (if we add 'sort' before 'group') aggregate do not return values ordered by total
WRONG BEHAVIOR:
db.connection.collection( statsCollection ).aggregate( [
|
{ $match : { } },
|
{ $unwind: '$states' },
|
{ $sort : { total : -1 } },
|
{ $group : { _id: "$states._id" , total: { $sum: "$states.count" } } }
|
],
|
function(err, result) {
|
if(err)
|
res.status(404).json({ status : 'error' })
|
else{
|
console.log(result);
|
res.send(result)
|
};
|
}
|
);
|
GOOD BEHAVIOR:
db.connection.collection( statsCollection ).aggregate( [
|
{ $match : { } },
|
{ $unwind: '$states' },
|
{ $group : { _id: "$states._id" , total: { $sum: "$states.count" } } },
|
{ $sort : { total : -1 } }
|
],
|
function(err, result) {
|
if(err)
|
res.status(404).json({ status : 'error' })
|
else{
|
console.log(result);
|
res.send(result)
|
};
|
}
|
);
|