0

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?

djikay
  • 10,450
  • 8
  • 41
  • 52
HHH
  • 801
  • 1
  • 6
  • 10
  • Honestly, your schema seems pretty inefficient. You just have everything shoved in one big array. – betseyb Jul 15 '14 at 19:19

1 Answers1

0

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 } }