I have this data:
{
"_id" : ObjectId("5956795e1729955bbed30e68"),
"messages" : [
{
"text" : "Test 1"
},
{
"text" : "Test 2",
"link" : {
"type" : "Calendar"
}
}
]
}
and I run this query:
db.collection('user').update(
{
'_id': ObjectID.createFromHexString('5956795e1729955bbed30e68'),
'messages.isLocked': { $exists: false }
},
{
$set: { 'messages.$.isLocked': true }
}
);
I expect to have all messages with "isLocked: true" but I receive:
MongoError: The positional operator did not find the match needed from the query. Unexpanded update: messages.$.isLocked
Other input: if I run the query 'messages.text': { $exists: true }
instead of 'messages.isLocked': { $exists: false }
, Mongo update ONLY the first element -.-
What am I doing wrong about this feature?
UPDATE: I using MongoDB 3.4.5, so I think that it's possible to achieve:
It works only with the first occurence: https://docs.mongodb.com/manual/reference/operator/update/positional/