Change Field Name using MongoDB Shell

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Let us make our way through some basics as we get into how to change a field name using the MongoDB Shell.

MongoDB is a NoSQL database. It means there are no tables in MongoDB. It stores data in the JSON format. The data is stored in the documents which in turn are stored in collections. Every document has key-value pairs. The data is stored in key-value pairs in MongoDB. The following is an example of a document in MongoDB.

1
2
3
4
5
{
    name: "John",
    age: 29,
    city: "New York"
}

The above document is in the JSON format and it has three key-value pairs. The left part of a colon is known as a field and the right part is the data. The field in every document should be unique and it should have a proper name. The field denotes what kind of data is stored in it. While defining a document, we should design the names of the fields very carefully. But sometimes, there could be spelling mistakes in the field names or we could need to change the name of the field according to our requirements. MongoDB provides the rename operator for changing the name of the field in a document from mongo shell.

The rename operator

The rename operator is used with an update operation. The following is the syntax for using the rename operator with an update operation.

1
db.<collection-name>.update({query}, {\$rename: { <field1> : <newName1> , <field2> : <newName2>}})

Changing field name of a single document from the mongo shell

In the update operation, the first parameter is the query, i.e. the condition for the operation. We can leave it empty. In the second parameter, we can use the rename operator with a dollar ($) sign. Let’s understand the rename operator with the help of an example.

In the database, demoDB, there is a collection named demoCollection. The demoCollection has three documents.

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
> db.demoCollection.find().pretty()
> {

        "_id" : ObjectId("5d49d105676cc6e81764cdf1"),
        "name" : "John",
        "age" : 28,
        "city" : "New York",
        "code" : 123

}
{
"\_id" : ObjectId("5d49d10c676cc6e81764cdf2"),
"name" : "Sam",
"age" : 25,
"city" : "Texas",
"code" : 987
}
{
"\_id" : ObjectId("5d49d114676cc6e81764cdf3"),
"name" : "Peter",
"age" : 23,
"city" : "New York",
"code" : 546
}

>

Apart from the _id field, every document has four fields, name, age, city and code. Let’s change the city field to location where the name is Sam.

1
2
> db.demoCollection.update({"name" : "Sam"}, {\$rename : {"city": "location"}})
> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

In the above command, we used the update operation with the rename operator. The condition in the query is that the name equals Sam. In the second parameter, we used the rename operator and changed the city field to location. This operation will match the first document where the name is Sam and the city field will be changed to location. The value won’t be affected. Let’s verify it.

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
> db.demoCollection.find().pretty()
> {

        "_id" : ObjectId("5d49d105676cc6e81764cdf1"),
        "name" : "John",
        "age" : 28,
        "city" : "New York",
        "code" : 123

}
{
"\_id" : ObjectId("5d49d10c676cc6e81764cdf2"),
"name" : "Sam",
"age" : 25,
"code" : 987,
"location" : "Texas"
}
{
"\_id" : ObjectId("5d49d114676cc6e81764cdf3"),
"name" : "Peter",
"age" : 23,
"city" : "New York",
"code" : 546
}

>

Observe the second document, the city field is changed to location. The value is not affected. This is how the rename operator updates a field in a document.

Changing field names of multiple documents from the mongo shell

We can also update field names of multiple documents in a collection. Let’s discuss it with an example.

The documents with the name John and Peter, still have a city field. We need to change the city field to location as we did in the case of the document where the name is Sam. But instead of doing separately, we can rename it in one command. John and Peter have the same value for the city, i.e. New York. We can use this as our condition in the query parameter and then, we can use the rename operator.

