sorting - MongoDB: Sort in combination with Aggregation group -
i have collection called transaction
below documents,
/* 0 */ { "_id" : objectid("5603fad216e90d53d6795131"), "statusid" : "65c719e6727d", "relatedwith" : "65c719e67267", "status" : "a", "userid" : "100", "createdts" : isodate("2015-09-24t13:15:36.609z") } /* 1 */ { "_id" : objectid("5603fad216e90d53d6795134"), "statusid" : "65c719e6727d", "relatedwith" : "65c719e6726d", "status" : "b", "userid" : "100", "createdts" : isodate("2015-09-24t13:14:31.609z") } /* 2 */ { "_id" : objectid("5603fad216e90d53d679512e"), "statusid" : "65c719e6727d", "relatedwith" : "65c719e6726d", "status" : "c", "userid" : "100", "createdts" : isodate("2015-09-24t13:13:36.609z") } /* 3 */ { "_id" : objectid("5603fad216e90d53d6795132"), "statusid" : "65c719e6727d", "relatedwith" : "65c719e6726d", "status" : "d", "userid" : "100", "createdts" : isodate("2015-09-24t13:16:36.609z") }
when run below aggregation query without $group,
db.transaction.aggregate([ { "$match": { "userid": "100", "statusid": "65c719e6727d" } }, { "$sort": { "createdts": -1 } } ])
i result in expected sorting order. i.e sort createdts in descending order (minimal result)
/* 0 */ { "result" : [ { "_id" : objectid("5603fad216e90d53d6795132"), "createdts" : isodate("2015-09-24t13:16:36.609z") }, { "_id" : objectid("5603fad216e90d53d6795131"), "createdts" : isodate("2015-09-24t13:15:36.609z") }, { "_id" : objectid("5603fad216e90d53d6795134"), "createdts" : isodate("2015-09-24t13:14:31.609z") }, { "_id" : objectid("5603fad216e90d53d679512e"), "createdts" : isodate("2015-09-24t13:13:36.609z") } ], "ok" : 1 }
if apply below aggregation $group, resultant inversely sorted(i.e ascending sort)
db.transaction.aggregate([ { "$match": { "userid": "100", "statusid": "65c719e6727d" } }, { "$sort": { "createdts": -1 } }, { $group: { "_id": { "statusid": "$statusid", "relatedwith": "$relatedwith", "status": "$status" }, "status": {$first: "$status"}, "statusid": {$first: "$statusid"}, "relatedwith": {$first: "$relatedwith"}, "createdts": {$first: "$createdts"} } } ]);
i result in inverse order i.e. ** sort createdts in ascending order**
/* 0 */ { "result" : [ { "_id" : objectid("5603fad216e90d53d679512e"), "createdts" : isodate("2015-09-24t13:13:36.609z") }, { "_id" : objectid("5603fad216e90d53d6795134"), "createdts" : isodate("2015-09-24t13:14:31.609z") }, { "_id" : objectid("5603fad216e90d53d6795131"), "createdts" : isodate("2015-09-24t13:15:36.609z") }, { "_id" : objectid("5603fad216e90d53d6795132"), "createdts" : isodate("2015-09-24t13:16:36.609z") } ], "ok" : 1 }
where wrong ?
the $group stage doesn't insure ordering of results. see here first paragraph. if want results sorted after $group, need add $sort after $group stage.
in case, should move $sort after $group , before ask question : no, $sort won't able use index after $group before $group :-).
the internal algorithm of $group seems keep sort of ordering (reversed apparently), not count on , add $sort.
Comments
Post a Comment