|
Here's an example from googlegroups - basically if query part includes two arrays, the LAST array used and matched will determine which position in the array being updated gets matched - this gives incorrect update result:
Sample document:
|
{
|
"Sales" : [{
|
"foo" : "Pickles",
|
"bar" : "Green",
|
"Status" : "Pending",
|
"ProposedStatus": "Closed"
|
}, {
|
"foo" : "Gravy",
|
"bar" : ObjectId("4d51fb56330a000000000cb2"),
|
"Status" : "Pending",
|
"ProposedStatus": "Closed"
|
}, {
|
"foo" : "Ointment",
|
"bar" : "Red",
|
"Status" : "Pending",
|
"ProposedStatus": "Closed"
|
}],
|
"Tags" : [{
|
"Category" : "Bird",
|
"Values" : ["Butterfly", "Sandwich", "Ring", "Clock"]
|
}]
|
}
|
|
I want to remove the "ProposedStatus" field of the third element in the Sales array, but I need to match this document by tags. I have tried this:
|
|
db.collection.update(
|
{ "Sales" : { "$elemMatch" : { "foo" : "Ointment", "bar" : "Red" } }, "Tags" : { "$elemMatch" : { "Category" : "Bird", "Values" : { "$in" : ["Butterfly"] } } } },
|
{ "$unset" : { "Sales.$.ProposedStatus" : 1 } }
|
);
|
|
...but this removes the "ProposedSatatus" field of the first element in the Sales array. My tests indicate that if I put the Tags query before the Sales query, then the right element gets updated.
|
|
db.collection.update(
|
{ "Tags" : { "$elemMatch" : { "Category" : "Bird", "Values" : { "$in" : ["Butterfly"] } } }, "Sales" : { "$elemMatch" : { "foo" : "Ointment", "bar" : "Red" } } },
|
{ "$unset" : { "Sales.$.ProposedStatus" : 1 } }
|
);
|
|
I reproduced this as well - Ross' original example attached to this bug shows that it's not $elemMatch being present twice, it's any array being involved in the query (and being in position after the elemMatch/intended positional match).
|