I have a schema like:
classID: String,
students: [
{
id: String,
name: String,
marks: Number,
status: String
}
]
Now, I have a requirement to update all the student records with status "pass" to have the marks as 35.
Please let me know what is the best way to do it.
I tried with:
$set: {"students.$.marks" : 0}
but it just updated the first matched record.
Then I observed that I can do it by index:
$set: {"students.0.marks" : 0},
$set: {"students.1.marks" : 0}
and I was able to update the first two records, how can I update all the records in the array or the record which satisfy the condition?
First get all records as per your criteria then do loop on records and in the loop you can update records.
students.find({makers > 35 }, function(err,data))
{ for loop { update records } }