sort(), limit(), skip() — MongoDB Technical Interview Questions & Answers
Ordering and paging query results.
sort(), limit(), skip()
- Simple meaning: sort orders results, limit takes the first N, skip jumps N (pagination).
- How to use: db.students.find().sort({ marks: -1 }).skip(10).limit(10) — page 2 if page size 10.
- 1 = ascending, -1 = descending.
- Try this: top 3 marks: sort({ marks: -1 }).limit(3).
- Common mistake: huge skip on big collections (slow). Use range on _id or a key for deep pages.
- Interview tip: sort + limit for “top N”. skip for simple pages. Index the sort field.
Technical Interview Tip: Top 3: sort -1 + limit 3.
Code Examples for sort(), limit(), skip()
sort(), limit(), skip() — working example
db.users.find({ active: true })
.sort({ age: -1 })
.skip(10)
.limit(5)sort(), limit(), skip() — sort orders results, skip/limit page them (skip is costly on huge offsets).
How to Explain sort(), limit(), skip() to an Interviewer
How do you get top 3 marks?
find().sort({ marks: -1 }).limit(3)