![]() |
VOOZH | about |
The $limit stage in MongoDB controls how many documents move through the aggregation pipeline, helping reduce processing overhead and optimize query performance.
Syntax:
{ $limit: <positive integer> }These stages are commonly combined with $limit to filter, transform, order, and paginate results efficiently within the aggregation pipeline.
The $limit stage helps control the number of documents returned in an aggregation query, improving efficiency and reducing processing time. Below are practical examples demonstrating how to apply $limit in different scenarios. Let's consider a sample orders collection:
[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alex", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Ben", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Clevin", "total": 100 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a871"), "customer": "David", "total": 75 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a872"), "customer": "Eve", "total": 300 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a873"), "customer": "Frank", "total": 180 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a874"), "customer": "Grace", "total": 220 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a875"), "customer": "Harry", "total": 95 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a876"), "customer": "Ivy", "total": 210 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a877"), "customer": "Jack", "total": 125 }
]
Suppose We want to retrieve only the first three orders from the collection. Here's how we can use the $limit stage to achieve this
db.orders.aggregate([
{ $limit: 3 }
])
Output:
[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alex", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Ben", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Clevin", "total": 100 }
]
Retrieve only the first five orders from the collection. Here's how we can use the $limit stage to achieve this
db.orders.aggregate([
{ $limit: 5 }
])
Output:
[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alex", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Ben", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Clevin", "total": 100 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a871"), "customer": "David", "total": 75 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a872"), "customer": "Eve", "total": 300 }
]
Pagination is a common use of $limit, enabling efficient page-wise data retrieval (e.g., fetching a specific set of customer orders per page).
const pageSize = 10;
// First page
const pageNumber = 1;
db.orders.aggregate([
{ $sort: { _id:1} }, // define order first
{ $skip: (pageNumber-1)*pageSize},
{ $limit: pageSize}
])
Output:
The output will contain the documents corresponding to the specified page of results.
[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alex", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Ben", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Clevin", "total": 100 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a871"), "customer": "David", "total": 75 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a872"), "customer": "Eve", "total": 300 }
]
The $limit stage can be used in various scenarios, including:
To work within the limits of the aggregation pipeline and ensure efficient query execution, developers can employ various optimization techniques:
Creating indexes on frequently queried fields improves the efficiency of $limit queries. Utilizing indexes also reduces the number of documents processed by the pipeline.
db.orders.createIndex({ orderDate: -1 });
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $sort: { orderDate: -1 } },
{ $limit: 5 }
])
Limit the fields returned in output documents using the $project stage to minimize data transfer and processing overhead. By reducing the number of fields, you improve query performance and lower memory usage.
db.orders.aggregate([
{ $project: { customer: 1, total: 1 } },
{ $limit: 5 }
])
Filtering early reduces the number of documents processed. Place $match stages early in the pipeline to filter out irrelevant documents and reduce computation costs.
db.orders.aggregate([
{ $match: { total: { $gt: 100 } } },
{ $limit: 3 }
])
Sorting should be applied before limiting to reduce processing time. Apply $limit stages to restrict the number of documents processed by the pipeline and improve query efficiency.
db.orders.aggregate([
{ $sort: { total: -1 } },
{ $limit: 3 }
]);
Minimize in-memory operations within the pipeline to reduce memory usage and prevent out-of-memory errors. Use indexing, early filtering ($match), and projection ($project) to limit data processing and optimize query performance.