$rename

On this page本页内容

Definition定义

$rename

The $rename operator updates the name of a field and has the following form:$rename运算符更新字段的名称,其形式如下:

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

The new field name must differ from the existing field name. 新字段名必须与现有字段名不同。To specify a <field> in an embedded document, use dot notation.要在嵌入式文档中指定<field>,请使用点表示法

Consider the following example:考虑下面的例子:

db.students.update( { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } } )

This operation renames the field nickname to alias, and the field cell to mobile.此操作将字段nickname重命名为alias,将字段cell重命名为mobile

Behavior行为

The $rename operator logically performs an $unset of both the old name and the new name, and then performs a $set operation with the new name. $rename运算符在逻辑上对旧名称和新名称执行$unset,然后对新名称执行$set操作。As such, the operation may not preserve the order of the fields in the document; i.e. the renamed field may move within the document.因此,该操作可能不会保留文档中字段的顺序;亦即,重命名的字段可能会在文档中移动。

If the document already has a field with the <newName>, the $rename operator removes that field and renames the specified <field> to <newName>.如果文档已经有一个带有<newName>的字段,$rename运算符将删除该字段,并将指定的<field>重命名为<newName>

If the field to rename does not exist in a document, $rename does nothing (i.e. no operation).如果要重命名的字段在文档中不存在,$rename不执行任何操作(即不执行任何操作)。

For fields in embedded documents, the $rename operator can rename these fields as well as move the fields in and out of embedded documents. 对于嵌入文档中的字段,$rename运算符可以重命名这些字段,并将字段移入和移出嵌入文档。$rename does not work if these fields are in array elements.如果这些字段位于数组元素中,$rename不起作用。

Examples示例

A collection students contains the following documents where a field nmae appears misspelled, i.e. should be name:students集合包含以下文档,其中字段nmae出现拼写错误,即应为name

{
  "_id": 1,
  "alias": [ "The American Cincinnatus", "The American Fabius" ],
  "mobile": "555-555-5555",
  "nmae": { "first" : "george", "last" : "washington" }
}

{
  "_id": 2,
  "alias": [ "My dearest friend" ],
  "mobile": "222-222-2222",
  "nmae": { "first" : "abigail", "last" : "adams" }
}

{
  "_id": 3,
  "alias": [ "Amazing grace" ],
  "mobile": "111-111-1111",
  "nmae": { "first" : "grace", "last" : "hopper" }
}

The examples in this section successively updates the documents in the collection.本节中的示例依次更新了集合中的文档。

Rename a Field重命名字段

To rename a field, call the $rename operator with the current name of the field and the new name:要重命名字段,请使用字段的当前名称和新名称调用$rename运算符:

db.students.updateMany( {}, { $rename: { "nmae": "name" } } )

This operation renames the field nmae to name for all documents in the collection:此操作将集合中所有文档的字段nmae重命名为name

{
  "_id": 1,
  "alias": [ "The American Cincinnatus", "The American Fabius" ],
  "mobile": "555-555-5555",
  "name": { "first" : "george", "last" : "washington" }
}

{
   "_id" : 2,
   "alias" : [ "My dearest friend" ],
   "mobile" : "222-222-2222",
   "name" : { "first" : "abigail", "last" : "adams" }
}

{ "_id" : 3,
  "alias" : [ "Amazing grace" ],
  "mobile" : "111-111-1111",
  "name" : { "first" : "grace", "last" : "hopper" } }

Rename a Field in an Embedded Document重命名嵌入文档中的字段

To rename a field in an embedded document, call the $rename operator using the dot notation to refer to the field. 要重命名嵌入文档中的字段,请使用点符号调用$rename运算符来引用该字段。If the field is to remain in the same embedded document, also use the dot notation in the new name, as in the following:如果该字段要保留在同一嵌入文档中,也要在新名称中使用点符号,如下所示:

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

This operation renames the embedded field first to fname:此操作将嵌入字段first重命名为fname

{
  "_id" : 1,
  "alias" : [ "The American Cincinnatus", "The American Fabius" ],
  "mobile" : "555-555-5555",
  "name" : { "fname" : "george", "last" : "washington" }
}

Rename a Field That Does Not Exist重命名不存在的字段

When renaming a field and the existing field name refers to a field that does not exist, the $rename operator does nothing, as in the following:重命名字段时,如果现有字段名引用的字段不存在,$rename运算符不执行任何操作,如下所示:

db.students.update( { _id: 1 }, { $rename: { 'wife': 'spouse' } } )

This operation does nothing because there is no field named wife.此操作不执行任何操作,因为没有名为wife的字段。