2012-02-13 107 views
151

假設我在MongoDB中收集有5000條記錄,類似的每個包含的東西:如何重命名MongoDB中所有文檔的字段?

{ 
"occupation":"Doctor", 
"name": { 
    "first":"Jimmy", 
    "additional":"Smith" 
} 

是否有一個簡單的方法來重命名文件的所有領域的「附加」到「最後」?我在文檔中看到了$rename運算符,但我不清楚如何指定子字段。

回答

286

您可以使用:

db.foo.update({}, {$rename:{"name.additional":"name.last"}}, false, true); 

在上述方法的false, true是:{ upsert:false, multi:true }。您需要multi:true更新全部您的記錄。

或者你可以用前一種方法:

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); 

在MongoDB中3.2你也可以使用

db.students.updateMany({}, { $rename: { "oldname": "newname" } }) 

這樣做的一般語法是

db.collection.updateMany(filter, update, options) 

https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/

+0

謝謝!我編輯了我的答案來糾正這個問題。 – 2012-02-13 01:52:02

+43

只是一句話,如果你將頭撞向牆壁,因爲沒有大規模更新:'$ rename'版本的'update'方法中'false,true'是:'{upsert:false,multi:true }'。你需要'multi:true'來更新你的所有記錄。 – RickyA 2013-03-07 14:29:20

+1

,如果我得到它'upsert:true'將創建字段名稱,如果字段名稱不存在,默認爲'false'。 – IGRACH 2015-01-01 21:43:46

44

請儘量 db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true })

和閱讀:) http://docs.mongodb.org/manual/reference/operator/rename/#_S_rename

+1

請更新鏈接,看起來像新的文檔沒有說'upsert'和'multi'選項。 – 2016-03-20 15:34:03

+0

根據新的文檔,應該如下所示:db.collectionName.updateMany({},{$ rename:{'name.additional':'name.last'}}) – 1r3k 2016-10-20 09:31:20

12

要是你需要做同樣的事情mongoid:

Model.all.rename(:old_field, :new_field) 

UPDATE

有在monogoid 4.0.0中修改語法:

Model.all.rename(old_field: :new_field) 
+10

最後一個語法有變化monogoid(4.0.0)'Model.all.rename(old_field :: new_field)' – Calin 2014-07-24 06:15:04

1

任何人都可能使用此命令從集合重命名字段(由於不使用任何_id):

dbName.collectionName.update({}, {$rename:{"oldFieldName":"newFieldName"}}, false, true); 

看到FYI

0

此的NodeJS代碼只是做到這一點,如@ Felix Yan提到前一種方式似乎工作得很好,我對其他snipets有一些問題希望這有助於。

這將重命名列 「oldColumnName」 是 「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!"); 
} 
0

我使用,蒙戈3.4.0

的$更名運營商更新的名稱一個字段並具有以下形式:

{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } } 

for e。g

db.getCollection('user').update({ _id: 1 }, { $rename: { 'fname': 'FirstName', 'lname': 'LastName' } }) 

新字段名稱必須與現有字段名稱不同。要在嵌入式文檔中指定,請使用點符號。

此操作將重命名場nmae命名爲集合中的所有文檔:

db.getCollection('user').updateMany({}, { $rename: { "add": "Address" } }) 

db.getCollection('user').update({}, {$rename:{"name.first":"name.FirstName"}}, false, true); 

在上面假的方法,真正是:{UPSERT:假的,多:真}。要更新所有記錄,你需要multi:true。

重命名字段嵌入文檔

db.getCollection('user').update({ _id: 1 }, { $rename: { "name.first": "name.fname" } }) 

使用鏈接:https://docs.mongodb.com/manual/reference/operator/update/rename/