如何在 mongodb 中全局更新字段值

How to update a field value globally in mongodb

有没有更简单的方法来全局更新 mongodb 中的字段文本?

我没有 mongodb 知识,但有些事情是这样的,

更新"all collections that has field1" set field1=some where field1=1234

请查看 mysql 使用数据库转储的解决方案: 类似于 Find and replace in entire mysql database

如果以上不可能,那么编写一次性脚本来迁移 mongodb 中的数据的最佳方法是什么?这是在 linux 环境中。

要更新同一集合中的多个文档,请在进行更新时使用 multi:true 选项:

db.collection.update(
   { "field1": 1234 },
   { "$set": { "field1" : somevalue } },
   { multi: true }
);

MongoDB 没有一次影响多个集合的命令,因此您必须对每个集合单独执行此命令。当您想在 shell 中执行此操作时,您可以使用如下脚本分别对每个集合执行命令:

 db.getCollectionNames().forEach( function(name) {
      db[name].update(
             { "field1": 1234 },
             { "$set": { "field1" : somevalue } },
             { multi: true }
      );
 });