AWS documentdb: Add $sum window operator usage examples for DocumentDB
Summary
Adds documentation and code examples (MongoDB Shell, Node.js, Python) for using the $sum operator as a window operator in the $setWindowFields stage, including sample documents and running-total examples.
Security assessment
The change is purely functional documentation for the $sum aggregation operator's window usage; it contains no security features, vulnerabilities, credentials, or hardening guidance.
Evidence
+## Window operator usage example (MongoDB Shell)
Diff
diff --git a/documentdb/latest/devguide/sum.md b/documentdb/latest/devguide/sum.md index b560b127f..914d6b70e 100644 --- a//documentdb/latest/devguide/sum.md +++ b//documentdb/latest/devguide/sum.md @@ -7 +7 @@ -Example (MongoDB Shell)Code examples +Example (MongoDB Shell)Window operator usage example (MongoDB Shell)Code examples @@ -51,0 +52,50 @@ The following example demonstrates the use of the `$sum` operator to calculate t +## Window operator usage example (MongoDB Shell) + +New from version 8.0.2. + +The `$sum` operator can also be used as a window operator in the `$setWindowFields` stage. In this context, it returns the sum of the specified expression for the documents in each window. You specify the operator under the `output` field, and optionally define the window boundaries with a `window` document. + +**Create sample documents** + + + db.dailySales.insertMany([ + { _id: 1, product: "abc", day: 1, amount: 10 }, + { _id: 2, product: "abc", day: 2, amount: 20 }, + { _id: 3, product: "abc", day: 3, amount: 15 }, + { _id: 4, product: "xyz", day: 1, amount: 30 }, + { _id: 5, product: "xyz", day: 2, amount: 25 } + ]); + +**Query example** + +The following example partitions the documents by `product`, sorts each partition by `day`, and returns a running total of `amount` from the start of the partition through the current document. + + + db.dailySales.aggregate([ + { + $setWindowFields: { + partitionBy: "$product", + sortBy: { day: 1 }, + output: { + runningTotal: { + $sum: "$amount", + window: { documents: ["unbounded", "current"] } + } + } + } + } + ]); + +**Output** + + + [ + { "_id": 1, "product": "abc", "day": 1, "amount": 10, "runningTotal": 10 }, + { "_id": 2, "product": "abc", "day": 2, "amount": 20, "runningTotal": 30 }, + { "_id": 3, "product": "abc", "day": 3, "amount": 15, "runningTotal": 45 }, + { "_id": 4, "product": "xyz", "day": 1, "amount": 30, "runningTotal": 30 }, + { "_id": 5, "product": "xyz", "day": 2, "amount": 25, "runningTotal": 55 } + ] + +Each document is augmented with `runningTotal`, the cumulative sum of `amount` within its partition up to and including the current document. + @@ -54 +104 @@ The following example demonstrates the use of the `$sum` operator to calculate t -To view a code example for using the `$sum` command, choose the tab for the language that you want to use: +To view a code example for using the `$sum` operator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in `$group`) and window operator usage (in `$setWindowFields`): @@ -70 +119,0 @@ Node.js - const collection = db.collection('sales'); @@ -72 +121,9 @@ Node.js - const result = await collection.aggregate([ + // Accumulator usage: sum per group + const sales = db.collection('sales'); + await sales.insertMany([ + { product: "abc", price: 10, quantity: 2 }, + { product: "abc", price: 10, quantity: 3 }, + { product: "xyz", price: 20, quantity: 1 }, + { product: "xyz", price: 20, quantity: 5 } + ]); + const accumulatorResult = await sales.aggregate([ @@ -78,2 +135,26 @@ Node.js - - console.log(result); + console.log('Accumulator result:', accumulatorResult); + + // Window operator usage: running total within each partition + const dailySales = db.collection('dailySales'); + await dailySales.insertMany([ + { _id: 1, product: "abc", day: 1, amount: 10 }, + { _id: 2, product: "abc", day: 2, amount: 20 }, + { _id: 3, product: "abc", day: 3, amount: 15 }, + { _id: 4, product: "xyz", day: 1, amount: 30 }, + { _id: 5, product: "xyz", day: 2, amount: 25 } + ]); + const windowResult = await dailySales.aggregate([ + { + $setWindowFields: { + partitionBy: "$product", + sortBy: { day: 1 }, + output: { + runningTotal: { + $sum: "$amount", + window: { documents: ["unbounded", "current"] } + } + } + } + } + ]).toArray(); + console.log('Window result:', windowResult); @@ -103 +183,0 @@ Python - collection = db.sales @@ -105 +185,9 @@ Python - result = list(collection.aggregate([ + # Accumulator usage: sum per group + sales = db.sales + sales.insert_many([ + { 'product': 'abc', 'price': 10, 'quantity': 2 }, + { 'product': 'abc', 'price': 10, 'quantity': 3 }, + { 'product': 'xyz', 'price': 20, 'quantity': 1 }, + { 'product': 'xyz', 'price': 20, 'quantity': 5 } + ]) + accumulator_result = list(sales.aggregate([ @@ -111,2 +199,26 @@ Python - - pprint(result) + pprint(accumulator_result) + + # Window operator usage: running total within each partition + daily_sales = db.dailySales + daily_sales.insert_many([ + { '_id': 1, 'product': 'abc', 'day': 1, 'amount': 10 }, + { '_id': 2, 'product': 'abc', 'day': 2, 'amount': 20 }, + { '_id': 3, 'product': 'abc', 'day': 3, 'amount': 15 }, + { '_id': 4, 'product': 'xyz', 'day': 1, 'amount': 30 }, + { '_id': 5, 'product': 'xyz', 'day': 2, 'amount': 25 } + ]) + window_result = list(daily_sales.aggregate([ + { + '$setWindowFields': { + 'partitionBy': '$product', + 'sortBy': { 'day': 1 }, + 'output': { + 'runningTotal': { + '$sum': '$amount', + 'window': { 'documents': ['unbounded', 'current'] } + } + } + } + } + ])) + pprint(window_result)