The following is a modified example from https://docs.mongodb.com/manual/reference/operator/update/positional/#examples
db.students.insert([
{ "_id" : 1, "grades" : [ 85, 80, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
])
db.students.updateOne(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
{ multi: true }
)'
I expect {multi: true}
to update multiple documents that match the selector criteria.
As a matter of fact, the following doesn't seem to work either:
db.students.insert([
{ "_id" : 1, "grades" : [ 85, 82, 82 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 80, 100, 90 ] }
])
db.students.updateOne(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
{ multi: true }
)'
I expect the third document to be updated to {"_id" : 3, "grades" : [82, 100, 90]}
Your query clause is
{ _id: 1, grades: 80 }
butid:1
hasgrades : [ 85, 82, 82 ]
. The third document will not be updated because it has_id:3
which does not match your query clause.Pro tip: before running
update
execute afind
to get a preview of what may be updated*.*
find
will return all matches whileupdateOne
will update at most 1 document but at least you'll know your query clause works.