$limit and $skip stages in MongoDB - Time & Space Complexity
When using MongoDB, it's important to know how stages like $limit and $skip affect the work the database does.
We want to understand how the time to get results changes as we ask for more or skip more documents.
Analyze the time complexity of the following code snippet.
db.collection.aggregate([
{ $skip: 100 },
{ $limit: 50 }
])
This pipeline skips the first 100 documents and then returns the next 50 documents from the collection.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning documents to skip and then to limit.
- How many times: The database reads documents up to the number skipped plus the number limited.
As the number of documents to skip and limit grows, the work grows roughly by adding those two numbers.
| Input Size (skip + limit) | Approx. Operations |
|---|---|
| 10 + 5 = 15 | About 15 document reads |
| 100 + 50 = 150 | About 150 document reads |
| 1000 + 500 = 1500 | About 1500 document reads |
Pattern observation: The work grows linearly with the total number of documents skipped and limited.
Time Complexity: O(skip + limit)
This means the time to get results grows in a straight line as you increase how many documents you skip plus how many you want to return.
[X] Wrong: "Skipping documents is free and does not affect performance."
[OK] Correct: The database still has to read and count each skipped document, so skipping many documents takes more time.
Understanding how $skip and $limit affect performance helps you explain how queries scale and how to handle large data sets efficiently.
"What if we replaced $skip with a range query filter? How would the time complexity change?"
Practice
$limit stage do in a MongoDB aggregation pipeline?Solution
Step 1: Understand the purpose of
The$limit$limitstage restricts the number of documents that pass through the pipeline to a specified count.Step 2: Compare with other stages
$skipskips documents,$sortorders them, and$groupgroups them. Only$limitrestricts count.Final Answer:
It restricts the number of documents returned. -> Option DQuick Check:
$limit= restrict count [OK]
- Confusing $limit with $skip
- Thinking $limit sorts data
- Mixing $limit with $group
Solution
Step 1: Identify correct order of stages
To skip first, use$skipthen limit results with$limit.Step 2: Match syntax with values
Skipping 5 then limiting to 10 matches[{ $skip: 5 }, { $limit: 10 }].Final Answer:
[{ $skip: 5 }, { $limit: 10 }] -> Option AQuick Check:
Skip first, then limit [OK]
- Reversing $limit and $skip order
- Using wrong numbers for skip and limit
- Confusing syntax with find() options
[{ $skip: 3 }, { $limit: 4 }]Solution
Step 1: Apply
Skipping 3 documents removes 1, 2, 3; remaining are 4 to 10.$skip: 3on documents 1 to 10Step 2: Apply
Limit to 4 documents returns 4, 5, 6, 7.$limit: 4on remaining documentsFinal Answer:
[4, 5, 6, 7] -> Option BQuick Check:
Skip 3, then take 4 [OK]
- Counting skip as inclusive
- Mixing skip and limit values
- Assuming skip resets numbering
[{ $limit: 5 }, { $skip: 3 }]What is the problem?
Solution
Step 1: Understand stage order effect
$limitfirst restricts to 5 documents, then$skiptries to skip 3 from those 5.Step 2: Why it returns only 2 documents
Because$skipafter$limitskips from a smaller set (first 5), not the full collection.Final Answer:
$skipmust come before$limitto work correctly. -> Option CQuick Check:
Skip first, then limit [OK]
- Placing $limit before $skip
- Thinking $skip is invalid in aggregation
- Confusing aggregation with find()
Solution
Step 1: Calculate skip value for page 3
Each page shows 10 documents, so page 3 skips 2 pages x 10 = 20 documents.Step 2: Apply skip and limit in correct order
Skip 20 documents, then limit to 10 documents to get page 3 results.Final Answer:
[{ $skip: 20 }, { $limit: 10 }] -> Option AQuick Check:
Skip (page-1)*size, then limit size [OK]
- Using wrong skip calculation
- Reversing skip and limit order
- Confusing page number with skip count
Ready to prove it?
Pass the test and this pattern moves unknown
