Open In App

How to Rename Fields in MongoDB

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In MongoDB, the $rename operator offers a straightforward way to rename field names within documents. This operator functions by first using $unset to delete the existing field with the old name, and then using $set to set the new name as the field name.

While the $rename operator maintains the atomicity of the document, it might not preserve the order of the fields within the document. Let’s delve deeper into how this operator is used and explore some examples.

In this article we will learn about How to Rename Fields in MongoDB by understanding various examples in detail.

What is $rename Operator?

$rename operator in MongoDB is used to rename field names in Documents. The new name should be different than the older one. The $rename operator does its task in the following manner:

  1. Uses $unset to delete the existing field of the previous name and the new name.
  2. Performs $set operation to set the new name as field name.

The $rename operator preserves the atomicity of the document but may not be able to maintain the order of the fields in the document.

Let’s set up an Environment

To understand How to Rename Fields in MongoDB we need a collection and some documents on which we will perform various queries. Here we will consider a collection called userData which contains various information as shown in the below image.

insert_userData

Rename Fields in MongoDB Examples

1. Rename a Field in a Single Document

Let’s see how we can rename a single field in a mongoDB document. For, example, let’s take the user “Ravi“. and we want to change the field name from age to user_age. To achieve this, we will be using the $rename operator.

Here we have taken the _id of document for unique identification. Once the targeted document is selected we have used the $rename operator and first specified the current name of the field and then the new name to rename. Once, we will run this MongoDB will acknowledge the query and then check for match with the existing document. If matched then the field will be renamed.

db.collection.updateOne(
{ _id: ObjectId('661a9cf9061a6a292dd14a13') },
{ $rename: { "age": "user_age" } }
)

Output:

user_data-after-renaming-age

As you can see the age field for Ravi is now changed to user_age.

2. Rename Multiple Field in a Document

Now, Let’s try to rename more than one field for a document at once. For example here we are taking the same user “Ravi” and will change the field for name to new_name and user_age to new_age.

db.collection.updateMany(
{ _id: ObjectId("document_id") },
{ $rename: { "oldFieldName": "newFieldName", "anotherOldFieldName": "anotherNewFieldName" } }
)

Here also we are doing the same as the previous example and instead of using updateOne we are using updateMany. in the $rename operator we are writing multiple fields. First write the current name of the field and then write the new name.

db.collection.updateMany(
{ _id: ObjectId('661a9cf9061a6a292dd14a13') },
{ $rename: { "name": "new_name", "user_age": "new_age" } }
)

Output:

user-Ravi-after-update

As you can see, the name field has been changed to new_name and user_age is now changed to new_age.

3. Rename Field in Multiple documents

We renamed multiple fields on a single document. But how can we change field for multiple document at once.

Here also we will define this as updateMany and for selector we have define the filter. It we want to make the changes for all the documents then write {} in place of the filter. For making changes in certain documents, we have to first write the field name in filter and then pass the values as a list. Then we can define the rename operation.

For example, let’s say we want to change the field age to new_age for Sneha and Vikram, so the query will be.

 db.userData.updateMany( { name: { $in: ["Sneha", "Vikram"] } }, { $rename: { "age": "new_age"}})

Output:

after-renaming-multiple-document

4. Rename Field in Embedded Document

As you can see that, documents available in the collection here has a embedded or sub-document in them, named as address. Inside address we have two fields named city and state. Let’s see how we can change these fields. For example we are taking the user Sneha and will change the field city to location.

Firstly we have identified the document using the objectId and then used the rename operator. Now to define a field inside subdocument we have to write as subDocumentName.FieldName.

db.userData.updateOne(
{ _id: ObjectId('661a9cf9061a6a292dd14a14') },
{ $rename: { "address.city": "address.location"} }
)

Output:

after-renaming-field-in-embedded-document

Thus, we have successfully changed the field name in an embedded document.

Conclusion

Overall, The $rename operator in MongoDB provides a convenient method to rename fields in documents. Whether you need to rename a single field in a document or multiple fields across several documents, the $rename operator offers a flexible solution. By understanding how to use this operator, you can efficiently manage and update your MongoDB collections, ensuring your data remains organized and accessible.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads