MongoDB $rename Operator (original) (raw)

Last Updated : 5 May, 2026

The $rename operator in MongoDB lets you change field names without altering the stored values. It’s useful for schema cleanup and consistent naming across collections, even for nested fields.

Features of $rename Operator

Here are some features of $rename operator:

Syntax

{ $rename: { : , : , ... } }

Examples of MongoDB $rename

Examples of how to use the $rename operator in MongoDB, using an Employee collection as a sample.

Screenshot-2026-02-12-163916

Example 1: Rename a Single Field

Renaming experienceYear field to experience in the employee’s document whose first name is Noah.

db.Employee.updateOne(
{ "name.first": "Noah" },
{ $rename: { "experienceYear": "experience" } }
)

**Output:

Screenshot-2026-02-12-170228

Example 2: Renaming a multiple field

Renaming the department field to unit in all the documents present in the Employee collection.

db.Employee.updateMany(
{},
{ $rename: { "department": "unit" } }
)

**Output:

Screenshot-2026-02-12-170722

Example 3: Rename a Field in an Embedded Document

Renaming personalDetails.contactInfo to personalDetails.phoneNumber field in an embedded document of the employee whose name is Ethan.

db.Employee.updateOne(
{ "name.first": "Ethan" },
{ $rename: { "personalDetails.contactInfo": "personalDetails.phoneNumber" } }
)

**Output:

rename

Example 4: Renaming a Non-Existent Field

If a field does not exist in the document, the $rename operation will not perform any action. However, we can add a field first and then rename it. Here’s how you can rename a middle name to middleName:

db.Employee.updateMany(
{ "name.middle": { $exists: false } },
{ $set: { "name.middle": "" } }
);
db.Employee.updateMany(
{},
{ $rename: { "name.middle": "name.middleName" } }
)

**Output:

Screenshot-2026-02-12-172013

Usage of MongoDB $rename Operator

Here are some usage of $rename operator: