Definition定义
$renameThe$renameoperator updates the name of a field.$rename运算符用于更新字段的名称。
Syntax语法
{ $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' } }
)
The preceding operation renames the 在nickname field to alias and the cell field to mobile in a document where _id is 1._id为1的文档中,前面的操作将nickname字段重命名为alias,将cell字段重命名为mobile。
Behavior行为
When you run a 当你运行$rename operation, MongoDB performs the following actions:$rename操作时,MongoDB会执行以下操作:
Delete the old从文档中删除旧的<field>and field with<newName>from the document (using$unset).<field>和带有<newName>的字段(使用$unset)。Perform a使用$setoperation with<newName>, using the value from<field>.<field>中的值,使用<newName>执行$set操作。
Atomicity原子性
Each document matched by an update command is updated in an individual operation. Update operations (like 由更新命令匹配的每个文档都会在单独的操作中更新。更新操作(如$rename) only guarantee atomicity on a single-document level.$rename)仅保证单个文档级别的原子性。
Field Order场序
The $rename operation might not preserve the order of the fields in the document.$rename操作可能无法保留文档中字段的顺序。
Update Processing Order更新处理订单
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.具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
Rename Embedded Document Fields重命名嵌入式文档字段
The $rename operator can move fields into and out of embedded documents.$rename运算符可以将字段移入和移出嵌入式文档。
$rename does not work on embedded documents in arrays.不适用于数组中的嵌入式文档。
Other Considerations其他考虑
If the document already has a field with the如果文档中已经有一个<newName>, the$renameoperator 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,如果文档中不存在要重命名的字段,$renamedoes nothing.$rename将不执行任何操作。Starting in MongoDB 5.0,从MongoDB 5.0开始,当您使用带有空操作数表达式(mongodno longer raises an error when you use an update operator like$renamewith an empty operand expression ({ }). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).{ })的$rename等更新运算符时,mongod不再引发错误。空更新不会导致任何更改,也不会创建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. The examples in the following sections update the documents in the collection.nmae应该是name。以下部分中的示例更新了集合中的文档。
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(
{ "nmae": { $ne: null } },
{ $rename: { "nmae": "name" } }
)
This operation checks for documents where the 此操作检查nmae field is not null and updates those documents to rename the nmae field to name:nmae字段不为空的文档,并更新这些文档以将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. If the field is to remain in the same embedded document, also use the dot notation in the new name, as in the following:$rename运算符来引用该字段。如果字段要保留在同一个嵌入式文档中,请在新名称中使用点符号,如下所示:
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的字段。