|
jeff_porter, as described in the documentation this behavior is expected:
the positional $ operator acts as a placeholder for the first element that matches the query document
I simplified your example for clarity, so consider the following document:
db.foo.drop()
|
db.foo.insert(
|
{
|
"array1" : [ "1", "2", "3" ],
|
"array2" : [
|
{
|
"name" : "first",
|
"state" : "NOT STARTED"
|
},
|
{
|
"name" : "second",
|
"state" : "NOT STARTED"
|
}
|
]
|
});
|
If array1 appears in the query, whatever element matches that part of the query will be considered the value for the positional operator; for example, if I use 1 it doesn't matter that the document I want to match in array2 is in position 2:
db.foo.update( { "array1" : "1" , "array2" : { "$elemMatch" :{ "name" : "second" , "state" : "NOT STARTED"}}},
|
{
|
$set: {"array2.$.state" : "ENDED"}
|
}
|
);
|
db.foo.findOne()
|
{
|
"_id" : ObjectId("5516b9dbea8545e94ca0b805"),
|
"array1" : [ "1", "2", "3" ],
|
"array2" : [
|
{
|
"name" : "first",
|
"state" : "ENDED"
|
},
|
{
|
"name" : "second",
|
"state" : "NOT STARTED"
|
}
|
]
|
}
|
Hope that helps.
|