mala_sf
mala_sf

Reputation: 35

How to sort a $group aggregate output with mongodb

I am looking sort a collection of documents (emails) by the id of the sender, and then return the sender id's with the most frequent sends, limited to 5. I'm fairly close but struggling with the syntax now that I need to sort the group. I understand that with mongodb the $group operator does not sort the output, how can I sort the output now once it has been grouped. I've tried tacking on .sort() to the end of the query sequence, as well as setting the output equal to a variable and calling .sort().limit() on that to no avail.

I have as follows

cursor = db.emailcol.aggregate( 

    [{ $group : {_id : {emails_sent: "$sender_id"}, number : { $sum : 1} }},
        {$sort : {"_id.sender_id": 1}}

    ]
);

while ( cursor.hasNext() ) {
    printjson( cursor.next() );
};

which returns:

{ "_id" : { "emails_sent" : "6" }, "number" : 3 }
{ "_id" : { "emails_sent" : "5" }, "number" : 2 }
{ "_id" : { "emails_sent" : "3" }, "number" : 1 }
{ "_id" : { "emails_sent" : "4" }, "number" : 2 }
{ "_id" : { "emails_sent" : "2" }, "number" : 1 }

How can I either run a sort method on the output of the grouping, or just add the sort and limit to the query?

Upvotes: 0

Views: 4468

Answers (1)

Anand Jayabalan
Anand Jayabalan

Reputation: 12904

The sort phase of your aggregation pipeline should be:

{$sort : {"_id.emails_sent": 1}}

rather than:

{$sort : {"_id.sender_id": 1}}

as the output of the $group phase does not have a sub-document with field sender_id. To debug such issues, it would help to see the output of every phase of the pipeline.

Upvotes: 2

Related Questions