AWS documentdb documentation change
Summary
Added new documentation page for the $last operator in Amazon DocumentDB, including syntax, parameters, MongoDB shell example, and code examples in Node.js and Python
Security assessment
This change adds standard documentation for a database operator ($last) with usage examples. The code examples include TLS connection parameters (tls=true, tlsCAFile) which are standard security practices for encrypted connections, but this is not introducing new security documentation - it's just showing standard secure connection patterns that were likely already documented elsewhere. There is no evidence of addressing a specific security vulnerability or incident.
Diff
diff --git a/documentdb/latest/developerguide/last.md b/documentdb/latest/developerguide/last.md index 8b1378917..34ef628d0 100644 --- a//documentdb/latest/developerguide/last.md +++ b//documentdb/latest/developerguide/last.md @@ -0,0 +1 @@ +[](/pdfs/documentdb/latest/developerguide/developerguide.pdf#last "Open PDF") @@ -1,0 +3,184 @@ +[Documentation](/index.html)[Amazon DocumentDB](/documentdb/index.html)[Developer Guide](what-is.html) + +Example (MongoDB Shell)Code examples + +# $last + +The `$last` operator in Amazon DocumentDB is used to return the last element in an array that matches the query criteria. It is particularly useful for retrieving the most recent or the last element in an array that satisfies a specific condition. + +**Parameters** + + * `expression`: The expression to match the array elements. + + + + +## Example (MongoDB Shell) + +The following example demonstrates the use of the `$last` operator in combination with `$filter` to retrieve the last element from an array that meets a specific condition (e.g., subject is 'science'). + +**Create sample documents** + + + db.collection.insertMany([ + { + "_id": 1, + "name": "John", + "scores": [ + { "subject": "math", "score": 82 }, + { "subject": "english", "score": 85 }, + { "subject": "science", "score": 90 } + ] + }, + { + "_id": 2, + "name": "Jane", + "scores": [ + { "subject": "math", "score": 92 }, + { "subject": "english", "score": 88 }, + { "subject": "science", "score": 87 } + ] + }, + { + "_id": 3, + "name": "Bob", + "scores": [ + { "subject": "math", "score": 75 }, + { "subject": "english", "score": 80 }, + { "subject": "science", "score": 85 } + ] + } + ]); + +**Query example** + + + db.collection.aggregate([ + { $match: { name: "John" } }, + { + $project: { + name: 1, + lastScienceScore: { + $last: { + $filter: { + input: "$scores", + as: "score", + cond: { $eq: ["$$score.subject", "science"] } + } + } + } + } + } + ]); + +**Output** + + + [ + { + _id: 1, + name: 'John', + lastScienceScore: { subject: 'science', score: 90 } + } + ] + +## Code examples + +To view a code example for using the `$last` 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('collection'); + + const result = await collection.aggregate([ + { $match: { name: "John" } }, + { + $project: { + name: 1, + lastScienceScore: { + $last: { + $filter: { + input: "$scores", + as: "score", + cond: { $eq: ["$$score.subject", "science"] } + } + } + } + } + } + ]).toArray(); + + console.log(JSON.stringify(result, null, 2)); + await 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.test + collection = db.collection + + pipeline = [ + { "$match": { "name": "John" } }, + { + "$project": { + "name": 1, + "lastScienceScore": { + "$last": { + "$filter": { + "input": "$scores", + "as": "score", + "cond": { "$eq": ["$$score.subject", "science"] } + } + } + } + } + } + ] + + result = list(collection.aggregate(pipeline)) + + 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) + +$isoWeekYear + +$let + +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.