Show
db.collection.update( { myArray: [ "0" , "1" ] }, { $set: { "myArray.$[element]" : "2" } }, { arrayFilters: [ { element: "0" } ], upsert: true } )
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16836,
"errmsg" : "cannot use the part (myArray of myArray.$[element]) to traverse the element ({myArray: [ \"0\", \"1\" ]})"
}
})
Another example from docs
https://docs.mongodb.com/master/reference/operator/update/positional-filtered/#up._S_[ <identifier>]
> db.students3.insert([
... { "_id" : 1,
... "grades" : [
... { type: "quiz" , questions: [ 10, 8, 5 ] },
... { type: "quiz" , questions: [ 8, 9, 6 ] },
... { type: "hw" , questions: [ 5, 4, 3 ] },
... { type: "exam" , questions: [ 25, 10, 23, 0 ] },
... ]
... }
... ])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 1,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
>
>
> db.students3.update(
{},
{ $inc: { "grades.$[].questions.$[score]" : 2 } },
{ arrayFilters: [ { "score" : { $gte: 8 } } ], multi: true }
)
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "cannot use the part (grades of grades.$[].questions.$[score]) to traverse the element ({grades: [ { type: \"quiz\", questions: [ 10.0, 8.0, 5.0 ] }, { type: \"quiz\", questions: [ 8.0, 9.0, 6.0 ] }, { type: \"hw\", questions: [ 5.0, 4.0, 3.0 ] }, { type: \"exam\", questions: [ 25.0, 10.0, 23.0, 0.0 ] } ]})"
}
})
>