mongodb MongoDB将字段插入集合中的所有文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33650652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:34:23  来源:igfitidea点击:

MongoDB Insert Field to all documents in a collection

mongodb

提问by Val Nolav

I am trying to add a new field into all documents in an existing collection.

我正在尝试向现有集合中的所有文档添加一个新字段。

Database name = test Collection name = teams

数据库名称 = 测试集合名称 = 团队

test.teams.update({
}
,
{
    $set: {
        "isGolden": false
    }
}
,
false,
true)

When I am trying it with MongoChef, it is giving me the following error: enter image description here

当我用 MongoChef 尝试它时,它给了我以下错误: 在此处输入图片说明

What is wrong with this? Thanks

这有什么问题?谢谢

回答by rcmgleite

If you want to update all the documents, use something like this:

如果要更新所有文档,请使用以下内容:

db.teams.update({}, {$set: {isGolden: false}}, {multi: true});

your are selecting all, setting the field isGoldento false and making this update in all documents using multi: true

您正在全选,将该字段设置isGolden为 false 并使用 multi: true 在所有文档中进行此更新

回答by Rocky Li

With MongoDB 3.2 or higher version, you can do

使用 MongoDB 3.2 或更高版本,您可以

db.teams.updateMany({}, {$set: {isGolden: false}});

Here's the doc for updateMany function db.collection.updateMany()

这是 updateMany 函数db.collection.updateMany()的文档

回答by Parth Chauhan

With Studio 3T you were writing queries in JSON mode in which it wants JSON data but you are not writing JSON Query. You must go to InteliShell mode in which your query will execute in format that you were writing.

使用 Studio 3T,您以 JSON 模式编写查询,其中它需要 JSON 数据,但您没有编写 JSON 查询。您必须进入 InteliShell 模式,在该模式下,您的查询将以您编写的格式执行。

Studio 3T InteliShell

Studio 3T InteliShell