$replaceRoot
The $replaceRoot stage replaces the entire input document with a specified subdocument.
📌 Syntax
{ "$replaceRoot": { "newRoot": <expression> } }
✅ Base Example – Replace Root with Embedded Customer Object
📥 Input Document
{
"_id": 1,
"orderId": 22,
"customer": {
"name": "Alice",
"email": "alice@example.com"
},
"items": [{ "name": "Book" }]
}
📌 Stage
{ "$replaceRoot": { "newRoot": "$customer" } }
📤 Output Document
{
"name": "Alice",
"email": "alice@example.com"
}
🔧 Common Operators
$mergeObjects,$ifNull,$project
🔗 Related
$replaceWith,$project,$unwind