AWS documentdb: Document $push as window operator in $setWindowFields
Summary
Adds a new section documenting the $push operator used as a window operator in the $setWindowFields stage (new in version 8.0.2), including sample documents, a MongoDB Shell query example, expected output, and a 100 MB intermediate-data limit note. Also updates the Node.js and Python code examples to show both accumulator ($group) and window operator usage, and adjusts the intro sentence and tab labels accordingly.
Security assessment
The change is purely functional documentation of an aggregation operator's window-operator behavior and example code; it contains no authentication, authorization, encryption, credential, or vulnerability-related content. The only limit mentioned (100 MB of intermediate data) is a resource/performance constraint, not a security control.
Evidence
+New from version 8.0.2.
Diff
diff --git a/documentdb/latest/devguide/push-aggregation.md b/documentdb/latest/devguide/push-aggregation.md index 903ac96be..2cb50f349 100644 --- a//documentdb/latest/devguide/push-aggregation.md +++ b//documentdb/latest/devguide/push-aggregation.md @@ -7 +7 @@ -Example (MongoDB Shell)Code examples +Example (MongoDB Shell)Window operator usage example (MongoDB Shell)Code examples @@ -54,0 +55,54 @@ The following example demonstrates using the `$push` operator to collect all pro +## Window operator usage example (MongoDB Shell) + +New from version 8.0.2. + +The `$push` operator can also be used as a window operator in the `$setWindowFields` stage. In this context, it returns an array of all values of the specified expression for the documents in each window, in sort order. You specify the operator under the `output` field, and optionally define the window boundaries with a `window` document. + +###### Note + +When used as a window operator in `$setWindowFields`, `$push` is limited to 100 MB of intermediate data. An operation that exceeds this limit returns an error. + +**Create sample documents** + + + db.purchases.insertMany([ + { _id: 1, category: "Electronics", day: 1, product: "Laptop" }, + { _id: 2, category: "Electronics", day: 2, product: "Mouse" }, + { _id: 3, category: "Electronics", day: 3, product: "Keyboard" }, + { _id: 4, category: "Furniture", day: 1, product: "Desk" }, + { _id: 5, category: "Furniture", day: 2, product: "Chair" } + ]); + +**Query example** + +The following example partitions the documents by `category`, sorts each partition by `day`, and returns a running list of `product` values from the start of the partition through the current document. + + + db.purchases.aggregate([ + { + $setWindowFields: { + partitionBy: "$category", + sortBy: { day: 1 }, + output: { + productsSoFar: { + $push: "$product", + window: { documents: ["unbounded", "current"] } + } + } + } + } + ]); + +**Output** + + + [ + { "_id": 1, "category": "Electronics", "day": 1, "product": "Laptop", "productsSoFar": [ "Laptop" ] }, + { "_id": 2, "category": "Electronics", "day": 2, "product": "Mouse", "productsSoFar": [ "Laptop", "Mouse" ] }, + { "_id": 3, "category": "Electronics", "day": 3, "product": "Keyboard", "productsSoFar": [ "Laptop", "Mouse", "Keyboard" ] }, + { "_id": 4, "category": "Furniture", "day": 1, "product": "Desk", "productsSoFar": [ "Desk" ] }, + { "_id": 5, "category": "Furniture", "day": 2, "product": "Chair", "productsSoFar": [ "Desk", "Chair" ] } + ] + +Each document is augmented with `productsSoFar`, the array of all `product` values within its partition from the start through the current document, in sort order. + @@ -57 +111 @@ The following example demonstrates using the `$push` operator to collect all pro -To view a code example for using the `$push` aggregation operator, choose the tab for the language that you want to use: +To view a code example for using the `$push` 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`): @@ -68 +121,0 @@ Node.js - const collection = db.collection('sales'); @@ -70 +123,10 @@ Node.js - const result = await collection.aggregate([ + // Accumulator usage: collect all values per group + const sales = db.collection('sales'); + await sales.insertMany([ + { _id: 1, category: "Electronics", product: "Laptop", amount: 1200 }, + { _id: 2, category: "Electronics", product: "Mouse", amount: 25 }, + { _id: 3, category: "Furniture", product: "Desk", amount: 350 }, + { _id: 4, category: "Furniture", product: "Chair", amount: 150 }, + { _id: 5, category: "Electronics", product: "Keyboard", amount: 75 } + ]); + const accumulatorResult = await sales.aggregate([ @@ -77,0 +140,26 @@ Node.js + console.log('Accumulator result:', accumulatorResult); + + // Window operator usage: running list of values within each partition + const purchases = db.collection('purchases'); + await purchases.insertMany([ + { _id: 1, category: "Electronics", day: 1, product: "Laptop" }, + { _id: 2, category: "Electronics", day: 2, product: "Mouse" }, + { _id: 3, category: "Electronics", day: 3, product: "Keyboard" }, + { _id: 4, category: "Furniture", day: 1, product: "Desk" }, + { _id: 5, category: "Furniture", day: 2, product: "Chair" } + ]); + const windowResult = await purchases.aggregate([ + { + $setWindowFields: { + partitionBy: "$category", + sortBy: { day: 1 }, + output: { + productsSoFar: { + $push: "$product", + window: { documents: ["unbounded", "current"] } + } + } + } + } + ]).toArray(); + console.log('Window result:', windowResult); @@ -79 +166,0 @@ Node.js - console.log(result); @@ -94 +180,0 @@ Python - collection = db['sales'] @@ -96 +182,10 @@ Python - result = list(collection.aggregate([ + # Accumulator usage: collect all values per group + sales = db['sales'] + sales.insert_many([ + { '_id': 1, 'category': 'Electronics', 'product': 'Laptop', 'amount': 1200 }, + { '_id': 2, 'category': 'Electronics', 'product': 'Mouse', 'amount': 25 }, + { '_id': 3, 'category': 'Furniture', 'product': 'Desk', 'amount': 350 }, + { '_id': 4, 'category': 'Furniture', 'product': 'Chair', 'amount': 150 }, + { '_id': 5, 'category': 'Electronics', 'product': 'Keyboard', 'amount': 75 } + ]) + accumulator_result = list(sales.aggregate([ @@ -103,0 +199,26 @@ Python + print('Accumulator result:', accumulator_result) + + # Window operator usage: running list of values within each partition + purchases = db['purchases'] + purchases.insert_many([ + { '_id': 1, 'category': 'Electronics', 'day': 1, 'product': 'Laptop' }, + { '_id': 2, 'category': 'Electronics', 'day': 2, 'product': 'Mouse' }, + { '_id': 3, 'category': 'Electronics', 'day': 3, 'product': 'Keyboard' }, + { '_id': 4, 'category': 'Furniture', 'day': 1, 'product': 'Desk' }, + { '_id': 5, 'category': 'Furniture', 'day': 2, 'product': 'Chair' } + ]) + window_result = list(purchases.aggregate([ + { + '$setWindowFields': { + 'partitionBy': '$category', + 'sortBy': { 'day': 1 }, + 'output': { + 'productsSoFar': { + '$push': '$product', + 'window': { 'documents': ['unbounded', 'current'] } + } + } + } + } + ])) + print('Window result:', window_result) @@ -105 +225,0 @@ Python - print(result)