$ROOT - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$ROOT

Amazon DocumentDB의 $ROOT 연산자는 집계 파이프라인 내의 전체 입력 문서를 참조하는 데 사용됩니다. 이를 통해 중첩된 모든 필드 및 구조를 포함하여 전체 문서에 액세스하고 조작할 수 있습니다.

파라미터

없음

예제(MongoDB 쉘)

이 예제에서는 $ROOT를 사용하여 처리 시기에 대한 메타데이터와 함께 전체 원본 문서를 캡처하는 감사 로그를 생성하는 방법을 보여줍니다.

샘플 문서 생성

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 } ]);

쿼리 예제

db.orders.aggregate([ { $project: { processedAt: new Date(), originalDocument: "$$ROOT", summary: { $concat: [ "Order ", "$orderId", " for ", "$customer", " - Total: $", { $toString: "$totalAmount" } ] } } } ]);

출력

[ { _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' } ]

코드 예제

$ROOT 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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()