📖 Scenario: You work in a library database. The book records have a field called title but the team wants to rename it to bookTitle for clarity.
🎯 Goal: Learn how to use the MongoDB $rename operator to rename a field in all documents of a collection.
📋 What You'll Learn
Create a collection called books with three documents having fields title and author.
Create a filter variable called filter to select all documents.
Use the $rename operator in an update command to rename the title field to bookTitle.
Complete the update command to apply the rename to all matching documents.
💡 Why This Matters
🌍 Real World
Renaming fields in a database is common when improving data clarity or adapting to new requirements without losing existing data.
💼 Career
Database administrators and backend developers often need to update database schemas safely using operators like $rename.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a collection called books and insert these three documents exactly: { title: "1984", author: "George Orwell" }, { title: "Brave New World", author: "Aldous Huxley" }, and { title: "Fahrenheit 451", author: "Ray Bradbury" }.
MongoDB
Hint
Use db.books.insertMany([...]) with the exact documents inside the array.
2
Create a filter to select all documents
Create a variable called filter and set it to an empty object {} to select all documents in the collection.
MongoDB
Hint
Use const filter = {} to select all documents.
3
Use $rename operator to rename the field
Create an update object called update that uses the $rename operator to rename the field title to bookTitle.
MongoDB
Hint
Use { $rename: { title: "bookTitle" } } as the update object.
4
Apply the update to rename the field in all documents
Use db.books.updateMany with the filter and update objects to rename the title field to bookTitle in all documents.
MongoDB
Hint
Use db.books.updateMany(filter, update) to apply the rename to all documents.
Practice
(1/5)
1. What does the $rename operator do in MongoDB?
easy
A. It changes the value of a field in documents.
B. It deletes a field from documents.
C. It changes the name of a field in documents.
D. It adds a new field to documents.
Solution
Step 1: Understand the purpose of $rename
The $rename operator is used to change the name of an existing field in MongoDB documents.
Step 2: Compare with other update operators
Unlike $unset which deletes fields, or $set which changes values or adds fields, $rename only changes field names.
Final Answer:
It changes the name of a field in documents. -> Option C
Quick Check:
$rename changes field names [OK]
Hint: Remember: $rename changes field names, not values or deletion [OK]
Common Mistakes:
Confusing $rename with $set or $unset
Thinking $rename changes field values
Assuming $rename adds new fields
2. Which of the following is the correct syntax to rename the field oldName to newName in all documents of a collection named users?
easy
A. db.users.updateMany({}, { $rename: { oldName: newName } })
B. db.users.updateMany({}, { $rename: { oldName: 'newName' } })
C. db.users.updateMany({}, { $rename: { 'oldName': newName } })
D. db.users.updateMany({}, { $rename: { 'oldName': 'newName' } })
Solution
Step 1: Check the syntax for $rename
The $rename operator requires a mapping object where both old and new field names are strings, so keys and values must be quoted.
Step 2: Validate each option
db.users.updateMany({}, { $rename: { 'oldName': 'newName' } }) correctly uses quotes around both 'oldName' and 'newName'. Options A, C, and D miss quotes on either key or value, causing syntax errors.
B. The new field name "date" already exists in some documents.
C. Field names in $rename must be quoted strings.
D. The updateMany command requires a filter other than {}.
Solution
Step 1: Analyze the error cause for $rename
If the new field name already exists in documents, MongoDB throws an error because it cannot rename a field to an existing field name.
Step 2: Check other options
Field names in $rename must be quoted strings. is incorrect because field names are quoted correctly. The collection orders does not exist. is unlikely as the error is specific to $rename. The updateMany command requires a filter other than {}. is invalid because {} is a valid filter for updateMany.
Final Answer:
The new field name "date" already exists in some documents. -> Option B
Quick Check:
$rename fails if new field exists [OK]
Hint: Cannot rename to existing field name [OK]
Common Mistakes:
Ignoring existing fields with new name
Assuming empty filter is invalid
Confusing syntax errors with runtime errors
5. You have a collection employees with documents containing fields fname and lname. You want to rename fname to firstName and lname to lastName in one update command. Which command correctly does this?
The $rename operator accepts an object mapping old field names to new field names, allowing multiple renames in one command.
Step 2: Check syntax correctness
db.employees.updateMany({}, { $rename: { 'fname': 'firstName', 'lname': 'lastName' } }) correctly uses a single object with quoted old and new field names. db.employees.updateMany({}, { $rename: { 'fname': 'firstName' }, { 'lname': 'lastName' } }) has invalid syntax with two separate objects. db.employees.updateMany({}, { $rename: { fname: 'firstName', lname: 'lastName' } }) misses quotes on keys. db.employees.updateMany({}, { $rename: { 'fname': firstName, 'lname': lastName } }) misses quotes on new field names.