Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
MongoDB Aggregation Pipeline $limit
Next article icon

MongoDB Aggregation Pipeline $limit

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The MongoDB aggregation pipeline is a powerful tool for data processing and transformation, allowing users to perform efficient filtering, sorting, grouping, and reshaping of documents. Among its various stages, the $limit stage is essential for restricting the number of documents that flow through the pipeline, thereby improving performance and optimizing query execution.

In this article, we'll learn about the $limit stage of the aggregation pipeline by exploring its concepts, usage, and practical examples in detail. By the end, we will understand how to use $limit effectively to enhance query performance and implement pagination in MongoDB.

What is the $limit Stage in MongoDB?

The $limit stage in MongoDB restricts the number of documents that are passed to the next stage in the aggregation pipeline. This is useful for controlling result size, reducing data processing overhead, and improving query efficiency.

Key Features of $limit

  • It restricts the number of documents that pass through the pipeline.
  • Useful for limiting the amount of data processed or returned in an aggregation query.
  • It is used to reduce the number of documents processed in the pipeline, improving query performance.
  • $limit can be used at any stage of the pipeline but is commonly used towards the end to limit the final result set.
  • It takes a single argument, which is the maximum number of documents to return.

Syntax:

The basic syntax of the $limit stage in the aggregation pipeline is as follows:

{ $limit: <positive integer> }

Key Terms

  • $limit: The keyword indicating the stage.
  • <positive integer>: The maximum number of documents to output from the pipeline.

Common Aggregation Pipeline Stages Used with $limit

  • $match: Filters documents based on conditions before limiting them.
  • $group: Groups documents by a specified key and performs aggregation operations.
  • $project: Reshapes documents by including, excluding, or renaming fields.
  • $sort: Sorts documents based on specified fields.
  • $skip: Skips a number of documents before applying $limit (useful for pagination).

Example of Using $limit in MongoDB

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": "Alice", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Bob", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Charlie", "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 }
]

Example 1: Retrieve the First 3 Orders

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

Query:

db.orders.aggregate([
{ $limit: 3 }
]);

Output:

[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alice", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Bob", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Charlie", "total": 100 }
]

Explanation:

The aggregation pipeline is initiated with db.orders.aggregate([]), and the $limit stage ensures that only the first three documents from the collection are passed to the next stage. This reduces processing time and network load by returning only a subset of the total dataset.

Example 2: Retrieve the First 5 Orders

Suppose We want to retrieve only the first five orders from the collection. Here's how we can use the $limit stage to achieve this

Query:

db.orders.aggregate([
{ $limit: 5 }
])

Output:

[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alice", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Bob", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Charlie", "total": 100 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a871"), "customer": "David", "total": 75 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a872"), "customer": "Eve", "total": 300 }
]

Explanation:

Here, the $limit stage restricts the output to the first five documents. This is useful when we need only a limited number of results, such as displaying the top five most recent orders or fetching limited data for performance optimization.

Example 3: Implement Pagination using $limit and $skip

Pagination is a common use case for $limit, allowing efficient data retrieval in a paginated format. Suppose we want to implement pagination for a web application displaying customer orders. We can use the $limit stage to retrieve a specific page of results

Query:

const pageSize = 10;
const pageNumber = 1; // First page

db.orders.aggregate([
{ $skip: (pageNumber - 1) * pageSize }, // Skip documents on previous pages
{ $limit: pageSize } // Limit results to current page size
])

Output

The output will contain the documents corresponding to the specified page of results.

[
{ "_id": ObjectId("60f9d7ac345b7c9df348a86e"), "customer": "Alice", "total": 150 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a86f"), "customer": "Bob", "total": 200 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a870"), "customer": "Charlie", "total": 100 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a871"), "customer": "David", "total": 75 },
{ "_id": ObjectId("60f9d7ac345b7c9df348a872"), "customer": "Eve", "total": 300 }
]

Explanation:

In this example, we calculate the number of documents to skip based on the desired page number and page size, then apply the $limit stage to retrieve the specified number of documents for the current page.

Benefits of Using $limit in MongoDB

The $limit stage can be used in various scenarios, including:

✅ Enhances Performance: Reducing the amount of data processed in complex aggregation pipelines to improve query performance.

✅ Pagination Support: Limiting the number of results returned for paginated queries. It is Essential for building paginated APIs and web applications.

✅ Efficient Sampling: Retrieves only a subset of documents for analysis or testing.

✅ Reduces Network Load: Minimizes data transfer between MongoDB and the application.

Optimizing Techniques for $limit in Aggregation Pipelines

To work within the limits of the aggregation pipeline and ensure efficient query execution, developers can employ various optimization techniques:

1. Use Indexes for Faster Query Execution

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 }
]);

2. Projection Optimization

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 }
]);

3. Apply $match Before $limit

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 }
]);

4. Optimize $sort Before $limit

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 }
]);

4. Avoiding In-Memory Operations

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.

Conclusion

Overall, Understanding the limits of the aggregation pipeline in MongoDB is essential for designing efficient queries and optimizing query performance. By being aware of document size limits, pipeline stage limits, memory limits, and time limits, developers can effectively design and execute aggregation pipeline queries within the constraints of the MongoDB environment. By following these best practices, we can maximize MongoDB’s aggregation pipeline efficiency and build high-performance applications


Next Article
MongoDB Aggregation Pipeline $limit

K

kumarsar29u2
Improve
Article Tags :
  • MongoDB
  • Databases

Similar Reads

    Aggregation Pipeline Optimization
    MongoDB's aggregation pipeline is a powerful tool for data transformation, filtering and analysis enabling users to process documents efficiently in a multi-stage pipeline. However, when dealing with large datasets, it is crucial to optimize the MongoDB aggregation pipeline to ensure fast query exec
    6 min read
    Aggregation Pipeline Stages in MongoDB - Set 2
    In MongoDB, the Aggregation Pipeline is a powerful framework for processing and transforming data through several stages. Each stage performs a specific operation on the data, allowing for complex queries and aggregations. By linking multiple stages in sequence, users can effectively process and ana
    14 min read
    Aggregation Pipeline Stages in MongoDB - Set 1
    MongoDB aggregation pipeline is a powerful framework for data processing that allows documents to perform sequential transformations like filtering, grouping, and reshaping. In this article, We will learn about various Aggregation Pipeline Stages in MongoDB with the help of examples and so on.Aggreg
    8 min read
    Aggregation in MongoDB
    Aggregation in MongoDB is a powerful framework that allows developers to perform complex data transformations, computations and analysis on collections of documents. By utilizing the aggregation pipeline, users can efficiently group, filter, sort, reshape, and perform calculations on data to generat
    7 min read
    MongoDB Aggregation $first Operator
    MongoDB's aggregation framework offers a rich set of operators for data manipulation and analysis. One such operator is $first, which retrieves the first document from a group of documents. In this article, we will learn about the $first operator in MongoDB aggregation by covering its concepts and u
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences