简体   繁体   English

更新MongoDB文档列表

[英]Updating a list of MongoDB docs

I have a list of documents that i want to update in mongoDB. 我有要在mongoDB中更新的文档列表。 I send it to the API as a JSON array. 我将其作为JSON数组发送到API。

How can I update all the docs without putting the Document.update() into a loop? 如何在不将Document.update()放入循环的情况下更新所有文档?

I was looking at the $in modifier but i'm not sure how to pass in the actual data to the method. 我在看$ in修饰符,但是我不确定如何将实际数据传递给该方法。

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

You can use update query with "multi":true option: 您可以将更新查询与“ multi”:true选项一起使用:

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

Or you can use updateMany , added in MongoDB 3.2 version: 或者,您可以使用在MongoDB 3.2版本中添加的updateMany

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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