$rename Operator in MongoDB – Quick 2021 Guide

Rename Operator Featured Image

In this tutorial. I will explain how to use the $rename operator.

The $rename operator in MongoDB allows us to rename a field’s name. This guide will demonstrate examples of how to use the $rename operator in MongoDB inside the mongo shell.

The $rename Operator Syntax in MongoDB

Below is the syntax for the $rename operator in MongoDB:

{$rename: { fieldName1: newName1, fieldName2: newName2, ... } }

Make sure the new field name is different from the old field name. To select an embedded field, you can use the dot notation.

If the given document field already contains the specified newName, then the operator will remove that field and rename it with newName.

Using the $rename Operator in MongoDB

Let us get started by taking a look at a few examples to learn how to use the $rename operator in MongoDB.

Before we start, I will print the documents of my shoes collection here to know what our sample collection looks like for the guide.

> 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
        }
}

We have a total of 7 documents in our collection.

Renaming a Top-Level Field’s Name Using the $rename Operator

In this example, I will rename a top-level field’s name using the $rename operator in MongoDB inside the mongo shell.

Let’s say I want to rename the size field to sizeUK.

> db.shoes.updateMany( {}, { $rename: { "size": "sizeUK" } } )
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
> 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
}

You can notice that after the operation, the new field has shifted to the very bottom inside the document.

Renaming an Embedded Field’s Name Using the $rename Operator

In this example, I will rename an embedded field’s name using the $rename operator in MongoDB inside the mongo shell.

Let’s say I want to rename the qtyAvlbl field to just qty, which is embedded in the moreDetails field.

> db.shoes.updateMany( { }, { $rename: { "moreDetails.qtyAvlbl": "moreDetails.qty" } } )
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
> 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
}

Perfect! We have successfully renamed an embedded field’s name using the $rename operator in MongoDB.

Read More: $ (update) Operator in MongoDB – Quick Guide

Conclusion

In this article, we learned how to use the $rename operator in MongoDB.

Noteworthy References

MongoDB Docs