0
0
MongoDBquery~20 mins

$replaceRoot for restructuring in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $replaceRoot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this $replaceRoot aggregation?
Given the collection documents:
{ "_id": 1, "user": { "name": "Alice", "age": 30 }, "score": 85 }

What will be the output of this aggregation?
[{ $replaceRoot: { newRoot: "$user" } }]
MongoDB
[{ $replaceRoot: { newRoot: "$user" } }]
A[{ "name": "Alice", "age": 30 }]
B[{ "user": { "name": "Alice", "age": 30 }, "score": 85 }]
C[{ "_id": 1, "name": "Alice", "age": 30 }]
D[{ "name": "Alice", "age": 30, "score": 85 }]
Attempts:
2 left
💡 Hint
Think about what $replaceRoot does with the specified newRoot field.
query_result
intermediate
2:00remaining
What does this $replaceRoot pipeline produce?
Given documents:
{ "_id": 2, "order": { "id": 101, "items": 3 }, "status": "shipped" }

Run this pipeline:
[{ $replaceRoot: { newRoot: { $mergeObjects: ["$order", { "status": "$status" }] } } }]
MongoDB
[{ $replaceRoot: { newRoot: { $mergeObjects: ["$order", { "status": "$status" }] } } }]
A[{ "id": 101, "items": 3, "status": "shipped" }]
B[{ "order": { "id": 101, "items": 3 }, "status": "shipped" }]
C[{ "id": 101, "items": 3 }]
D[{ "status": "shipped" }]
Attempts:
2 left
💡 Hint
Look at how $mergeObjects combines fields from two documents.
📝 Syntax
advanced
2:00remaining
Which $replaceRoot usage is syntactically correct?
Choose the option with correct syntax for $replaceRoot stage in MongoDB aggregation.
A{ $replaceRoot: "$details" }
B{ $replaceRoot: { root: "$details" } }
C{ $replaceRoot: { newRoot: details } }
D{ $replaceRoot: { newRoot: "$details" } }
Attempts:
2 left
💡 Hint
Check the exact field name and value type required by $replaceRoot.
optimization
advanced
2:00remaining
How to optimize restructuring with $replaceRoot and $project?
You want to restructure documents by replacing the root with the 'profile' field but also keep the original '_id'. Which pipeline is best?
A[{ $replaceRoot: { newRoot: "$profile" } }, { $project: { _id: 1 } }]
B[{ $replaceRoot: { newRoot: { $mergeObjects: ["$profile", { _id: "$_id" }] } } }]
C[{ $replaceRoot: { newRoot: "$profile" } }, { $addFields: { _id: "$_id" } }]
D[{ $project: { _id: 1, profile: 1 } }, { $replaceRoot: { newRoot: "$profile" } }]
Attempts:
2 left
💡 Hint
Consider how to keep _id while replacing root in one step.
🧠 Conceptual
expert
2:00remaining
What error occurs if $replaceRoot newRoot field is missing?
Consider this aggregation stage:
{ $replaceRoot: { } }

What error will MongoDB raise when running this?
ATypeError: Cannot read property 'newRoot' of undefined
BSyntaxError: Unexpected end of stage specification
CMongoError: 'newRoot' is required for $replaceRoot stage
DNo error, pipeline runs but documents are unchanged
Attempts:
2 left
💡 Hint
Think about required fields for $replaceRoot stage.