-
Type: Question
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 4.3.2
-
Component/s: Aggregation Framework
-
None
data = [ {"name": a, dept: "IT", score: 150}, {"name": b, dept: "IT", score: 100}, {"name": c, dept: "Sales", score: 260}, {"name": d, dept: "Sales", score: 200} ]
Now we run following aggregation
db.serviceoffering.aggregate([ {$sort: {score: 1}}, {$group: {_id: {dept: "$dept"}, user: {$push: "$$ROOT"}}} ])
Now after $sort pipeline the result would be following:
data = [ {"name": b, dept: "IT", score: 100}, {"name": a, dept: "IT", score: 150}, {"name": d, dept: "Sales", score: 200}, {"name": c, dept: "Sales", score: 260} ]
But after $group pipeline result becomes:
{ { _id: IT, user:[ {"name": d, dept: "Sales", score: 200}, {"name": c, dept: "Sales", score: 260} ] }, { _id: Sales, user: [ {"name": b, dept: "IT", score: 100}, {"name": a, dept: "IT", score: 150} ] } }
If you observe the result data has been taken from bottom and hence even after sort d,c are on top in the final result.
- duplicates
-
SERVER-24799 $group aggregation command should maintain document order
- Closed