0

please can anyone tell me if $match used in aggregating , changes the order in the result :

I was trying the below query

  db.getCollection('fields').aggregate([
                    {
                        $match: {             
                               surveyFieldUniqueId: {$in:[ ObjectId("5a33"),
                                    ObjectId("5a35"),
                                    ObjectId("5a37"),
                                    ObjectId("5a39"),
                                    ObjectId("5a3b"),
                                    ObjectId("5a56")
                                    ]},
                            survey: ObjectId("5a24")
                               }
                    }
    ])

the result is in order: 5a56 , 5ba33 , 5a35 , 5a37 , 5a39 , 5a3b 

How ever if i remove the second attribute in match i.e survey i get the result in the right order as provided in the $in . I have checked the mongo docs but was unable to find much regarding this . Is there any way to maintain the order sent in the result.

user_531
  • 241
  • 4
  • 15
  • Yes the results are not guaranteed to match the order they are provided in $in. You have to provide the order in the aggregation code. More here https://stackoverflow.com/a/42293303/2683814 – s7vr Feb 13 '18 at 18:19
  • Possible duplicate of [Does MongoDB's $in clause guarantee order](https://stackoverflow.com/questions/22797768/does-mongodbs-in-clause-guarantee-order) – s7vr Feb 13 '18 at 18:20

0 Answers0