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

Mongodb update with array where select statement uses another array fails to update the document.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.6.4, 3.0.1
    • Component/s: Internal Code
    • None
    • ALL
    • Hide

      Create doc in TEST collection...

      {
          "_id" : ObjectId("5510070f7baa49e5db605c89"),
          "cacheIds" : [ 
              "456"
          ],
          "expectedSteps" : [ 
              {
                  "name" : "CacheBuildProcessor",
                  "state" : "ENDED"
              }, 
              {
                  "name" : "CacheProcessors",
                  "state" : "NOT STARTED"
              }, 
              {
                  "name" : "ProductSequential",
                  "state" : "NOT STARTED"
              }, 
              {
                  "name" : "PriceSequential",
                  "state" : "NOT STARTED"
              }, 
              {
                  "name" : "Data to CSV",
                  "state" : "NOT STARTED"
              }, 
              {
                  "name" : "CreateDeliveries",
                  "state" : "NOT STARTED"
              }
          ]
      }
      

      run query...

      db.getCollection('TEST').update(
      {
      "cacheIds" : "456" , 
      "expectedSteps" : { 
      "$elemMatch" :
       { "name" : "CreateDeliveries" , "state" : "NOT STARTED"}
       }
      },
      {
         $set: {"expectedSteps.$.state" : "ENDED"}
      }
      );
      

      Expected : 'CreateDeliveries' last element is updated from NOT STARTED to ENDED.

      Actual : 'CreateDeliveries' firstelement is updated from NOT STARTED to ENDED.

      Show
      Create doc in TEST collection... { "_id" : ObjectId( "5510070f7baa49e5db605c89" ), "cacheIds" : [ "456" ], "expectedSteps" : [ { "name" : "CacheBuildProcessor" , "state" : "ENDED" }, { "name" : "CacheProcessors" , "state" : "NOT STARTED" }, { "name" : "ProductSequential" , "state" : "NOT STARTED" }, { "name" : "PriceSequential" , "state" : "NOT STARTED" }, { "name" : "Data to CSV" , "state" : "NOT STARTED" }, { "name" : "CreateDeliveries" , "state" : "NOT STARTED" } ] } run query... db.getCollection( 'TEST' ).update( { "cacheIds" : "456" , "expectedSteps" : { "$elemMatch" : { "name" : "CreateDeliveries" , "state" : "NOT STARTED" } } }, { $set: { "expectedSteps.$.state" : "ENDED" } } ); Expected : 'CreateDeliveries' last element is updated from NOT STARTED to ENDED. Actual : 'CreateDeliveries' firstelement is updated from NOT STARTED to ENDED.

      I have an array of items, and I want to update the one with a name of 'CreateDeliveries' and a state of 'NOT STARTED'.

      The problem is this just updates the first element in the array, not the one that matches (i.e. the last element).

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            jeff_porter Jeffrey Porter
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: