Rename a Field in a MongoDB Collection

In MongoDB, you can rename a field when updating documents in a collection.

To rename a field, call the $rename operator with the current name of the field and the new name. This renames the field in all matching documents that have a field with that name.

Example

Suppose we have a collection called employees with the following documents:

{ "_id" : 1, "name" : "Sandy", "salary" : 55000 }
{ "_id" : 2, "name" : "Sarah", "salary" : 128000 }
{ "_id" : 3, "name" : "Fritz", "salary" : 25000 }
{ "_id" : 4, "name" : "Chris", "salary" : 45000 }
{ "_id" : 5, "name" : "Beck", "salary" : 82000 }

And suppose we want to rename the name field to employee.

We could do this:

db.employees.updateMany( 
  { }, 
  { $rename: { "name": "employee" } } 
)

Output:

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }

Now when we use the find() method to return the documents in the collection, we see the following result:

db.employees.find()

Result:

{ "_id" : 1, "salary" : 55000, "employee" : "Sandy" }
{ "_id" : 2, "salary" : 128000, "employee" : "Sarah" }
{ "_id" : 3, "salary" : 25000, "employee" : "Fritz" }
{ "_id" : 4, "salary" : 45000, "employee" : "Chris" }
{ "_id" : 5, "salary" : 82000, "employee" : "Beck" }

Rename Multiple Fields

You can rename multiple fields by separating them with a comma.

Example:

db.employees.updateMany( 
  { }, 
  { $rename: { "employee": "e", "salary": "s" } } 
)

Output:

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }

And here’s what the collection looks like now:

{ "_id" : 1, "e" : "Sandy", "s" : 55000 }
{ "_id" : 2, "e" : "Sarah", "s" : 128000 }
{ "_id" : 3, "e" : "Fritz", "s" : 25000 }
{ "_id" : 4, "e" : "Chris", "s" : 45000 }
{ "_id" : 5, "e" : "Beck", "s" : 82000 }

Embedded Documents

You can use dot notation to update field names in embedded documents.

Example document:

db.pets.findOne()

Result:

{
	"_id" : 1,
	"name" : "Wag",
	"details" : {
		"type" : "Dog",
		"weight" : 20,
		"awards" : {
			"Florida Dog Awards" : "Top Dog",
			"New York Marathon" : "Fastest Dog",
			"Sumo 2020" : "Biggest Dog"
		}
	}
}

Let’s update some of the fields in the embedded document:

db.pets.updateMany( 
  { }, 
  { $rename: { 
      "details.type": "details.t", 
      "details.weight": "details.w",
      "details.awards": "details.a"
    } 
  } 
)

Now when we check the document, we see the following:

db.pets.findOne()

Result:

{
	"_id" : 1,
	"name" : "Wag",
	"details" : {
		"a" : {
			"Florida Dog Awards" : "Top Dog",
			"New York Marathon" : "Fastest Dog",
			"Sumo 2020" : "Biggest Dog"
		},
		"t" : "Dog",
		"w" : 20
	}
}

We can also update the field names of the documents that are embedded in the embedded documents:

db.pets.updateMany( 
  { }, 
  { $rename: { 
      "details.a.Florida Dog Awards": "details.a.fda",
      "details.a.New York Marathon": "details.a.nym",
      "details.a.Sumo 2020": "details.a.s2020"
    } 
  } 
)

And let’s check the document again:

db.pets.findOne()

Result:

{
	"_id" : 1,
	"name" : "Wag",
	"details" : {
		"a" : {
			"fda" : "Top Dog",
			"nym" : "Fastest Dog",
			"s2020" : "Biggest Dog"
		},
		"t" : "Dog",
		"w" : 20
	}
}