The $rename operator changes the name of a field in a document without altering its value.
Click to reveal answer
beginner
How do you use $rename to rename a field called oldName to newName?
Use { $rename: { 'oldName': 'newName' } } inside an update operation to rename the field.
Click to reveal answer
intermediate
Can $rename rename multiple fields at once?
Yes, you can rename multiple fields by listing them all inside the $rename object, like { $rename: { 'old1': 'new1', 'old2': 'new2' } }.
Click to reveal answer
intermediate
What happens if the new field name already exists when using $rename?
MongoDB will overwrite the existing field with the renamed field's value.
Click to reveal answer
advanced
Is it possible to rename a nested field using $rename?
Yes, you can rename nested fields by specifying the full path with dot notation, like { $rename: { 'address.street': 'address.road' } }.
Click to reveal answer
Which MongoDB operator is used to rename a field in a document?
A$unset
B$set
C$rename
D$push
✗ Incorrect
The $rename operator changes the name of a field.
How do you rename the field username to user_name in MongoDB?
A{ $rename: { 'username': 'user_name' } }
B{ $unset: { 'username': '' } }
C{ $set: { 'user_name': 'username' } }
D{ $rename: { 'user_name': 'username' } }
✗ Incorrect
The syntax is { $rename: { 'oldField': 'newField' } }.
What happens if the new field name already exists when using $rename?
AThe operation fails with an error.
BThe existing field is overwritten.
CBoth fields are kept with the same name.
DThe rename is ignored.
✗ Incorrect
MongoDB overwrites the existing field with the renamed field's value.
Can $rename be used to rename nested fields?
ANo, it only works on top-level fields.
BOnly if the nested field is an array.
COnly if the nested field is a string.
DYes, by using dot notation for the field path.
✗ Incorrect
You rename nested fields by specifying the full path with dot notation.
Which of the following is a valid $rename update to rename two fields at once?
A{ $rename: { 'a': 'b', 'c': 'd' } }
B{ $rename: { 'a': 'b' }, { 'c': 'd' } }
C{ $rename: ['a', 'b', 'c', 'd'] }
D{ $rename: 'a to b, c to d' }
✗ Incorrect
Multiple fields can be renamed by listing them inside the same $rename object.
Explain how to rename a field in a MongoDB document using the $rename operator.
Think about how you tell MongoDB which field to change and what to rename it to.
You got /3 concepts.
Describe what happens if the new field name already exists when using $rename.
Consider if MongoDB merges, errors, or replaces fields.
You got /3 concepts.
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.