MongoDB – Rename Operator ($rename)
Last Updated :
21 Jun, 2024
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure.
This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in detail.
MongoDB $rename
MongoDB provides different types of field update operators to update the values of the fields of the documents and the $rename operator is one of them.
This operator is used to update the names of the fields with new names. The new name of the field should be different from the existing name of the field.
- $rename operator logically first performs an $unset operation on both the old name and the new name and then performs an $set operation with the new name.
- If the document contains the field with the new name then this operator will remove that field and rename the specified field to the new name.
- This operator can also work with embedded/nested documents or arrays. In embedded/nested documents, this operator can rename the specified field as well as move the fields in and out of the embedded documents
- We can use this operator in methods like update(), findAndModify() etc. according to our requirements.
- If the specified field for renaming is not available then this operator will do nothing.
Syntax:Â
{$rename: {<field1>:<newName1>, <field2>:<newName2>, ... } }
Here, newName is the new name and it must be different from the existing name of the field. To specify field in embedded/nested documents with the help of dot notation.Â
Examples of MongoDB $rename
In the following examples, we are working with:
Database: GeeksforGeeksÂ
Collection: EmployeeÂ
Document: three documents that contain the details of the employees in the form of field-value pairs.
Output:

Example 1: Rename a Field
In this example, we are renaming the name of experienceYear field to experience in the employeeâs document whose first name is Amu.Â
db.Employee.update({"name.first": "Amu"},
{$rename: {"experienceYear":"experience"}})
Output:

Example 2: Renaming a multiple field
In this example, we are renaming the name of the department field to unit in all the documents present in the Employee collection.Â
db.Employee.updateMany({}, {$rename: {"department":"unit"}})
Output:

Example 3: Rename a Field in an Embedded Document
In this example, we are renaming the name of personalDetails.contactInfo to personalDetails.phoneNumber field in an embedded/nested document of the employee whose name is Sumit.Â
db.Employee.update({"name.first": "Sumit"},
{$rename: {"personalDetails.contactInfo":"personalDetails.phoneNumber"}})
Output:
Example 4: Rename a Field That Does Not Exist
db.Employee.updateMany({ "name.middle": { $exists: false } }, { $set: { "name.middle": "" } }); db.Employee.updateMany({}, { $rename: { "name.middle": "name.middleName" } });
Output:
[
{
"_id": ObjectId("5e9823b792e6dfa3fc48ddda"),
"name": {
"first": "Om",
"last": "Goyal",
"middleName": ""
},
"personalDetails": {
"age": 23,
"contactInfo": 222773330
},
"department": "Development",
"salary": 30000,
"experienceYear": 3,
"points": [1, 2, 41],
"joiningDate": ISODate("2020-04-16T09:26:23.669Z")
},
{
"_id": ObjectId("5e9823b792e6dfa3fc48dddb"),
"name": {
"first": "Sumit",
"last": "Singh",
"middleName": ""
},
"personalDetails": {
"age": 24,
"contactInfo": 222993330
},
"department": "HR",
"salary": 35000,
"experienceYear": 2,
"points": [5, 7],
"joiningDate": ISODate("2020-04-16T09:57:20.343Z")
},
{
"_id": ObjectId("5e9823b792e6dfa3fc48dddc"),
"name": {
"first": "Amu",
"last": "Goyal",
"middleName": ""
},
"personalDetails": {
"age": 24,
"contactInfo": 228833330
},
"department": "HR",
"salary": 35000,
"experienceYear": 1,
"points": [7, 6],
"joiningDate": ISODate("2020-04-16T10:12:34.116Z")
}
]
Explanation
- Step 1: Ensures all documents have a
name.middle field with a default empty string if it didn’t already exist.
- Step 2: Renames the
name.middle field to name.middleName in all documents.
This output reflects the updated collection where the name.middle field has been successfully renamed to name.middleName.
Conclusion
The $rename operator in MongoDB is a versatile tool for updating field names within documents, accommodating embedded/nested structures and arrays. This operator’s ability to handle both renaming and conditional operations makes it useful for maintaining and organizing document structures. By utilizing methods such as update() and findAndModify() developers can efficiently manage field names and ensure data integrity within their MongoDB collections.
FAQs on MongoDB $rename
Is it possible for the $rename operator to rename multiple fields simultaneously?
Yes, the $rename operator can rename multiple fields simultaneously by specifying each field and its new name within the same command.
What happens if the new field name already exists in the document?
If the new field name already exists, the $rename operator will remove the existing field before renaming the specified field to the new name.
Can the $rename operator be used on embedded documents or arrays?
Yes, the $rename operator can be used on embedded/nested documents or arrays, allowing for renaming fields within these structures or moving fields in and out of them.