I have seen how mongoose updateMany() can be used to update some fields easily. My question is can updateMany() also be used when you are to manipulate data before updating the database. I have some lines of code here below. Can updateMany be used to implement something similar to this?
try {
const userWithObjectLocation = await User.find({zipcode: { $exists: true }});
userWithObjectLocation.map(async user =>{
const locationObject = zipcodes.lookup(user.zipcode);
if(locationObject){
const location = `${locationObject.city}, ${locationObject.state}`
await User.findByIdAndUpdate(user._id, {location})
}
})
return res.status(200).json({message: "Location has been updated successfully"});
} catch (err) {
console.log(err);
return res.status(500).json(err);
}