D
D
deadkEEper12016-04-27 13:05:05
MongoDB
deadkEEper1, 2016-04-27 13:05:05

How to update a mongo model so that an already updated model is returned?

There is a mongo collection of users.
I know that to get updated data you need to pass options {new: true}

UserModel.update({_id: id}, {name: 'newName'}, {new: true}, function(err, userModel){
   ////userModel будут с обновленными свойствами
})

but my problem is a bit different. You need to do this:
UserModel.findById(id, function(err, userModel){
   userModel.update({name: 'newName'},{new: true},function(err, result){
           //возвращаеться результат операции,  а нужна уже обновленная модель
})
})

the fact is that I use async.waterfall, I get the model in the first function, and I pass it along the chain from the callback to the callback, and only in the last one I do userModel.update

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question