$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.updateOne(
   { _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行为

Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,更新运算符以词典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为

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不起作用。

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $rename with an empty operand expression ( { } ). 从MongoDB 5.0开始,当使用带有空操作数表达式({ })的更新运算符(如$rename)时,mongod不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。

Examples示例

Create the students collection:创建students集合:

db.students.insertMany( [
   {
     "_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 documents contain an error, nmae should be name. 文档包含错误,nmae应为nameThe examples in the following sections update 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.updateOne( { _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: { last: 'washington', fname: 'george' }
}

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.updateOne( { _id: 1 }, { $rename: { 'wife': 'spouse' } } )

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

←  $mul$set →