简体   繁体   English

如何使用 node.js 替换 mongo db 中的文档

[英]How to replace a document in mongo db using node.js

任何人都可以帮助如何使用node.js中的replaceone函数替换mongodb中的整个文档

As from the name and documentation replaceOne is used to Replaces a single document within the collection based on the filter .从名称和文档来看, replaceOne用于根据过滤器替换集合中的单个文档

The Syntax from the documentation文档中的语法

db.collection.replaceOne(
   <filter>,
   <replacement>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     hint: <document|string>                   // Available starting in 4.2.1
   }
)

To replace multiple documents,要替换多个文档,

1) Remove the desired documents with remove() Method and then, run insertMany() Method. 1) 使用remove()方法删除所需的文档,然后运行insertMany()方法。

OR或者

2) use updateMany() Method to update multiple documents matching the filter with a particular set of value. 2) 使用updateMany()方法更新多个匹配过滤器的文档,其中包含一组特定的值。

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2.1
   }
)

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

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