DEV Community

Sahil kashyap
Sahil kashyap

Posted on

MongoDB Update multiple document with multiple condition

How to use loop in update query in mongoDB?

Let’s assume a teacher has many student in his class and he is taking attendance. Default attendance is set as “Absent”.
He wants to update only those student whose who are actually present and have the same subject as his subject.
He’ll mark the student who are Present and press send

my attendance collection.Check “professorclass” has the student details and an “attendance_status”

I want to update only those student whose email_id and subject is passed from UI.

Have an array of email. We’ll use map to iterate through each email,update each document. And will use Promise.all and await to get it all done.

P.S: If anyone wants to add new info ,or if there’s a better way to update sequentially,Kindly share. I tried using updateMany but couldn’t filter data. I’m still learning :D
Full project here :https://github.com/sahilkashyap64/attendance-system-nodejs/blob/master/server/controllers/userController.js

Top comments (0)