Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO:
MongoDB vCore
The $rename
operator is used to rename fields in documents during update operations. It removes the field with the old name and creates a new field with the specified name, preserving the original value. This operator is useful for restructuring document schemas or correcting field naming conventions.
The syntax for the $rename
operator is as follows:
{
$rename: {
<field1>: <newName1>,
<field2>: <newName2>,
...
}
}
Description | |
---|---|
field |
The current name of the field to be renamed. |
newName |
The new name for the field. |
Let's understand the usage with sample json from stores
dataset.
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"name": "First Up Consultants | Bed and Bath Center - South Amir",
"location": {
"lat": 60.7954,
"lon": -142.0012
},
"staff": {
"totalStaff": {
"fullTime": 18,
"partTime": 17
}
},
"sales": {
"totalSales": 37701,
"salesByCategory": [
{
"categoryName": "Mattress Toppers",
"totalSales": 37701
}
]
}
}
Suppose you want to rename the name
field to storeName
and location
to storeLocation
.
db.stores.updateOne(
{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
{
$rename: {
"name": "storeName",
"location": "storeLocation"
}
}
)
You can also rename nested fields by using dot notation.
db.stores.updateOne(
{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
{
$rename: {
"location.lat": "location.latitude",
"location.lon": "location.longitude",
"staff.totalStaff.fullTime": "staff.totalStaff.fullTimeEmployees"
}
}
)
You can rename fields across multiple documents using updateMany()
.
db.stores.updateMany(
{},
{
$rename: {
"sales.totalSales": "sales.revenue",
"staff.totalStaff": "staff.employeeCount"
}
}
)
Important
If the field specified in $rename
does not exist, the operation will have no effect on that field.
If the new field name already exists, the $rename
operation will overwrite the existing field.
The $rename
operator cannot be used to rename array elements or fields within array elements.
Field names cannot be empty strings or contain null characters.
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB