Docs Menu

$rename

$rename

The $rename operator updates the name of a field.

{ $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 ドット表記.

次の例で考えてみます。

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.

When you run a $rename operation, MongoDB performs the following actions:

  • Delete the old <field> and field with <newName> from the document (using $unset).

  • Perform a $set operation with <newName>, using the value from <field>.

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.

The $rename operation might not preserve the order of the fields in the document.

MongoDB 5.0 以降、更新演算子では名前が文字列ベースのドキュメントフィールドを辞書順に処理します。数値名のフィールドは、数値順に処理されます。詳細については、「更新演算子の動作」を参照してください。

The $rename operator can move fields into and out of embedded documents.

$rename does not work on embedded documents in arrays.

  • If the document already has a field with the <newName>, the $rename operator removes that field and renames the specified <field> to <newName>.

  • If the field to rename does not exist in a document, $rename does nothing.

  • MongoDB 5.0 以降、$renameなどの更新演算子を空のオペランド式({ })と併用しても、mongod でエラーが発生しなくなりました。空の更新を使用すると変更は一切されず、oplog エントリも作成されません(操作は実行されません)。

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.

To rename a field, call the $rename operator with the current name of the field and the new name:

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:

{
"_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" }
}

To rename a field in an embedded document, call the $rename operator using the ドット表記 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:

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

This operation renames the embedded field first to fname:

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

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:

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

This operation does nothing because there is no field named wife.

以下も参照してください。