• Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 7.0.0, 8.0.0, 8.3.0, 9.0.0
    • Component/s: None
    • None
    • Query Optimization
    • ALL
    • Hide

      Affects versions: 9.0.8 and master (2f8d82a7, 2026-11-18). Default server parameters. Standalone mongod; Ubuntu 24.04 x86_64 and macOS arm64 give the same output.

      Repro

      db = db.getSiblingDB("test");
      db.c.drop();
      db.c.insertMany([{_id: 1, a: [{y: 5}, "s"]}, {_id: 2, a: [{y: 3}]}]);
      const ids = (docs) => EJSON.stringify(docs.map((d) => d._id));
      print("find().sort({'a.y': 1}):                        " + ids(db.c.find().sort({"a.y": 1}).toArray()));
      print("find({}, {'a.y': 1}).sort({'a.y': 1}):          " + ids(db.c.find({}, {"a.y": 1}).sort({"a.y": 1}).toArray()));
      print("aggregate [$limit, $sort]:                      " + ids(db.c.aggregate([{$limit: 5}, {$sort: {"a.y": 1}}]).toArray()));
      print("aggregate [$limit, $sort, $project {_id: 1}]:   " + ids(db.c.aggregate([{$limit: 5}, {$sort: {"a.y": 1}}, {$project: {_id: 1}}]).toArray()));
      print("sort keys ($meta sortKey):                      " + EJSON.stringify(db.c.aggregate([{$sort: {"a.y": 1}}, {$project: {k: {$meta: "sortKey"}}}]).toArray()));
      

      Output (9.0.8)

      find().sort({'a.y': 1}):                        [1,2]
      find({}, {'a.y': 1}).sort({'a.y': 1}):          [2,1]
      aggregate [$limit, $sort]:                      [1,2]
      aggregate [$limit, $sort, $project {_id: 1}]:   [2,1]
      sort keys ($meta sortKey):                      [{"_id":1,"k":[null]},{"_id":2,"k":[3]}]
      

      Master 2f8d82a7 prints the same lines.

      Show
      Affects versions: 9.0.8 and master (2f8d82a7, 2026-11-18). Default server parameters. Standalone mongod; Ubuntu 24.04 x86_64 and macOS arm64 give the same output. Repro db = db.getSiblingDB( "test" ); db.c.drop(); db.c.insertMany([{_id: 1, a: [{y: 5}, "s" ]}, {_id: 2, a: [{y: 3}]}]); const ids = (docs) => EJSON.stringify(docs.map((d) => d._id)); print( "find().sort({ 'a.y' : 1}): " + ids(db.c.find().sort({ "a.y" : 1}).toArray())); print( "find({}, { 'a.y' : 1}).sort({ 'a.y' : 1}): " + ids(db.c.find({}, { "a.y" : 1}).sort({ "a.y" : 1}).toArray())); print( "aggregate [$limit, $sort]: " + ids(db.c.aggregate([{$limit: 5}, {$sort: { "a.y" : 1}}]).toArray())); print( "aggregate [$limit, $sort, $project {_id: 1}]: " + ids(db.c.aggregate([{$limit: 5}, {$sort: { "a.y" : 1}}, {$project: {_id: 1}}]).toArray())); print( "sort keys ($meta sortKey): " + EJSON.stringify(db.c.aggregate([{$sort: { "a.y" : 1}}, {$project: {k: {$meta: "sortKey" }}}]).toArray())); Output (9.0.8) find().sort({'a.y': 1}): [1,2] find({}, {'a.y': 1}).sort({'a.y': 1}): [2,1] aggregate [$limit, $sort]: [1,2] aggregate [$limit, $sort, $project {_id: 1}]: [2,1] sort keys ($meta sortKey): [{"_id":1,"k":[null]},{"_id":2,"k":[3]}] Master 2f8d82a7 prints the same lines.
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      On a dotted sort path through an array, adding a projection of that path changes the order of the sort result. With a: [{y: 5}, "s"] in document 1 and a: [{y: 3}] in document 2, find().sort({"a.y": 1}) returns [1, 2], but find({},
      {"a.y": 1}).sort(
      {"a.y": 1})
      returns [2, 1]. In aggregation, [$limit, $sort, $project {_id: 1}] flips the order in the same way, while [$limit, $sort] does not.

      Found with Ephyra, a property-based testing tool I'm developing; this report was drafted with an LLM from the test output, and I reviewed it and reran the repro on 9.0.8 and master.

            Assignee:
            Unassigned
            Reporter:
            Alperen Keleş (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: