mongodb db.collection.update() 所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15649613/
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-09 13:08:13  来源:igfitidea点击:

db.collection.update( ) all documents

mongodbrename

提问by JuanPablo

I try rename one field in all documents of a collection, with

我尝试重命名集合的所有文档中的一个字段,

db.coll.update({},{ $rename: {'originField':'newField'} });

but only one document is changed, why ?

但只更改了一个文件,为什么?

回答by Sammaye

All updates in MongoDB are, by default, singular. You must add a third option to your command to make:

默认情况下,MongoDB 中的所有更新都是单数的。您必须在命令中添加第三个选项才能使:

db.coll.update({},{ $rename: {'originField':'newField'} }, {multi:true});

If you are using 3.2 and above you can use updateMany():

如果您使用的是 3.2 及更高版本,则可以使用updateMany()

db.coll.updateMany({}, {$rename: {'originField': "newField"}})

回答by abdulH

db.collectionname.update( { "field" : "oldvalue" }, { $set:{ "field" : "newvalue" } }, { multi : true } );

回答by Josh

Since MongoDB 3.2, you can use this shorter syntax:

从 MongoDB 3.2 开始,您可以使用以下更短的语法:

db.coll.updateMany({}, {$rename: {'originField': "newField"}})

db.coll.updateMany({}, {$rename: {'originField': "newField"}})