$ROOT
The $ROOT operator in Amazon DocumentDB is used to reference the entire input document within an aggregation pipeline. It allows you to access and manipulate the complete document, including all its nested fields and structures.
Parameters
None
Example (MongoDB Shell)
This example demonstrates using $ROOT to create an audit log that captures the complete original document along with metadata about when it was processed.
Create sample documents
db.orders.insertMany([ { _id: 1, orderId: "ORD-2024-001", customer: "María García", email: "maría@example.com", items: [ { product: "Laptop", quantity: 1, price: 1299.99 } ], totalAmount: 1299.99 }, { _id: 2, orderId: "ORD-2024-002", customer: "Arnav Desai", email: "arnav@example.com", items: [ { product: "Mouse", quantity: 2, price: 29.99 }, { product: "Keyboard", quantity: 1, price: 89.99 } ], totalAmount: 149.97 } ]);
Query example
db.orders.aggregate([ { $project: { processedAt: new Date(), originalDocument: "$$ROOT", summary: { $concat: [ "Order ", "$orderId", " for ", "$customer", " - Total: $", { $toString: "$totalAmount" } ] } } } ]);
Output
[
{
_id: 1,
processedAt: ISODate('2025-11-24T20:43:51.492Z'),
originalDocument: {
_id: 1,
orderId: 'ORD-2024-001',
customer: 'María García',
email: 'maría@example.com',
items: [ { product: 'Laptop', quantity: 1, price: 1299.99 } ],
totalAmount: 1299.99
},
summary: 'Order ORD-2024-001 for María García - Total: $1299.99'
},
{
_id: 2,
processedAt: ISODate('2025-11-24T20:43:51.492Z'),
originalDocument: {
_id: 2,
orderId: 'ORD-2024-002',
customer: 'Arnav Desai',
email: 'arnav@example.com',
items: [
{ product: 'Mouse', quantity: 2, price: 29.99 },
{ product: 'Keyboard', quantity: 1, price: 89.99 }
],
totalAmount: 149.97
},
summary: 'Order ORD-2024-002 for Arnav Desai - Total: $149.97'
}
]
Code examples
To view a code example for using the $ROOT command, choose the tab for the language that you want to use: