$addFields may add new fields in a different order in certain cases

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: 4.2 Required
    • Component/s: Aggregation Framework
    • ALL
    • Hide

       

      db.coll.drop()
      db.coll.insert({_id:1, obj: {obj: {}}})
      db.coll.aggregate([{$project: {“obj.obj.obj.str”: 1}},  {$addFields: {“obj.obj.array”:  ‘abc’, “obj.obj.obj.num”: ‘xyz’}}])  
      
      { “_id” : 1, “obj” : { “obj” : { “obj” : { “num” : “xyz” }, “array” : “abc” } } }
        
      // Now we just add a $match stage, and the order of new fields changes.
      db.coll.aggregate([{$match: {_id: 1}}, {$project: {“obj.obj.obj.str”: 1}},  {$addFields: {“obj.obj.array”:  ‘abc’, “obj.obj.obj.num”: ‘xyz’}}])  
      
      { “_id” : 1, “obj” : { “obj” : { “array” : “abc”, “obj” : { “num” : “xyz” } } }

       

       

      Show
        db.coll.drop() db.coll.insert({_id:1, obj: {obj: {}}}) db.coll.aggregate([{$project: {“obj.obj.obj.str”: 1}},  {$addFields: {“obj.obj.array”:  ‘abc’, “obj.obj.obj.num”: ‘xyz’}}])   { “_id” : 1, “obj” : { “obj” : { “obj” : { “num” : “xyz” }, “array” : “abc” } } }   // Now we just add a $match stage, and the order of new fields changes. db.coll.aggregate([{$match: {_id: 1}}, {$project: {“obj.obj.obj.str”: 1}},  {$addFields: {“obj.obj.array”:  ‘abc’, “obj.obj.obj.num”: ‘xyz’}}])   { “_id” : 1, “obj” : { “obj” : { “array” : “abc”, “obj” : { “num” : “xyz” } } }    
    • Query 2020-06-29, Query 2020-07-13, Query 2020-07-27
    • 11
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Under certain conditions $addFields may add new fields to the output document in a different order, even when applied to the same input document.

       

      Note that this behaviour was changed in 4.4 when find and aggregate projections were unified, and the order of new fields is always the same as in $addFields specification.

            Assignee:
            Anton Korshunov
            Reporter:
            Anton Korshunov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: