$limit is a MongoDB aggregation stage that keeps only the first N documents from the current pipeline and drops the rest—essential for top-10 lists, paginated APIs, and trimming results early.
Remember
$limit = "keep only the first N"
{ $limit: 5 }
does NOT sort by itself
for top-N → $sort then $limit
for pages → $sort → $skip → $limit
fewer than N docs → you get all of them
{ $limit: 0 } → empty result
find().limit(N) → similar idea outside aggregation
Intro to MongoDB 12400 published
Aggregation Basics 8900 published
Node.js + MongoDB 15200 published
Indexing Strategies 6700 published
Draft: Sharding 0 draft
REST API Design 11300 published
Schema Validation 5400 published
Express Middleware 7800 published
Read the important fields like this:
Field
Example
Plain English
views
15200
Sort key for top-N
published
true
Filter before limiting
title
"REST API Design"
What we show in results
How It Works
Reset once.drop() keeps demos repeatable.
Mixed publish state. One draft is excluded by later $match.
Index on views. Helps $sort + $limit for top-N.
Example 2 — Top N
Goal: published articles only, highest views first, keep three. This is the classic leaderboard pattern.
Same order every page.$sort first so pages do not reshuffle.
Skip the previous page. Page 2 skips the first 3 rows.
Limit the page size. Keep the next 3 only.
Large skips. Very high offsets get expensive—use cursors for deep pages.
Edge Cases & Pitfalls
Check these before shipping list and page APIs with $limit.
order
$limit does not sort
Without $sort, “first N” is pipeline order—not highest, newest, or best.
zero
$limit: 0 returns nothing
A limit of zero is valid and yields an empty cursor. Guard bad page sizes in your app.
skip
Large $skip is costly
Skipping thousands of rows still reads them. Prefer range/cursor pagination for deep pages.
ties
Add a tie-breaker for stable pages
Equal views can shuffle between pages. Sort by views then _id (or createdAt) for consistency.
Analysis
Performance Notes
Approach
Cost
Best for
$sort + $limit (indexed)
Top-k friendly
Leaderboards / top-N
$match then limit
Fewer docs early
Filtered lists
Huge $skip
Reads skipped rows
Avoid for deep pages
Put selective $match first, keep $sort next to $limit for top-N, and index the sort field when lists are hot.
Remember
Key Takeaways
Role: keep the first N documents; drop the rest.
Top-N:$sort then $limit.
Pages:$sort → $skip → $limit.
Reminder: limit alone does not define ranking.
One line:$limit answers “only keep the first N from here.”
Frequently Asked Questions
$limit passes only the first N documents from its input to the next stage and discards the rest. Use it to cap result size—top 10 after $sort, or one page of a paginated API.
{ $limit: <positive integer> }. Example: { $limit: 5 } returns at most five documents. If fewer than N documents arrive, you get all of them.
Yes for meaningful top-N (newest, highest score). Without $sort, $limit returns an arbitrary first N in pipeline order—rarely what you want for ranked lists.
Combine $skip and $limit after $sort. Page 1: skip 0, limit 20; page 2: skip 20, limit 20. For very large offsets, prefer cursor-based pagination over high $skip values.
$limit keeps the first N in current order. $sample picks N documents randomly. Use $limit for ranked or ordered slices; $sample for random subsets.
It can—especially with adjacent $sort + $limit (top-k) or after selective $match. Placing $limit early reduces work for later stages when ordering is already correct.