are there any drawbacks to using findByIdAndUpdate and doing it manually. I noticed that findByIdAndUpdate drops my mongo connection, and read that you should try to keep connection open, only close when you close your app. Is this the case, and if so, is there a config setting that I am not seeing for findByIdAndUpdate, that keep the connection on?
updateItemById: function(id, updateObj, options, callback){
//OPTION 1
Badge.findById(id, null , function(err, doc){
doc.update(updateObj, function(err, numberAffected, raw){
if (err) return handleError(err);
Badge.findById(id, null , function(err, doc){
callback(doc);
});
});
});
//OPTION 2
Badge.findByIdAndUpdate(id, updateObj, options, function(err, data){
callback(doc);
});
}
findByIdAndUpdate should be faster (as long as the driver and mongoose are correctly programmed), because:
With Option 1:
With Option 2:
As you can see, the second options does require less steps and therefore should be faster.
Bonus: The code is more readable.