Mongo array update $elemMatch and $in operator miss behaving based on data
Solution 1:
This one is working correctly:
db.collection.update({
array: {
$in: [
"second"
]
},
objectList: {
$elemMatch: {
toMatch: {
$exists: true
}
}
}
},
{
$set: {
"objectList.$[].toChange": 10
}
},
{
multi: true
})
playground with elemMatch
Explained: The $ need to be $[] , also $arrayFilters can be used here as well but indeed it is an iteresting buggy behaviour of $
db.collection.update({
array: {
$in: [
"second"
]
}
},
{
$set: {
"objectList.$[x].toChange": 10
}
},
{
arrayFilters: [
{
"x.toMatch": {
$exists: true
}
}
],
multi: true
})
playground with arrayFilters