View a markdown version of this page

$lastN - Amazon DocumentDB

$lastN

New from version 8.0.1.

The $lastN operator in Amazon DocumentDB returns the last N elements. When used as an accumulator in a $group stage, it returns an array of the last N values in each group. When used as an array expression operator, it returns the last N elements of an array.

Parameters

  • input: The expression that resolves to the field or array from which to return values.

  • n: A positive integer that specifies how many values to return. When used as an accumulator in a $group stage, n can also be an expression, as long as it resolves to a positive integer based on the group _id field.

Example (MongoDB Shell)

The following example shows how to use the $lastN accumulator to retrieve the last two quantities for each item during the aggregation.

Note

$lastN selects values in the order that documents reach the $group stage. To return the last N values for a specific ordering (for example, by date or score), add a $sort stage before $group.

Create sample documents

db.sales.insertMany([ { item: "abc", quantity: 10, date: ISODate("2023-01-01") }, { item: "abc", quantity: 5, date: ISODate("2023-01-02") }, { item: "abc", quantity: 8, date: ISODate("2023-01-03") }, { item: "xyz", quantity: 15, date: ISODate("2023-01-01") }, { item: "xyz", quantity: 7, date: ISODate("2023-01-02") }, { item: "xyz", quantity: 3, date: ISODate("2023-01-03") } ]);

Query example

db.sales.aggregate([ { $group: { _id: "$item", lastTwoQuantities: { $lastN: { input: "$quantity", n: 2 } } } } ]);

Output

[ { "_id": "abc", "lastTwoQuantities": [5, 8] }, { "_id": "xyz", "lastTwoQuantities": [7, 3] } ]

Expression usage example (MongoDB Shell)

The $lastN operator can also be used as an expression within a $project stage to return the last N elements of an array field.

Create sample documents

db.inventory.insertMany([ { _id: 1, item: "abc", tags: ["red", "green", "blue", "yellow", "purple"] }, { _id: 2, item: "xyz", tags: ["alpha", "beta", "gamma"] } ]);

Query example

db.inventory.aggregate([ { $project: { lastThreeTags: { $lastN: { input: "$tags", n: 3 } } }} ]);

Output

[ { "_id": 1, "lastThreeTags": ["blue", "yellow", "purple"] }, { "_id": 2, "lastThreeTags": ["alpha", "beta", "gamma"] } ]

Code examples

To view a code example for using the $lastN accumulator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and expression usage (in $project):

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); // Accumulator usage: last N values per group const sales = db.collection('sales'); const accumulatorResult = await sales.aggregate([ { $group: { _id: "$item", lastTwoQuantities: { $lastN: { input: "$quantity", n: 2 } } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Expression usage: last N elements of an array field const inventory = db.collection('inventory'); const expressionResult = await inventory.aggregate([ { $project: { lastThreeTags: { $lastN: { input: "$tags", n: 3 } } } } ]).toArray(); console.log('Expression result:', expressionResult); } finally { 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') try: db = client['test'] # Accumulator usage: last N values per group sales = db['sales'] accumulator_result = list(sales.aggregate([ { '$group': { '_id': '$item', 'lastTwoQuantities': { '$lastN': { 'input': '$quantity', 'n': 2 } } } } ])) print('Accumulator result:', accumulator_result) # Expression usage: last N elements of an array field inventory = db['inventory'] expression_result = list(inventory.aggregate([ { '$project': { 'lastThreeTags': { '$lastN': { 'input': '$tags', 'n': 3 } } } } ])) print('Expression result:', expression_result) finally: client.close() example()