$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
toalias
, and the fieldcell
tomobile
.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开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。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, 从MongoDB 5.0开始,当您使用带有空操作数表达式(mongod
no longer raises an error when you use an update operator like $rename
with an empty operand expression ( { }
). {}
)的更新运算符(如$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
. 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( {}, { $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
。