MongoDB $rename operator
What is the $rename operator in MongoDB?
MongoDB provides a variety of field update operators to update the values of the fields. The $rename operator is one of those operators. The $rename operator is used to change the name of a field. The new name of the field must be different from the old name of the field.
Important points:
- The $rename operator performs the $unset operation on both the old and new names before performing the $set operation on the new name.
- It can also work with arrays or nested documents.
- According to your requirements you can use this operator in findAndModify(), like(), update(), etc.
- If the given field does not exist to rename in the document, the $rename operator does nothing.
Syntax of the $rename operator:
Examples:
In the following examples, we are working with:
>db.employees.find().pretty() { "_id" : 1, "employee_name" : "Tin", "father_name" : "Thor", "department" : "Tester", "address" : "London", "joinning" : 2020, "phone_no" : 9856321478, "gender" : "Male", "age" : 20, "salary" : 10000 } { "_id" : 2, "employee_name" : "John", "father_name" : "Mick", "department" : "Tester", "address" : "NewYork", "joining" : 2015, "phone_no" : 7896541478, "gender" : "Male", "age" : 23, "salary" : 20000, "report_lastDate" : ISODate("2021-08-05T00:00:00Z") } { "_id" : 3, "employee_name" : "Ammy john", "father_name" : "John", "department" : "Software developer", "address" : "London", "joining" : 2019, "phone_no" : 7985631478, "gender" : "Female", "age" : 26, "salary" : 15000 } { "_id" : 4, "employee_name" : "Reeza", "father_name" : "Reeza Hendricks", "department" : "Tester", "address" : "USA", "joining" : 2020, "phone_no" : 7412563278, "gender" : "Male", "age" : 22, "salary" : 20000 } { "_id" : 5, "employee_name" : "John Lewis", "father_name" : "Lewis", "department" : "Software developer", "address" : "London", "joining" : 2015, "phone_no" : 9632587418, "gender" : "Male", "age" : 25, "salary" : 25000, } { "_id" : 6, "employee_name" : "Temba", "father_name" : "George", "department" : "Tester", "address" : "NewYork", "joining" : 2018, "phone_no" : 8965247418, "gender" : "Male", "age" : 24, "salary" : { "first_month" : 15000, "second_month" : 18000, "bonus" : 2000 } }
Example 1: Renaming a single field
In this example, we’re renaming the name of the “joining” field to “joinYear” in the employee’s document whose department is Tester.
Output:
{ "_id" : 1, "employee_name" : "Tin", "father_name" : "Thor", "department" : "Tester", "address" : "London", "joinYear" : 2020, "phone_no" : 9856321478, "gender" : "Male", "age" : 20, "salary" : 10000 } { "_id" : 2, "employee_name" : "John", "father_name" : "Mick", "department" : "Tester", "address" : "NewYork", "joinYear" : 2015, "phone_no" : 7896541478, "gender" : "Male", "age" : 23, "salary" : 20000, "report_lastDate" : ISODate("2021-08-05T00:00:00Z") } { "_id" : 4, "employee_name" : "Reeza", "father_name" : "Reeza Hendricks", "department" : "Tester", "address" : "USA", "joinYear" : 2020, "phone_no" : 7412563278, "gender" : "Male", "age" : 22, "salary" : 20000 } { "_id" : 6, "employee_name" : "Temba", "father_name" : "George", "department" : "Tester", "address" : "NewYork", "joinYear" : 2018, "phone_no" : 8965247418, "gender" : "Male", "age" : 24, "salary" : { "first_month" : 15000, "second_month" : 18000, "bonus" : 2000 } }
Example 2: Renaming multiple fields in the documents
In this example, we’re renaming the name of the “phone_no” field to “contact_no” in all the documents present in the employees collection.
Output:
{ "_id" : 1, "employee_name" : "Tin", "father_name" : "Thor", "department" : "Tester", "address" : "London", "joining" : 2020, "contact_no" : 9856321478, "gender" : "Male", "age" : 20, "salary" : 10000 } { "_id" : 2, "employee_name" : "John", "father_name" : "Mick", "department" : "Tester", "address" : "NewYork", "joining" : 2015, "contact_no" : 7896541478, "gender" : "Male", "age" : 23, "salary" : 20000, "report_lastDate" : ISODate("2021-08-05T00:00:00Z") } { "_id" : 3, "employee_name" : "Ammy john", "father_name" : "John", "department" : "Software developer", "address" : "London", "joining" : 2019, "contact_no" : 7985631478, "gender" : "Female", "age" : 26, "salary" : 15000 } { "_id" : 4, "employee_name" : "Reeza", "father_name" : "Reeza Hendricks", "department" : "Tester", "address" : "USA", "joining" : 2020, "contact_no" : 7412563278, "gender" : "Male", "age" : 22, "salary" : 20000 } { "_id" : 5, "employee_name" : "John Lewis", "father_name" : "Lewis", "department" : "Software developer", "address" : "London", "joining" : 2015, "contact_no" : 9632587418, "gender" : "Male", "age" : 25, "salary" : 25000, } { "_id" : 6, "employee_name" : "Temba", "father_name" : "George", "department" : "Tester", "address" : "NewYork", "joining" : 2018, "contact_no" : 8965247418, "gender" : "Male", "age" : 24, "salary" : { "first_month" : 15000, "second_month" : 18000, "bonus" : 2000 } }
Example 3: Renaming a field in nested documents
In this example, we’re renaming the name of the “salary.first_month” field to “salary.month” in the employee document whose employee_name is Temba.
Output:
{ "_id" : 6, "employee_name" : "Temba", "father_name" : "George", "department" : "Tester", "address" : "NewYork", "joining" : 2018, "phone_no" : 8965247418, "gender" : "Male", "age" : 24, "salary" : { "month" : 15000, "second_month" : 18000, "bonus" : 2000 } }