1
2
> db.demoCollection.updateMany({"city" : "New York"}, {\$rename : {"city": "location"}})
> { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

In the above command, we used updateMany to update multiple documents. The condition in the query is that the city equals New York. Every document where the city is equal to New York will be matched and then, because of the rename operator, the city field will be changed to location. Let’s verify it.

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
> db.demoCollection.updateMany({"city" : "New York"}, {\$rename : {"city": "location"}})
> { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.demoCollection.find().pretty()
> {

        "_id" : ObjectId("5d49d105676cc6e81764cdf1"),
        "name" : "John",
        "age" : 28,
        "code" : 123,
        "location" : "New York"

}
{
"\_id" : ObjectId("5d49d10c676cc6e81764cdf2"),
"name" : "Sam",
"age" : 25,
"code" : 987,
"location" : "Texas"
}
{
"\_id" : ObjectId("5d49d114676cc6e81764cdf3"),
"name" : "Peter",
"age" : 23,
"code" : 546,
"location" : "New York"
}

>

The command worked perfectly and all the documents have a location field instead of city.

Changing multiple field names of a single document

What if we need to change more than one field name of a single document? We can also do this by using the rename operator. Let’s change the names of age and code fields of the document where name is Sam.

1
2
> db.demoCollection.updateMany({"name" : "Sam"}, {\$rename : {"age" : "properAge", "code" : "clientCode" }})
> { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

In the above command, the condition in the query is that the name equals Sam. This time, the rename operator has more than one value to be changed, separated by a comma. The name’s of the age and code fields are changed to properAge and clientCode respectively. Let’s verify this.

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
> db.demoCollection.find().pretty()
> {

        "_id" : ObjectId("5d49d105676cc6e81764cdf1"),
        "name" : "John",
        "age" : 28,
        "code" : 123,
        "location" : "New York"

}
{
"\_id" : ObjectId("5d49d10c676cc6e81764cdf2"),
"name" : "Sam",
"location" : "Texas",
"clientCode" : 987,
"properAge" : 25
}
{
"\_id" : ObjectId("5d49d114676cc6e81764cdf3"),
"name" : "Peter",
"age" : 23,
"code" : 546,
"location" : "New York"
}

>

Observe the second document. The names of both fields are changed without affecting the values.

Changing the name of a field in an embedded document

We can also change the names of the fields that are embedded inside a document. There is a slight difference in that. But first let’s add an embedded document to one of the existing documents in the demoCollection.

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
37
38
> db.demoCollection.find().pretty()
> {

        "_id" : ObjectId("5d49dc64676cc6e81764cdfb"),
        "name" : "John",
        "age" : 28,
        "code" : 123,
        "location" : "New York"

}
{
"\_id" : ObjectId("5d49dc70676cc6e81764cdfc"),
"name" : "Sam",
"location" : "Texas",
"clientCode" : 987,
"properAge" : 25
}
{
"\_id" : ObjectId("5d49dc7b676cc6e81764cdfd"),
"name" : "Peter",
"age" : 23,
"code" : 546,
"location" : "New York"
}
{
"\_id" : ObjectId("5d49dcc4676cc6e81764cdfe"),
"name" : "Lisa",
"age" : 24,
"city" : "Chicago",
"code" : 780,
"Adress" : {
"streeeeet" : 43,
"houseNumber" : 345,
"city" : "Detroit"
}
}

>

We inserted a new document in the demoCollection that has one embedded document, named Adress. Observe the latest document and its address field. The street name is wrong. We can use the rename operator to correct it.

1
2
> db.demoCollection.update({"name" : "Lisa" }, { \$rename : { "Address.streeeeet" : "Address.street"}})
> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Observe the rename operator carefully. To change the name of the field inside an embedded document, we have to write the name of the document, followed by the field name we want to change. The new name should be in the same order. Let’s verify this.

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
37
38
> db.demoCollection.find().pretty()
> {

        "_id" : ObjectId("5d49dc64676cc6e81764cdfb"),
        "name" : "John",
        "age" : 28,
        "code" : 123,
        "location" : "New York"

}
{
"\_id" : ObjectId("5d49dc70676cc6e81764cdfc"),
"name" : "Sam",
"location" : "Texas",
"clientCode" : 987,
"properAge" : 25
}
{
"\_id" : ObjectId("5d49dc7b676cc6e81764cdfd"),
"name" : "Peter",
"age" : 23,
"code" : 546,
"location" : "New York"
}
{
"\_id" : ObjectId("5d49dcc4676cc6e81764cdfe"),
"name" : "Lisa",
"age" : 24,
"city" : "Chicago",
"code" : 780,
"Address" : {
"houseNumber" : 345,
"city" : "Detroit",
"street" : 43
}
}

>

Check the embedded document in the fourth document. The street name is correct now. This is how we can change the name of fields in an embedded document.

Conclusion

The rename operator is used to change the name of fields from a mongo shell. There are different ways to change the names of the fields and discussed every way with examples.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.