AWS documentdb documentation change
Summary
Added documentation for the $first aggregation operator in Amazon DocumentDB, including syntax, parameters, MongoDB shell examples, and code examples in Node.js and Python with connection strings.
Security assessment
This change documents a new aggregation operator ($first) available from version 5.0. The code examples include standard TLS connection parameters (tls=true, tlsCAFile) which are security best practices for DocumentDB connections, but this is not new security documentation - it's standard connection configuration that appears in many DocumentDB examples. There is no evidence this addresses a specific security vulnerability or incident.
Diff
diff --git a/documentdb/latest/developerguide/first.md b/documentdb/latest/developerguide/first.md index 8b1378917..66fec8d31 100644 --- a//documentdb/latest/developerguide/first.md +++ b//documentdb/latest/developerguide/first.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#first "Open PDF") @@ -1,0 +3,136 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $first + +New from version 5.0. + +Not supported by Elastic cluster. + +The `$first` operator in Amazon DocumentDB returns the first document from a grouped set of documents. It is commonly used in aggregation pipelines to retrieve the first document that matches a specific condition. + +**Parameters** + + * `expression`: The expression to return as the first value in each group. + + + + +## Example (MongoDB Shell) + +The following example demonstrates the use of the `$first` operator to retrieve the first item value encountered for each category during the aggregation. + +Note: `$first` returns the first document based on the current order of documents in the pipeline. To ensure a specific order (e.g., by date, price, etc.), a `$sort` stage should be used before the `$group` stage. + +**Create sample documents** + + + db.products.insertMany([ + { _id: 1, item: "abc", price: 10, category: "food" }, + { _id: 2, item: "jkl", price: 20, category: "food" }, + { _id: 3, item: "xyz", price: 5, category: "toy" }, + { _id: 4, item: "abc", price: 5, category: "toy" } + ]); + +**Query example** + + + db.products.aggregate([ + { $group: { _id: "$category", firstItem: { $first: "$item" } } } + ]); + +**Output** + + + [ + { "_id" : "food", "firstItem" : "abc" }, + { "_id" : "toy", "firstItem" : "xyz" } + ] + +## Code examples + +To view a code example for using the `$first` 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('products'); + + const result = await collection.aggregate([ + { $group: { _id: "$category", firstItem: { $first: "$item" } } } + ]).toArray(); + + console.log(result); + + } catch (error) { + console.error('Error:', error); + } finally { + await client.close(); + } + } + + example(); + +Python + + + + from pymongo import MongoClient + from pprint import pprint + + def example(): + client = None + try: + client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') + + db = client['test'] + collection = db['products'] + + result = list(collection.aggregate([ + { '$group': { '_id': '$category', 'firstItem': { '$first': '$item' } } } + ])) + + pprint(result) + + except Exception as e: + print(f"An error occurred: {e}") + + finally: + if client: + client.close() + + 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) + +$filter + +$floor + +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.