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”
{ | |
"_id": { | |
"$oid": "5e68c7eaa0887971ea6ef54c" | |
}, | |
"professorclass": [ | |
{ | |
"_id": { | |
"$oid": "5e678d257cd5a55f2188f02f" | |
}, | |
"attendance_status": "SES", | |
"role": "student", | |
"userType": "StudentUser", | |
"email": "student1@gmail.com", | |
"subject": "chem" | |
}, | |
{ | |
"_id": { | |
"$oid": "5e679f16ef640474e3fdb572" | |
}, | |
"attendance_status": "SES", | |
"role": "student", | |
"userType": "StudentUser", | |
"email": "student2@gmail.com", | |
"subject": "chem" | |
} | |
], | |
"role": "teacher", | |
"userType": "TeacherUser", | |
"email": "teacher1@gmail.com", | |
"subject": "chem", | |
"attendate": { | |
"$date": { | |
"$numberLong": "1583925226000" | |
} | |
}, | |
"__v": { | |
"$numberInt": "0" | |
} | |
} |
I want to update only those student whose email_id and subject is passed from UI.
//student whose attendance status will change | |
var someData = [ | |
{email: "student2@gmail.com", subject: "chem"}, | |
{email: "student1@gmail.com", subject: "chem"}, | |
]; | |
var sub=someData[0].subject; //chem | |
//function to extract the emails and saving them in array | |
function listEmails(email) { | |
let product_names = []; | |
for (let i=0; i<email.length; i+=1) { | |
product_names.push(email[i].email); | |
} | |
return product_names; | |
} | |
//update those student whose email & subject was passed | |
const files = await listEmails(someData);//get all the emails | |
//Use map iterate through all the mail and update sequentially | |
//condition to check before update if they are student ,subject,email match the parameters passed | |
//set those student attendance_status=present | |
//promise all is where the magic is happening | |
await Promise.all(files.map(async (file) => { | |
const updatestatus=await Attendance.update( | |
{ }, | |
{ $set: { "professorclass.$[elem].attendance_status" : "Present" } }, | |
{ | |
multi: true, | |
arrayFilters: [ { "elem.role": { $eq: "student" }, "elem.subject": { $eq: sub}, "elem.email": { $eq: file}} ], multi: true | |
} | |
); | |
console.log(updatestatus); // it will return if files got modified or not | |
console.log(file);//to check which mail | |
})); |
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)