AWS documentdb documentation change
Summary
Added comprehensive documentation for the $arrayElemAt operator in Amazon DocumentDB, including syntax, parameters, MongoDB shell examples, and code examples in Node.js and Python
Security assessment
This change adds standard documentation for a database operator function. The code examples include TLS connection parameters (tls=true, tlsCAFile) which are security best practices for DocumentDB connections, but these are standard configurations already documented elsewhere. There is no evidence this addresses a specific security vulnerability or incident.
Diff
diff --git a/documentdb/latest/developerguide/arrayElemAt.md b/documentdb/latest/developerguide/arrayElemAt.md index 8b1378917..697624b7c 100644 --- a//documentdb/latest/developerguide/arrayElemAt.md +++ b//documentdb/latest/developerguide/arrayElemAt.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#arrayElemAt "Open PDF") @@ -1,0 +3,124 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $arrayElemAt + +The `$arrayElemAt` operator in Amazon DocumentDB allows you to retrieve an element from an array by its index position. This is particularly useful when you need to access a specific element within an array field in your documents. + +**Parameters** + + * `array`: The input array from which an element is to be retrieved. + + * `index`: The zero-based index position of the element to retrieve. This value must be a non-negative integer. + + + + +## Example (MongoDB Shell) + +In this example, we'll demonstrate how to use the `$arrayElemAt` operator to retrieve specific elements from the `flight_miles` array in the `miles` collection. + +**Create sample documents** + + + db.miles.insertMany([ + { "_id" : 1, "member_since" : ISODate("1987-01-01T00:00:00Z"), "credit_card" : false, "flight_miles" : [ 1205, 2560, 880 ]}, + { "_id" : 2, "member_since" : ISODate("1982-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 1205, 2560, 890, 2780 ]}, + { "_id" : 3, "member_since" : ISODate("1999-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 1205, 880 ]} + ]); + +**Query example** + + + db.miles.aggregate([ + { $project: { + "_id": 1, + "first_mile": { $arrayElemAt: [ "$flight_miles", 0 ] }, + "last_mile": { $arrayElemAt: [ "$flight_miles", -1 ] } + }} + ]); + +**Output** + + + { "_id" : 1, "first_mile" : 1205, "last_mile" : 880 } + { "_id" : 2, "first_mile" : 1205, "last_mile" : 2780 } + { "_id" : 3, "first_mile" : 1205, "last_mile" : 880 } + +In this example, we use the `$arrayElemAt` operator to retrieve the first and last elements of the `flight_miles` array for each document. + +## Code examples + +To view a code example for using the `$arrayElemAt` command, choose the tab for the language that you want to use: + +Node.js + + + + const { MongoClient } = require('mongodb'); + + async function example() { + 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 collection = db.collection('miles'); + + const result = await collection.aggregate([ + { $project: { + "_id": 1, + "first_mile": { $arrayElemAt: [ "$flight_miles", 0 ] }, + "last_mile": { $arrayElemAt: [ "$flight_miles", -1 ] } + }} + ]).toArray(); + + console.log(result); + client.close(); + } + + example(); + +Python + + + + from pymongo import MongoClient + + def example(): + client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') + db = client.mydatabase + collection = db.miles + + result = list(collection.aggregate([ + { "$project": { + "_id": 1, + "first_mile": { "$arrayElemAt": [ "$flight_miles", 0 ] }, + "last_mile": { "$arrayElemAt": [ "$flight_miles", -1 ] } + }} + ])) + + print(result) + 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) + +$anyElementTrue + +$arrayToObject + +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.