$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: