I have a mongodb document something like below :
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44901,
"total_points" : 7.00,
},
{
"match_key" : 44901,
"total_points" : 11.00,
},
{
"match_key" : 44801,
"total_points" : 7.00,
}
I want to sort the above docs ,but want to keep the documents with same match_key together.
Actual result :
{
"match_key" : 44901,
"total_points" : 11.00,
}
,
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44801,
"total_points" : 7.00,
}
{
"match_key" : 44901,
"total_points" : 6.00,
},
But you see that same match keys are not together.
Expected result :
{
"match_key" : 44901,
"total_points" : 11.00,
},
{
"match_key" : 44901,
"total_points" : 8.00,
},
{
"match_key" : 44901,
"total_points" : 7.00,
},
{
"match_key" : 44801,
"total_points" : 10.00,
},
{
"match_key" : 44801,
"total_points" : 6.00,
},
Such that docs with same_match key are in descending order.
This is what I tried doing :
db.collection.aggregate(
[
{ $match: { match_key:{$in:[44801,45910]}}},
{ $sort: {total_points:1,match_key:1} }
]
)
This is some sql equivalent of the question ,I am trying to ask -Sort by most recent but keep together by another ID column
My main motive here is to sort the docs and keep the docs with same match_key together but in order /**