I’m new to mongo and have already searched through most of this forum to find the solution to my current problem, but I haven’t been able to, so I apologize in advance if my question appears repetitive or stupid.
I have provided one real-world example based on the mongo db documents and examples:
db.students.insertOne(
{ "_id" : 1,
"key":"first",
"grades" : [
{ type: "quiz", questions: [{name:"1",value:"1"}, {name:"2",value:"2"}],images:["1","2"] },
{ type: "quiz" },
{ type: "hw", questions: [ {name:"5",value:"5"}, {name:"6"} ] },
{ type: "exam", questions: [ {name:"7",value:"7"}, {name:"8",value:"8"},{name:"1",value:"1"} ],images:["1","2"] },
]
}
)
In my search, I am looking for all objects containing {“name”:“1”}
and updating just the name to "10
Here is the query:
db.getCollection("students").updateMany({"grades.questions.name":"1"},{$set:{"grades.$[].questions.$[x].name":"10"}},
{ arrayFilters: [ {"x.name": "1" } ] }
,{upsert:false})
But it complains that ‘grades.1.questions’ must exist in the document in order to apply array updates. This is why I included the first condition “grades.questions.name”:“1”
.
any help will be appreciated!