i already gone through below answers:- update multiple records using mongoosejs in node
Update multiple docs with different values
but in my case my query is dynamic , means i need to query dynamically based on obj, I am also not sure if forEach is a goof solution Is there any better alternative for same, as this is taking too much time in each time.
req.body.forEach(function (obj) {
myModel.find(
{
country: obj.country,
product: obj.product,
month: parseInt(obj.month),
}
).update({
$set: {
value: parseInt(obj.value),
'lastmodified': Date.now()
}
},
{ upsert: true},
function (err) {
//callback();
if (err) {
return res.status(500).send(err);
}
return res.status(200);
}
);
req.bodyan array or an object with key value pairs? array.forEach works on arrays.for...instatement iterates over the enumerable properties of an object. Maybe if you show more of your code more people might be able to help you