Suppose you have created a schema for storing data and stored some data based on it. Later any time you want to change the field name, well, you can easily do it manually in the schema, but you cannot edit the large amount of stored data field names manually and you also can’t leave them, since updated fields and the already present fields must have the same name. So you need an operator that can rename the existing fields for you. Here comes the $rename operator.
In this article, we will learn about the $rename operator, its syntax and see how to use it to rename the field names in MongoDB. Let’s get started.
MongoDB $rename Operator
The $rename operator in MongoDB allows us to rename a field’s name. It takes an expression containing the old field name as a key and the field name you want to change as a value. You can pass such multiple key-value pairs which makes it easy to rename multiple field names at a time.
Syntax:
Following is the syntax for using the $rename operator in MongoDB.
db.collection.updateMany(
{},
{ $rename: { fieldName1: newName1, fieldName2: newName2, ... } }
);
Here:
- “collection” is the collection name where we are looking for the documents.
- “{}” is used to specify that the operation will be performed on all documents in the collection.
- “updateMany()” is the method we are using to update the documents.
- “{ fieldName1: newName1, fieldName2: newName2, … }” is the field name pairs to be renamed. Each key-value pair represents the old field name (fieldNameN) and the corresponding new field name (newNameN).
Note:
- Make sure the new name is different from the old existing field name.
- To select an embedded field, you can use the dot notation.
Important:
If another field in the document already contains the specified newName, then the operator will remove that field and rename the specified fieldName with newName.
Examples of Renaming Fields in MongoDB Using $rename Operator
Let us now look at some practical examples of using the $rename operator in MongoDB.
Setup:
1. Start the MongoDB shell.
2. Execute the following commands to list all the available databases & move into one of them.
show dbs
use shop
Here as an example, we are using our “shop” database which contains a collection named “shoes”.
3. Let’s pretty print the documents in the “shoes” collection to see what our sample collection looks like.
> db.shoes.find().pretty()
{
"_id" : ObjectId("616b18a556e71503e4a0334d"),
"name" : "Mariana Grider Boots",
"price" : 23000,
"size" : 7,
"style" : "Slingbacks",
"onSale" : false,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 40
}
}
{
"_id" : ObjectId("616b18a556e71503e4a0334e"),
"name" : "Purple Shimmery Heels",
"price" : 13380,
"size" : 6,
"style" : "Peep Toes",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
},
"itemNumber" : 0
}
{
"_id" : ObjectId("616b18a556e71503e4a0334f"),
"name" : "Zac Porter Straps",
"price" : 19760,
"size" : 8,
"style" : "Ankle Strap",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
}
}
{
"_id" : ObjectId("616b18a556e71503e4a03350"),
"name" : "Valerina's Red Carpet Toes",
"price" : 26000,
"size" : 9,
"style" : "Scarpin",
"onSale" : false,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
}
}
{
"_id" : ObjectId("616b18a556e71503e4a03351"),
"name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
"price" : 10780,
"size" : 7,
"style" : "Wedge Booties",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
}
}
{
"_id" : ObjectId("6171bbf968378cae00d83aca"),
"name" : "Caroline Wielder Shoes",
"price" : NumberDecimal("14999.985"),
"size" : 5,
"style" : "Wedge Booties",
"onSale" : true,
"moreDetails" : {
"qtyAvlbl" : 10
}
}
{
"_id" : ObjectId("6171bbf968378cae00d83acb"),
"name" : "Marilyn Jaworski Sky Highs",
"price" : NumberDecimal("12399.85"),
"size" : 5,
"style" : "Scarpin",
"onSale" : false,
"moreDetails" : {
"qtyAvlbl" : 10
}
}
As a result, we got a total of 7 documents which we will be working on.
Renaming a Field Name Using $rename Operator
In this example, we will see how to rename a top-level field’s name using the $rename operator.
Let’s try to rename the “size” field to “sizeUK”:
> db.shoes.updateMany( {}, { $rename: { "size": "sizeUK" } } )
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
Let’s see what our documents look like after the $rename operation:
> db.shoes.find().pretty()
{
"_id" : ObjectId("616b18a556e71503e4a0334d"),
"name" : "Mariana Grider Boots",
"price" : 23000,
"style" : "Slingbacks",
"onSale" : false,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 40
},
"sizeUK" : 7
}
{
"_id" : ObjectId("616b18a556e71503e4a0334e"),
"name" : "Purple Shimmery Heels",
"price" : 13380,
"style" : "Peep Toes",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
},
"itemNumber" : 0,
"sizeUK" : 6
}
{
"_id" : ObjectId("616b18a556e71503e4a0334f"),
"name" : "Zac Porter Straps",
"price" : 19760,
"style" : "Ankle Strap",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
},
"sizeUK" : 8
}
{
"_id" : ObjectId("616b18a556e71503e4a03350"),
"name" : "Valerina's Red Carpet Toes",
"price" : 26000,
"style" : "Scarpin",
"onSale" : false,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
},
"sizeUK" : 9
}
{
"_id" : ObjectId("616b18a556e71503e4a03351"),
"name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
"price" : 10780,
"style" : "Wedge Booties",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qtyAvlbl" : 10
},
"sizeUK" : 7
}
{
"_id" : ObjectId("6171bbf968378cae00d83aca"),
"name" : "Caroline Wielder Shoes",
"price" : NumberDecimal("14999.985"),
"style" : "Wedge Booties",
"onSale" : true,
"moreDetails" : {
"qtyAvlbl" : 10
},
"sizeUK" : 5
}
{
"_id" : ObjectId("6171bbf968378cae00d83acb"),
"name" : "Marilyn Jaworski Sky Highs",
"price" : NumberDecimal("12399.85"),
"style" : "Scarpin",
"onSale" : false,
"moreDetails" : {
"qtyAvlbl" : 10
},
"sizeUK" : 5
}
In the output, it can be seen that the given field name is successfully renamed to the specified field name. You can also see that after the operation, the new field is moved to the very bottom of the document.
Renaming an Embedded Document Field Name Using $rename Operator
In this example, we will rename an embedded field’s name. An embedded field is a field that is present or nested inside another field.
Let’s try to rename the “qtyAvlbl” field to just “qty” within the “moreDetails” field:
> db.shoes.updateMany( { }, { $rename: { "moreDetails.qtyAvlbl": "moreDetails.qty" } } )
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
Let’s see what our documents look like after the operation:
> db.shoes.find().pretty()
{
"_id" : ObjectId("616b18a556e71503e4a0334d"),
"name" : "Mariana Grider Boots",
"price" : 23000,
"style" : "Slingbacks",
"onSale" : false,
"__v" : 0,
"moreDetails" : {
"qty" : 10
},
"sizeUK" : 7
}
{
"_id" : ObjectId("616b18a556e71503e4a0334e"),
"name" : "Purple Shimmery Heels",
"price" : 13380,
"style" : "Peep Toes",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qty" : 10
},
"itemNumber" : 0,
"sizeUK" : 6
}
{
"_id" : ObjectId("616b18a556e71503e4a0334f"),
"name" : "Zac Porter Straps",
"price" : 19760,
"style" : "Ankle Strap",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qty" : 10
},
"sizeUK" : 8
}
{
"_id" : ObjectId("616b18a556e71503e4a03350"),
"name" : "Valerina's Red Carpet Toes",
"price" : 26000,
"style" : "Scarpin",
"onSale" : false,
"__v" : 0,
"moreDetails" : {
"qty" : 10
},
"sizeUK" : 9
}
{
"_id" : ObjectId("616b18a556e71503e4a03351"),
"name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
"price" : 10780,
"style" : "Wedge Booties",
"onSale" : true,
"__v" : 0,
"moreDetails" : {
"qty" : 10
},
"sizeUK" : 7
}
{
"_id" : ObjectId("6171bbf968378cae00d83aca"),
"name" : "Caroline Wielder Shoes",
"price" : NumberDecimal("14999.985"),
"style" : "Wedge Booties",
"onSale" : true,
"moreDetails" : {
"qty" : 10
},
"sizeUK" : 5
}
{
"_id" : ObjectId("6171bbf968378cae00d83acb"),
"name" : "Marilyn Jaworski Sky Highs",
"price" : NumberDecimal("12399.85"),
"style" : "Scarpin",
"onSale" : false,
"moreDetails" : {
"qty" : 10
},
"sizeUK" : 5
}
You can see that “qtyAvlbl” is renamed to “qty” inside the “moreDetails” field.
Conclusion
In this article, we learned about an extraordinary MongoDB operator $rename that is used to rename document fields. This operator can update multiple fields simultaneously which can make the process of changing the database structure extremely comfortable.
For example in this article, we have used the $rename Operator with the updateMany() method, but you can also combine it with methods like updateOne(), findOneAndUpdate() etc. to do the operation. Hope this article includes all the important details about the $rename operator in MongoDB.
Articles you may find interesting: