How to Change a Field Name using the MongoDB Shell
Introduction
Maybe you misnamed a field in an entire collection or want to update so it’s more intuitive for your development team. Doing this in a relational database is a little bit simler because all you have to do is update the column name ( and update all your queries to match ) but with NoSQL the field names aren’t just on a table, they are in every document so we’ll have to update every document in the collection.
Prerequisites
- You should have MongoDB installed and running.
- It’s recommended that you create a database and collection to experiment with as you follow along.
- Some command line experience is recommended.
Goal
We always like to start out with an explicit goal to make sure we hit our mark by the end of the tutorial. Our goal is to assume that we want to standardize our field names by removing all spaces so we will update our “First Name” field to “FirstName” and “Last Name” field to “LastName” in our demoPeopleCollection
in our demoDatabase
.
Here is the initial demoPeopleCollection
for comparison of before and after:
`
js
> db.demoPeopleCollection.find()
{
1
2
3
4
5 "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79ca8"),
"First Name" : "Alfred",
"Last Name" : "Amus",
"Gender" : "Male",
"Age" : 39
} {
1
2
3
4
5 "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79ca9"),
"First Name" : "Caitlyn",
"Last Name" : "Anderson",
"Gender" : "Female",
"Age" : 21
} {
1
2
3
4
5 "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79caa"),
"First Name" : "Donna",
"Last Name" : "Dee",
"Gender" : "Female",
"Age" : 21
} {
1
2
3
4
5 "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79cab"),
"First Name" : "Bob",
"Last Name" : "Caster",
"Gender" : "Male",
"Age" : 65
} {
1
2
3
4
5 "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79cac"),
"First Name" : "Tessa",
"Last Name" : "Sanders",
"Gender" : "Female",
"Age" : 39
}
`
1. Update the fields with .updateMany() function and $rename operator
To rename a field you use the .updateMany() method which has the function definition below:
`
js
db.collection.updateMany(`
For our example we will leave the <updateData>
parameter we’ll use the $rename operator with a document describing which fields we want to redefine and how. Let’s take a look at the command we we executed to accomplish the field name change:
1 2 | > db.demoPeopleCollection.updateMany( {}, { $rename: { 'First Name': 'FirstName', 'Last Name': 'LastName'} } ) { "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 } |
As you can see we get a response that 5 documents have been modified. Let’s verify changes by outputting the contents of the collection using the .find() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | > db.demoPeopleCollection.find() { "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79ca8"), "Gender" : "Male", "Age" : 39, "FirstName" : "Alfred", "LastName" : "Amus" } { "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79ca9"), "Gender" : "Female", "Age" : 21, "FirstName" : "Caitlyn", "LastName" : "Anderson" } { "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79caa"), "Gender" : "Female", "Age" : 21, "FirstName" : "Donna", "LastName" : "Dee" } { "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79cab"), "Gender" : "Male", "Age" : 65, "FirstName" : "Bob", "LastName" : "Caster" } { "_id" : ObjectId("5c7ef6e4d2ea12b0cdc79cac"), "Gender" : "Female", "Age" : 39, "FirstName" : "Tessa", "LastName" : "Sanders" } |
As you can see the field name “First Name” has been updated to “FirstName” and the field name “Last Name” has been updated to “LastName”.
Learn More
To learn more about the .updateMany() commands and all the ways it can be utilized` we suggest you go to the MongoDB documentation directly or you can always reach out to an expert at Object Rocket to discuss your specific application.
Conclusion
We showed you how to rename a field name in your MongoDB collection using the MongoDB shell. This is an important task since it’s key to keep your data organized because with the large datasets we use today it can quickly become a nightmare. If you’d like to learn more about the options available in these functions, the MongoDB documentations is fairly straightforward and can answer many of your questions. We hope this tutorial was able to answer your question or provide you the snippet of code that you were looking for all along. Thank you for your time and if you have any feedback or questions do not hesitate to reach out to us.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started