- Reference >
- Operators >
- Update Operators >
- Field Update Operators >
- $rename
$rename¶
-
$rename¶ New in version 1.7.2.
Syntax:
{$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }The
$renameoperator updates the name of a field. The new field name must differ from the existing field name.Consider the following example:
This operation renames the field
nicknametoalias, and the fieldcelltomobile.If the document already has a field with the new field name, the
$renameoperator removes that field and renames the field with the old field name to the new field name.The
$renameoperator will expand arrays and sub-documents to find a match for field names. When renaming a field in a sub-document to another sub-document or to a regular field, the sub-document itself remains.Consider the following examples involving the sub-document of the following document:
To rename a sub-document, call the
$renameoperator with the name of the sub-document as you would any other field:This operation renames the sub-document
nmaetoname:To rename a field within a sub-document, call the
$renameoperator using the dot notation to refer to the field. Include the name of the sub-document in the new field name to ensure the field remains in the sub-document:This operation renames the sub-document field
firsttofname:To rename a field within a sub-document and move it to another sub-document, call the
$renameoperator using the dot notation to refer to the field. Include the name of the new sub-document in thenew name:This operation renames the sub-document field
lasttolnameand moves it to the sub-documentcontact:If the new field name does not include a sub-document name, the field moves out of the subdocument and becomes a regular document field.
Consider the following behavior when the specified old field name does not exist:
When renaming a single field and the existing field name refers to a non-existing field, the
$renameoperator does nothing, as in the following:This operation does nothing because there is no field named
wife.When renaming multiple fields and all of the old field names refer to non-existing fields, the
$renameoperator does nothing, as in the following:This operation does nothing because there are no fields named
wife,vice, andoffice.When renaming multiple fields and some but not all old field names refer to non-existing fields, the
$renameoperator performs the following operations:Changed in version 2.2.
- Renames the fields that exist to the specified new field names.
- Ignores the non-existing fields.
Consider the following query that renames both an existing field
mobileand a non-existing fieldwife. The field namedwifedoes not exist and$renamesets the field to a name that already existsalias.This operation renames the
mobilefield tocell, and has no other impact action occurs.Note
Before version 2.2, when renaming multiple fields and only some (but not all) old field names refer to non-existing fields:
- For the fields with the old names that do exist, the
$renameoperator renames these fields to the specified new field names. - For the fields with the old names that do not exist:
Consider the following operation that renames both the field
mobile, which exists, and the fieldwife, which does not exist. The operation tries to set the field namedwifetoalias, which is the name of an existing field:Before 2.2, the operation renames the field
mobiletocelland drops thealiasfield even though the fieldwifedoes not exist: