简体   繁体   中英

Updating a list of MongoDB docs

I have a list of documents that i want to update in mongoDB. I send it to the API as a JSON array.

How can I update all the docs without putting the Document.update() into a loop?

I was looking at the $in modifier but i'm not sure how to pass in the actual data to the method.

    var docs = req.body
    Card.update(
        {_id: {$in: docs}},
        {whatgoeshere?}
    )

You can use update query with "multi":true option:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>
   }
)

Or you can use updateMany , added in MongoDB 3.2 version:

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>
   }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM