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

update with aggregation pipeline and array position not working as expected

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.8
    • Component/s: None
    • ALL

      If you attempt to update an array on a document using the dot notation the result is not correct

      Given the document

       

      {
        _id: ObjectId('1234'),
        myArray: [
         {
            someValue: false
         },
         {
            someValue: true
         }
        ]
      }

      and the operation

       

      collection.findOneAndUpdate(new ObjectId('1234'), {
         $set: {
            'myArray.0.someValue': true,
            'myArray.1.someValue': true,
         }
      })
      

      the result is as expected

       

      {
       _id: ObjectId('1234'),
       myArray: [
          {
             someValue: true
          },
          {
             someValue: true
          }
       ]
      }
      

      But if I supply the same $set operation as part of an aggregation pipeline the result is very different

       

       

      collection.findOneAndUpdate(new ObjectId('1234'), [
       $set: {
          'myArray.0.someValue': true,
          'myArray.1.someValue': true,
       }
      ]})
      

       

       

      The result is as follows

       

      {
       _id: ObjectId('1234'),
       myArray: [
          {
             someValue: false,
             0: { someValue: true},
             1: { someValue: true},
          },
          {
             someValue: true
             0: { someValue: true},
             1: { someValue: true},
          }
        ]
      }
      

       

       

       

       

            Assignee:
            edwin.zhou@mongodb.com Edwin Zhou
            Reporter:
            ah@21risk.com Andreas Hald
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: