0
0
MongoDBquery~20 mins

$rename operator for field names in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Rename Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after using $rename to change a field name?
Given a MongoDB collection with a document:
{ "_id": 1, "name": "Alice", "age": 30 }
After running this update:
db.collection.updateOne({ _id: 1 }, { $rename: { "name": "fullName" } })
What does the document look like now?
A{ "_id": 1, "name": "Alice", "fullName": "Alice", "age": 30 }
B{ "_id": 1, "name": "Alice", "age": 30 }
C{ "_id": 1, "fullName": "Alice", "age": 30 }
D{ "_id": 1, "age": 30 }
Attempts:
2 left
💡 Hint
The $rename operator changes the name of a field without changing its value.
📝 Syntax
intermediate
2:00remaining
Which $rename update query is syntactically correct?
Choose the valid MongoDB update query that renames the field "oldField" to "newField".
Adb.collection.updateOne({}, { $rename: ["oldField", "newField"] })
Bdb.collection.updateOne({}, { $rename: { oldField: newField } })
Cdb.collection.updateOne({}, { rename: { "oldField": "newField" } })
Ddb.collection.updateOne({}, { $rename: { "oldField": "newField" } })
Attempts:
2 left
💡 Hint
Field names and new names must be strings inside the $rename object.
🧠 Conceptual
advanced
2:00remaining
What happens if you try to rename a field to an existing field name?
Suppose a document has fields "a" and "b". You run:
db.collection.updateOne({}, { $rename: { "a": "b" } })
What is the result?
AThe update fails with an error because "b" already exists.
BThe update succeeds but creates a nested field "b.a".
CBoth fields "a" and "b" remain unchanged.
DThe field "a" is renamed to "b", overwriting the original "b" field.
Attempts:
2 left
💡 Hint
MongoDB does not allow renaming a field to a name that already exists in the document.
optimization
advanced
2:00remaining
How to rename multiple fields in one update efficiently?
You want to rename fields "firstName" to "fname" and "lastName" to "lname" in one update. Which query is best?
Adb.collection.updateMany({}, { $rename: { "firstName": "fname", "lastName": "lname" } })
Bdb.collection.updateMany({}, { $rename: { "firstName": "fname" } }); db.collection.updateMany({}, { $rename: { "lastName": "lname" } })
Cdb.collection.updateMany({}, { $set: { "fname": "$firstName", "lname": "$lastName" }, $unset: ["firstName", "lastName"] })
Ddb.collection.updateMany({}, { $rename: ["firstName", "fname"], $rename: ["lastName", "lname"] })
Attempts:
2 left
💡 Hint
You can rename multiple fields in one $rename object.
🔧 Debug
expert
2:00remaining
Why does this $rename update fail with a syntax error?
Consider this update query:
db.collection.updateOne({}, { $rename: { 'oldField': 'newField', } })
It causes a syntax error. What is the cause?
ASingle quotes cannot be used for field names in MongoDB update operators.
BTrailing comma after the last key-value pair in the $rename object is invalid in MongoDB shell.
CThe $rename operator requires an array, not an object.
DThe updateOne method requires a filter with at least one field.
Attempts:
2 left
💡 Hint
Check the syntax of the JavaScript object inside the update.