Update multiple documents and return all updated documents

前端 未结 1 1359
后悔当初
后悔当初 2021-02-12 22:08

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

1条回答
  •  误落风尘
    2021-02-12 22:34

    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 })
    

    0 讨论(0)
提交回复
热议问题