Challenge - 5 Problems
MongoDB Rename Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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?Attempts:
2 left
💡 Hint
The $rename operator changes the name of a field without changing its value.
✗ Incorrect
The $rename operator renames the field "name" to "fullName". The original "name" field is removed and replaced by "fullName" with the same value.
📝 Syntax
intermediate2:00remaining
Which $rename update query is syntactically correct?
Choose the valid MongoDB update query that renames the field "oldField" to "newField".
Attempts:
2 left
💡 Hint
Field names and new names must be strings inside the $rename object.
✗ Incorrect
The $rename operator requires an object with string keys and string values representing old and new field names. Option D uses correct syntax with quotes.
🧠 Conceptual
advanced2: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?Attempts:
2 left
💡 Hint
MongoDB does not allow renaming a field to a name that already exists in the document.
✗ Incorrect
Renaming a field to an existing field name causes a conflict and MongoDB throws an error to prevent data loss.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
You can rename multiple fields in one $rename object.
✗ Incorrect
Option A uses a single update with $rename specifying both fields, which is efficient and atomic. Option A runs two updates, less efficient. Option A is invalid syntax. Option A is invalid syntax.
🔧 Debug
expert2: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?Attempts:
2 left
💡 Hint
Check the syntax of the JavaScript object inside the update.
✗ Incorrect
In JavaScript, trailing commas in object literals are allowed in modern environments, but some MongoDB shells or drivers may reject them causing syntax errors.