如何重命名 MongoDB 中所有文档的字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9254351/
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
How can I rename a field for all documents in MongoDB?
提问by soulkphp
Assuming I have a collection in MongoDB with 5000 records, each containing something similar to:
假设我在 MongoDB 中有一个包含 5000 条记录的集合,每条记录都包含类似于以下内容的内容:
{
"occupation":"Doctor",
"name": {
"first":"Jimmy",
"additional":"Smith"
}
Is there an easy way to rename the field "additional" to "last" in all documents? I saw the $renameoperator in the documentation but I'm not really clear on how to specify a subfield.
是否有一种简单的方法可以将所有文档中的字段“附加”重命名为“最后”?我在文档中看到了$rename运算符,但我不太清楚如何指定子字段。
回答by Felix Yan
You can use:
您可以使用:
db.foo.update({}, {$rename:{"name.additional":"name.last"}}, false, true);
Or to just update the docs which contain the property:
或者只是更新包含该属性的文档:
db.foo.update({"name.additional": {$exists: true}}, {$rename:{"name.additional":"name.last"}}, false, true);
The false, true
in the method above are: { upsert:false, multi:true }
. You need the multi:true
to update allyour records.
在false, true
上述方法中有:{ upsert:false, multi:true }
。您需要multi:true
更新所有记录。
Or you can use the former way:
或者您可以使用前一种方式:
remap = function (x) {
if (x.additional){
db.foo.update({_id:x._id}, {$set:{"name.last":x.name.additional}, $unset:{"name.additional":1}});
}
}
db.foo.find().forEach(remap);
In MongoDB 3.2 you can also use
在 MongoDB 3.2 中,您还可以使用
db.students.updateMany( {}, { $rename: { "oldname": "newname" } } )
The general syntax of this is
这个的一般语法是
db.collection.updateMany(filter, update, options)
https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/
https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/
回答by Alex
please try
db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )
请尝试
db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )
and read this :) http://docs.mongodb.org/manual/reference/operator/rename/#_S_rename
并阅读这个:) http://docs.mongodb.org/manual/reference/operator/rename/#_S_rename
回答by metakungfu
If ever you need to do the same thing with mongoid:
如果你需要用 mongoid 做同样的事情:
Model.all.rename(:old_field, :new_field)
UPDATE
更新
There is change in the syntax in monogoid 4.0.0
:
中的语法有变化monogoid 4.0.0
:
Model.all.rename(old_field: :new_field)
回答by Mants
回答by d1jhoni1b
This nodejs code just do that , as @Felix Yan mentioned former way seems to work just fine , i had some issues with other snipets hope this helps.
这个nodejs代码就是这样做的,正如@Felix Yan提到的以前的方式似乎工作得很好,我对其他snipets有一些问题希望这会有所帮助。
This will rename column "oldColumnName" to be "newColumnName" of table "documents"
这会将列“oldColumnName”重命名为表“documents”的“newColumnName”
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
//var url = 'mongodb://localhost:27017/myproject';
var url = 'mongodb://myuser:[email protected]:portNumber/databasename';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
renameDBColumn(db, function() {
db.close();
});
});
//
// This function should be used for renaming a field for all documents
//
var renameDBColumn = function(db, callback) {
// Get the documents collection
console.log("renaming database column of table documents");
//use the former way:
remap = function (x) {
if (x.oldColumnName){
db.collection('documents').update({_id:x._id}, {$set:{"newColumnName":x.oldColumnName}, $unset:{"oldColumnName":1}});
}
}
db.collection('documents').find().forEach(remap);
console.log("db table documents remap successfully!");
}
回答by Swadeshi
I am using ,Mongo 3.4.0
我正在使用,Mongo 3.4.0
The $rename operator updates the name of a field and has the following form:
$rename 运算符更新字段的名称并具有以下形式:
{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
for e.g
例如
db.getCollection('user').update( { _id: 1 }, { $rename: { 'fname': 'FirstName', 'lname': 'LastName' } } )
The new field name must differ from the existing field name. To specify a in an embedded document, use dot notation.
新字段名称必须与现有字段名称不同。要在嵌入文档中指定 a,请使用点表示法。
This operation renames the field nmae to name for all documents in the collection:
此操作将字段 nmae 重命名为集合中所有文档的名称:
db.getCollection('user').updateMany( {}, { $rename: { "add": "Address" } } )
db.getCollection('user').update({}, {$rename:{"name.first":"name.FirstName"}}, false, true);
In the method above false, true are: { upsert:false, multi:true }.To update all your records, You need the multi:true.
在上面的方法中,false,true 是:{ upsert:false, multi:true }。要更新所有记录,您需要 multi:true。
Rename a Field in an Embedded Document
重命名嵌入文档中的字段
db.getCollection('user').update( { _id: 1 }, { $rename: { "name.first": "name.fname" } } )
use link : https://docs.mongodb.com/manual/reference/operator/update/rename/
使用链接:https: //docs.mongodb.com/manual/reference/operator/update/rename/
回答by Jon Kern
If you are using MongoMapper, this works:
如果您使用的是 MongoMapper,则此方法有效:
Access.collection.update( {}, { '$rename' => { 'location' => 'location_info' } }, :multi => true )