AWS documentdb documentation change
Summary
Added comprehensive documentation for the $filter operator in Amazon DocumentDB, including parameters, MongoDB Shell example with sample data and query, and code examples in Node.js and Python.
Security assessment
The change is purely a feature documentation addition explaining how to use the $filter operator for array filtering in DocumentDB queries. There is no mention of security vulnerabilities, patches, or security incidents. The code examples include standard TLS connection parameters that are typical for DocumentDB connections, but this is not new security documentation.
Diff
diff --git a/documentdb/latest/developerguide/filter.md b/documentdb/latest/developerguide/filter.md index 8b1378917..b2e4acd8f 100644 --- a//documentdb/latest/developerguide/filter.md +++ b//documentdb/latest/developerguide/filter.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#filter "Open PDF") @@ -1,0 +3,173 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $filter + +The `$filter` operator in Amazon DocumentDB is used to apply a filter expression to each element of an array and return an array containing only the elements that match the specified condition. This operator is useful when you need to perform complex filtering operations on array fields within your documents. + +**Parameters** + + * `input`: The array field to filter. + + * `as`: The variable name to use for each element of the `input` array within the `cond` expression. + + * `cond`: The boolean expression that determines whether a given element should be included in the output array. + + + + +## Example (MongoDB Shell) + +The following example demonstrates how to use the `$filter` operator to project each order's customer and create a new array field paidItems containing only the items from the items array where the price is greater than 15. Essentially, it filters each order’s items to include only products that cost more than 15. + +**Create sample documents** + + + db.orders.insertMany([ + { _id: 1, customer: "abc123", items: [ + { name: "Product A", price: 10, qty: 2 }, + { name: "Product B", price: 20, qty: 1 } + ]}, + { _id: 2, customer: "def456", items: [ + { name: "Product C", price: 5, qty: 3 }, + { name: "Product D", price: 15, qty: 4 } + ]}, + { _id: 3, customer: "ghi789", items: [ + { name: "Product E", price: 8, qty: 3 }, + { name: "Product F", price: 12, qty: 1 } + ]} + ]); + +**Query example** + + + db.orders.aggregate([ + { + $project: { + customer: 1, + paidItems: { + $filter: { + input: "$items", + as: "item", + cond: { $gt: ["$$item.price", 15] } + } + } + } + } + ]).pretty(); + +**Output** + + + [ + { + _id: 1, + customer: 'abc123', + paidItems: [ { name: 'Product B', price: 20, qty: 1 } ] + }, + { _id: 2, customer: 'def456', paidItems: [] }, + { _id: 3, customer: 'ghi789', paidItems: [] } + ] + +## Code examples + +To view a code example for using the `$filter` command, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + async function example() { + const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; + const client = new MongoClient(uri); + + try { + await client.connect(); + + const db = client.db('test'); + const collection = db.collection('orders'); + + const result = await collection.aggregate([ + { + $project: { + customer: 1, + paidItems: { + $filter: { + input: "$items", + as: "item", + cond: { $gt: ["$$item.price", 15] } + } + } + } + } + ]).toArray(); + + console.log(JSON.stringify(result, null, 2)); + + } catch (error) { + console.error('Error:', error); + } finally { + await client.close(); + } + } + + example(); + +Python + + + + from pymongo import MongoClient + from pprint import pprint + + def example(): + uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false' + + with MongoClient(uri) as client: + db = client.test + collection = db.orders + + result = list(collection.aggregate([ + { + '$project': { + 'customer': 1, + 'paidItems': { + '$filter': { + 'input': '$items', + 'as': 'item', + 'cond': { '$gt': ['$$item.price', 15] } + } + } + } + } + ])) + + for doc in result: + pprint(doc) + + example() + + **Javascript is disabled or is unavailable in your browser.** + +To use the Amazon Web Services Documentation, Javascript must be enabled. Please refer to your browser's Help pages for instructions. + +[Document Conventions](/general/latest/gr/docconventions.html) + +$exp + +$first + +Did this page help you? - Yes + +Thanks for letting us know we're doing a good job! + +If you've got a moment, please tell us what we did right so we can do more of it. + +Did this page help you? - No + +Thanks for letting us know this page needs work. We're sorry we let you down. + +If you've got a moment, please tell us how we can make the documentation better.