MongoDB - Limiting Records
MongoDB - Limiting and Paginating Records
Section titled “MongoDB - Limiting and Paginating Records”When querying large datasets, you rarely want to retrieve all documents at once. MongoDB provides cursor methods to limit the number of results, skip over documents, and sort them, which are the building blocks for features like pagination.
The limit() Method
Section titled “The limit() Method”The limit() method specifies the maximum number of documents the cursor will return. It accepts a single numeric argument.
Syntax
Section titled “Syntax”db.COLLECTION_NAME.find(<query>).limit(<number>)Example
Section titled “Example”Imagine a posts collection. To get just the 5 most recent posts, you should combine sort() and limit().
// Assume 'posts' collection with 'title' and 'publishDate' fields.
// Find the 5 most recently published postsdb.posts.find() .sort({ publishDate: -1 }) // -1 for descending order (newest first) .limit(5);Pro Tip: Always use sort() with limit()
Section titled “Pro Tip: Always use sort() with limit()”Without sort(), the order of documents returned from a find() query is not guaranteed. If you use limit() without sort(), you might get a different set of 5 documents each time you run the query. Always provide an explicit sort order to ensure consistent results.
The skip() Method
Section titled “The skip() Method”The skip() method controls the starting point of the results set, telling the cursor to discard a specified number of documents from the beginning.
Syntax
Section titled “Syntax”db.COLLECTION_NAME.find(<query>).skip(<number>)Example: Implementing Pagination
Section titled “Example: Implementing Pagination”You can combine sort(), skip(), and limit() to build a classic pagination system. For example, to display page 3 of our blog, with 10 posts per page:
const page = 3;const postsPerPage = 10;const postsToSkip = (page - 1) * postsPerPage; // (3 - 1) * 10 = 20
db.posts.find() .sort({ publishDate: -1 }) .skip(postsToSkip) .limit(postsPerPage);This query sorts all posts by date, skips the first 20, and then returns the next 10.
Common Pitfall: skip() Performance
Section titled “Common Pitfall: skip() Performance”While skip() is easy to use, it can become slow on very large collections. To skip N documents, the server still has to fetch and traverse all N documents before it can return the result. This is known as the “offset problem.”
For high-performance, “infinite scroll” style pagination, a better approach is keyset pagination (or cursor-based pagination). This involves using a filter on an indexed field (like _id or a timestamp) from the last item of the previous page to fetch the next set of results, which is much more efficient.