return User.find({totalSavings: {$gt:99}},{totalSavings:1, _id:1}).then((result, err)=>{
if (result!==null){
for (let count = Object.keys(result).length-1; count >= 0; count--) {
return User.findByIdAndUpdate(result[count]._id, {$push:{savingsInterests: [{amount: result[count].totalSavings*0.002083}]}
}).then((updatedInfo, err)=> {
res.send(updatedInfo)
}).catch(err => res.send(err+"updating SavingsInterests"))
}
res.send("sucessful")
}
else{
res.send(false)
}
}).catch(err => res.send(err+"."))
This block of code only update the result[0]._id because once it reaches res.send, it won't loop. I tried replacing res.send with res.write but it results to continued loop. I tried another code using updateMany without loop but it returns a cast object error.
return User.updateMany({totalSavings: {$gt:99}},
{$push:{savingsInterests:{amount: {$mul: {totalSavings: 0.002083}}}]
}}).then((result, err)=>{
I have a similar one that is working fine but it doesn't update an array of object.
return User.updateMany({totalSavings: {$gt:99}}, {$mul: {totalSavings: 1.002083}}).then((updatedInterest, err) => {