I am looking for a way to update many documents at once using mongoose
and return all the modified documents. I tried with setting multi:true
in update
Currently I don't think its possible in MongoDB to update multiple documents and return all the updated documents in the same query.
In Mongoose, the findOneAndUpdate()
is based on the native findAndModify()
method of MongoDB.
If you check the offical documentation of the findAndModify()
method, its states that -
The findAndModify command modifies and returns a single document.
Although the query may match multiple documents, findAndModify will only select one document to modify.
Hence, you can not update multiple documents using findAndModify
.
update()
or updateMany()
method on the other hand updates many documents with the multi
flag but it only returns the WriteResult which looks like this -
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })