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

时间:2022-01-07 20:17:54

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 $rename operator in the documentation but I'm not really clear on how to specify a subfield.

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

6 个解决方案

#1


303  

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 all your records.

上面方法中的false, true是:{upsert:false, 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/

#2


45  

please try db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )

请试着db.collectionName。更新({},{$rename: {'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

#3


13  

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)

#4


1  

Anyone could potentially use this command to rename a field from the collection (By not using any _id):

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

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

see FYI

看到通知你

#5


0  

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提到的,以前的方法似乎很好用,我和其他狙击兵有一些问题,希望这能有所帮助。

This will rename column "oldColumnName" to be "newColumnName" of table "documents"

这将把列“oldColumnName”重命名为表“文档”的“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!");
}

#6


0  

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}。要更新所有的记录,您需要多: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/

#1


303  

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 all your records.

上面方法中的false, true是:{upsert:false, 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/

#2


45  

please try db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )

请试着db.collectionName。更新({},{$rename: {'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

#3


13  

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)

#4


1  

Anyone could potentially use this command to rename a field from the collection (By not using any _id):

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

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

see FYI

看到通知你

#5


0  

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提到的,以前的方法似乎很好用,我和其他狙击兵有一些问题,希望这能有所帮助。

This will rename column "oldColumnName" to be "newColumnName" of table "documents"

这将把列“oldColumnName”重命名为表“文档”的“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!");
}

#6


0  

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}。要更新所有的记录,您需要多: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/