$ROOT - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function createAuditLog() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const orders = db.collection('orders'); const result = await orders.aggregate([ { $project: { processedAt: new Date(), originalDocument: "$$ROOT", summary: { $concat: [ "Order ", "$orderId", " for ", "$customer", " - Total: $", { $toString: "$totalAmount" } ] } } } ]).toArray(); console.log(result); await client.close(); } createAuditLog();
Python
from pymongo import MongoClient from datetime import datetime def create_audit_log(): client = MongoClient('mongodb://username:password@docdb-cluster.cluster-123456789.us-east-1.docdb.amazonaws.com:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['orders'] result = list(collection.aggregate([ { '$project': { 'processedAt': datetime.now(), 'originalDocument': '$$ROOT', 'summary': { '$concat': [ "Order ", "$orderId", " for ", "$customer", " - Total: $", { '$toString': "$totalAmount" } ] } } } ])) print(result) client.close() create_audit_log()