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

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

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • 4.2 Required
    • 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

    Description

      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.

      Attachments

        Activity

          People

            anton.korshunov@mongodb.com Anton Korshunov
            anton.korshunov@mongodb.com Anton Korshunov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: