首页 文章

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

提问于
浏览
174

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

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

有没有一种简单的方法可以在所有文档中将字段"additional"重命名为"last"?我在文档中看到了$rename运算符,但我并不清楚如何指定子字段 .

6 回答

  • 0

    您可以使用:

    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 来更新 all 您的记录 .

    或者您可以使用以前的方式:

    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

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

    这个的一般语法是

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

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

  • 45

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

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

  • 337

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

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

    UPDATE

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

    Model.all.rename(old_field: :new_field)
    
  • 0

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

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

    FYI

  • 1

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

    这会将列“oldColumnName”重命名为表“documents”的“newColumnName”

    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!");
    }
    
  • 15

    我正在使用, 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中,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" } } )
    

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

相关问题