0

I have an update statement that use to work in 2.2.6, but fails in 2.8.x and in 3.0.1

In short: 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).

In 2.2.6 this worked fine.

In 2.8.x this updates the first element (incorrect)

In 3.0.1 this updates zero rows.

This is my test document..

    {
    "_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"
        }
    ]
}

note: cacheIds is also an array, this might be affecting it.

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

Now I've tried to use a projection to just return the section of the array I care about, and so just get it to update that section, but that doesn't seem to be a valid query/update command (it is a valid query).

Has anyone got any ideas?

jeff porter
  • 6,560
  • 13
  • 65
  • 123

2 Answers2

0

Surprisingly enough, the cacheIds array is causing this misbehavior. I have tried multiple variations of your query, but couldn't manage to make it work.

As soon as I changed cacheIds to a standard non-array type, your query worked just as intended.

I've also came across an interesting read signaling a similar behavior:

Community
  • 1
  • 1
vladzam
  • 5,462
  • 6
  • 30
  • 36
  • That link isn't the same case as here - but I do replicate this weird-seeming behavior in 2.6. – wdberkeley Mar 23 '15 at 18:29
  • @wdberkeley I considered them to be similar due to the response I got after firing the update query - WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) - it matches, but fails to update (I'm using 3.0.1) – vladzam Mar 24 '15 at 07:30
0

This is actually As Designed.

https://jira.mongodb.org/browse/SERVER-17765

I need to use the $Projection operator to limit what I get back, so to only update the section I want.

jeff porter
  • 6,560
  • 13
  • 65
  • 123