Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-90485

$sort does not work as expected when used in conjunction with includeArrayIndex

    • Query Execution
    • Fully Compatible
    • ALL
    • v8.0, v7.0, v6.0
    • Hide

      1.db.coll.insert([{items: [1, 2, 3]}])

      2.db.coll.aggregate([
          {
            $unwind: {         path: "$items",         includeArrayIndex: "arrayIndex"       }
          },
          {
            $sort: {         "arrayIndex": -1       }
          }
        ])

      3. Output: (which is incorrect)

      [
        {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(0),     "items": 1   },
        {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(1),     "items": 2   },
        {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(2),     "items": 3   }
      ]

      ---------

      Now if I add a $set like below:

      db.coll.aggregate([
        {
          $unwind: {       path: "$items",       includeArrayIndex: "arrayIndex"     }
        },
        {
          $set: {}
        },
        {
          $sort: {       "arrayIndex": -1     }
        }
      ])

      ...then I get the correct output:

      [
        {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(2),     "items": 3   },
        {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(1),     "items": 2   },
        {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(0),     "items": 1   }
      ]

      Show
      1.db.coll.insert([{items: [1, 2, 3] }]) 2.db.coll.aggregate([     {       $unwind: {         path: "$items",         includeArrayIndex: "arrayIndex"       }     },     {       $sort: {         "arrayIndex": -1       }     }   ]) 3. Output: (which is incorrect) [   {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(0),     "items": 1   },   {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(1),     "items": 2   },   {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(2),     "items": 3   } ] --------- Now if I add a $set like below: db.coll.aggregate([   {     $unwind: {       path: "$items",       includeArrayIndex: "arrayIndex"     }   },   {     $set: {}   },   {     $sort: {       "arrayIndex": -1     }   } ]) ...then I get the correct output: [   {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(2),     "items": 3   },   {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(1),     "items": 2   },   {     "_id": ObjectId("5a934e000102030405000000"),     "arrayIndex": NumberLong(0),     "items": 1   } ]
    • QE 2024-05-27

      Hi Team,

      Attempting to $sort the results of an $unwind by the array index field (includeArrayIndex) does not sort it properly, unless we add a $set stage (it can be empty) in between. See the included reproduction steps

            Assignee:
            ivan.fefer@mongodb.com Ivan Fefer
            Reporter:
            reginald.chounoune@mongodb.com Reginald Chounoune
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: