I am working on a project, which has over 25k users, everytime any record is created for Oppurtinity module, it executes this function, which inserts notification details for each user in mongodb,
The problem is that it uses up the cpu and doesn't let any other APIs to produce results, until this async ends it takes up time, meanwhile the server doesn't load any other data/it gives late response.
- Node-
v10.15.3 - MongoDB shell version
v4.0.17 - Server=Windows=
Memory-8GB - CPU
2.70GHz speed
I want to know, what is the problem here, is it the code, if so how can i optimize it?
async.eachSeries(users, (eachuser, next) => {
db.notifications.find({user_id:eachuser._id},function(errr,opp_noti){
if(opp_noti.length != 0){
db.notifications.update( { user_id:eachuser._id},{$push:{oppurtunity:oppurtunity_id}},function(errr,result){
})
}else{
db.notifications.insert({user_id:eachuser._id,oppurtunity:[oppurtunity_id]},function(errr,result){
})
}
})
next();
})