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

Dot notation updates with arrays can lead to nonsensical results

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Optimization
    • ALL
    • Hide
      Enterprise test> db.dropDatabase()
      Enterprise test> db.test.insert({_id: 1, arr_a: [1, 3]});
      Enterprise test> db.test.insert({_id: 2, arr_a: [1, 3]});
      Enterprise test> db.test.updateOne({_id: 1}, {$set: {"arr_a.0": 2}});    
      Enterprise test> db.test.updateOne({_id: 2}, [{$set: {"arr_a.0": 2}}]);
      Enterprise test> db.test.find()
      [
        { _id: 1, arr_a: [ 2, 3 ] },
        { _id: 2, arr_a: [ { '0': 2 }, { '0': 2 } ] }
      ]
      
      Show
      Enterprise test> db.dropDatabase() Enterprise test> db.test.insert({_id: 1, arr_a: [1, 3]}); Enterprise test> db.test.insert({_id: 2, arr_a: [1, 3]}); Enterprise test> db.test.updateOne({_id: 1}, {$set: { "arr_a.0" : 2}}); Enterprise test> db.test.updateOne({_id: 2}, [{$set: { "arr_a.0" : 2}}]); Enterprise test> db.test.find() [ { _id: 1, arr_a: [ 2, 3 ] }, { _id: 2, arr_a: [ { '0' : 2 }, { '0' : 2 } ] } ]

      The documentation isn't clear that dot notation behaviour with arrays using $set provides very different results than using $set with arrays.

      The behaviour is surprising.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            ross@mongodb.com Ross Lawley
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: