如何为MongoDB中的所有文档重命名字段?

时间:2012-02-13 01:04:00

标签: mongodb

假设我在MongoDB中有一个包含5000条记录的集合,每条记录包含类似于:

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

是否有一种简单的方法可以将所有文档中的“附加”字段重命名为“最后”?我在文档中看到了$rename运算符,但我并不清楚如何指定子字段。

7 个答案:

答案 0 :(得分:366)

您可以使用:

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

或者只是更新包含该属性的文档:

db.foo.update({"name.additional": {$exists: true}}, {$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/

答案 1 :(得分:46)

请尝试 db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )

并阅读此:) http://docs.mongodb.org/manual/reference/operator/rename/#_S_rename

答案 2 :(得分:15)

如果你需要用mongoid做同样的事情:

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

<强>更新

monogoid 4.0.0中的语法发生了变化:

Model.all.rename(old_field: :new_field)

答案 3 :(得分:1)

任何人都可以使用此命令重命名集合中的字段(不使用任何_id):

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

请参阅FYI

答案 4 :(得分:0)

这个nodejs代码只是这样做,因为@Felix Yan提到以前的方式似乎工作得很好,我有一些问题与其他snipets希望这有帮助。

这将重命名列&#34; oldColumnName&#34;成为&#34; newColumnName&#34;表&#34;文件&#34;

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
//var url = 'mongodb://localhost:27017/myproject';
var url = 'mongodb://myuser:mypwd@myserver.cloud.com: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!");
}

答案 5 :(得分:0)

我正在使用 Mongo 3.4.0

$ rename运算符更新字段的名称,其格式如下:

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

例如

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

新字段名称必须与现有字段名称不同。要在嵌入文档中指定a,请使用点表示法。

此操作将字段nmae重命名为集合中所有文档的名称:

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

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

在上面的方法中,false为:{upsert:false,multi:true}。要更新所有记录,需要multi:true。

重命名嵌入文档中的字段

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

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

答案 6 :(得分:0)

如果您使用的是MongoMapper,则可以:

Access.collection.update( {}, { '$rename' => { 'location' => 'location_info' } }, :multi => true )
相关问题