Updating documents in MongoDB
It is possible to modify one or more documents of a collection. The updateOne()
and updateMany()
class methods are used respectively to modify the first document found or all of the documents found.
These two methods have similar parameters:
updateMany(conditions, update, callback)
indicates modifying the data indicated in theupdate
object on the documents specified by the indicatedconditions
. The callback function of the formcallback(err, response)
is called after the update.updateOne(conditions, update, callback)
indicates modifying the data indicated in theupdate
object on the first document found by the indicatedconditions
. The callback function of the formcallback(err, response)
is called after the update.- Only the
conditions
andupdate
parameters are mandatory in the two methods.Warning
If the callback is not present in the method, you must use the
then()
orexec()
method afterward, otherwise the update is not done.