1

Group by with limit and sort for each group

db.getCollection('logSrcDest').aggregate(
     {
        $group: {
            _id: {
                "src": "$srcId",
                "dest": "$destId",
                "account": "$account"
            },  
            total: { $sum : "$count" }
        }
    },
    { $sort: { total : -1 } },
    { $limit: 10 }
);

I have gone through this : mongo with limit and sort but i couldn't find the answer i required.

Any help in this regard will be appreciated

Community
  • 1
  • 1
user2888996
  • 417
  • 8
  • 20
  • 1
    Possible duplicate of [Mongodb aggregation pipeline how to limit a group push](http://stackoverflow.com/questions/24594049/mongodb-aggregation-pipeline-how-to-limit-a-group-push) – styvane Sep 29 '16 at 08:30
  • @Styvane That's is different from what i am looking for – user2888996 Sep 29 '16 at 09:36
  • In that case, please show us sample documents with the expected result. – styvane Sep 29 '16 at 10:48
  • Please add sample documents with the expected result to your question. – styvane Sep 30 '16 at 07:18
  • this is stored: {account=123,src=101,dest=202,count=10} { account=123,src=102,dest=203,count=13} {account=123,src=104,dest=205,count=8} {account=123,src=105,dest=206,count=12} { account=199,src=101,dest=202,count=19} { account=199,src=102,dest=203,count=23} {account=199,src=103,dest=204,count=15} {account=199,src=104,dest=205,count=28} {account=199,src=105,dest=206,count=17} Expected output in case of top 2 results: {account=123,src=102,dest=203,count=13} {account=123,src=105,dest=206,count=12} {account=199,src=104,dest=205,count=28} {account=199,src=102,dest=203,count=23} – user2888996 Sep 30 '16 at 07:27
  • Please use the [edit](http://stackoverflow.com/posts/39764531/edit) link on your question. Also make sure you post a valid JSON document. – styvane Sep 30 '16 at 07:28
  • Please accept like this, i am unable to make json format for sample – user2888996 Sep 30 '16 at 07:33
  • Don't expect answers with poorly formatted question. Present question well then someone will give attention. – nurgasemetey Oct 01 '16 at 10:52
  • I think you are looking for this-> http://stackoverflow.com/questions/33458107/limit-and-sort-each-group-by-in-mongodb-using-aggregation – Riya Saxena Oct 25 '16 at 10:23

0 Answers0