【问题标题】:Is it possible to add a field to every document in a collection in MongoDB Compass?是否可以在 MongoDB Compass 的集合中为每个文档添加一个字段?
【发布时间】:2020-03-21 02:37:29
【问题描述】:

是否可以在 MongoDB Compass 的集合中为每个文档添加一个字段?或者这是必须在 shell 中完成的事情?

【问题讨论】:

    标签: mongodb mongodb-compass


    【解决方案1】:

    Compass 中没有使用新字段更新所有文档的选项; Compass 的“文档选项卡”具有修改文档字段添加新字段的选项(modify 一次一个文档)。

    这可以通过 mongo shell 或您最喜欢的编程语言来完成。

    在 shell 中,要使用新字段更新集合中的所有文档,请使用 db.collection.updateMany() 方法。例如,db.test.updateMany( { }, { $set: { new_field: "initial value" } } )

    一旦文件被更新,就可以从 Compass 中查看;只需在 Documents 视图/选项卡中执行 refresh

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多