Introduction
MongoDB allows you to control how many documents are returned from a query using the limit() method. This is useful when you only need a small number of results, such as the first 5 students, top 3 products, or latest 10 records. In this chapter, you will practice using limit() with find(), filtering, sorting, projection, and other practical MongoDB queries. MongoDB Limiting Results practice questions with solutions to help you understand the concepts.
Q1. Display Only 3 Students
Problem Statement:
Find all students but return only the first 3 documents.
MongoDB Query:
db.students.find().limit(3)
Expected Output:
{ name: "Rahul", age: 16 }
{ name: "Priya", age: 17 }
{ name: "Aman", age: 14 }
Explanation:limit(3) tells MongoDB to return a maximum of 3 documents.
Q2. Display Only 5 Students
Problem Statement:
Find all students but return only 5 documents.
MongoDB Query:
db.students.find().limit(5)
Expected Output:
{ name: "Rahul", age: 16 }
{ name: "Priya", age: 17 }
{ name: "Aman", age: 14 }
{ name: "Neha", age: 18 }
{ name: "Vikas", age: 15 }
Explanation:
The number passed to limit() determines the maximum number of documents returned.
Q3. Find Python Students and Limit the Results to 2
Problem Statement:
Find students enrolled in Python and display only the first 2 matching documents.
MongoDB Query:
db.students.find({
course: "Python"
}).limit(2)
Expected Output:
{ name: "Rahul", course: "Python" }
{ name: "Aman", course: "Python" }
Explanation:
The query first filters students whose course is Python and then limits the returned results to 2.
Q4. Find the 3 Youngest Students
Problem Statement:
Find the three youngest students.
MongoDB Query:
db.students.find().sort({
age: 1
}).limit(3)
Expected Output:
{ name: "Aman", age: 14 }
{ name: "Vikas", age: 15 }
{ name: "Rahul", age: 16 }
Explanation:
.sort({ age: 1 })
sorts students from youngest to oldest.
.limit(3)
then returns only the first three students.
Q5. Find the Top 3 Students by Marks
Problem Statement:
Find the three students who have the highest marks.
MongoDB Query:
db.students.find().sort({
marks: -1
}).limit(3)
Expected Output:
{ name: "Neha", marks: 95 }
{ name: "Rahul", marks: 91 }
{ name: "Priya", marks: 87 }
Explanation:marks: -1 sorts marks from highest to lowest, and limit(3) selects the top three results.
Q6. Display Only 2 Products Above ₹1000
Problem Statement:
Find products whose price is greater than 1000, but return only 2 products.
MongoDB Query:
db.products.find({
price: { $gt: 1000 }
}).limit(2)
Expected Output:
{ name: "Keyboard", price: 1200 }
{ name: "Monitor", price: 8000 }
Explanation:
The filter:
{ price: { $gt: 1000 } }
finds products costing more than 1000.
limit(2) restricts the result to two documents.
Q7. Find the 5 Most Expensive Products
Problem Statement:
Find the five products with the highest prices.
MongoDB Query:
db.products.find().sort({
price: -1
}).limit(5)
Expected Output:
{ name: "Laptop", price: 55000 }
{ name: "Monitor", price: 8000 }
{ name: "Keyboard", price: 1200 }
{ name: "Mouse", price: 500 }
Explanation:
The descending sort places the most expensive products first. limit(5) returns a maximum of five products.
If the collection contains fewer than five matching documents, MongoDB returns whatever is available.
Q8. Limit Results with Projection
Problem Statement:
Find students and return only their names and ages. Display a maximum of 3 students.
MongoDB Query:
db.students.find(
{},
{
_id: 0,
name: 1,
age: 1
}
).limit(3)
Expected Output:
{ name: "Rahul", age: 16 }
{ name: "Priya", age: 17 }
{ name: "Aman", age: 14 }
Explanation:
This query uses:
- Projection → returns only
nameandage limit(3)→ returns a maximum of 3 documents
Q9. Find the 2 Oldest Students from Delhi
Problem Statement:
Find students living in Delhi and return the two oldest students.
MongoDB Query:
db.students.find({
"address.city": "Delhi"
}).sort({
age: -1
}).limit(2)
Expected Output:
{
name: "Neha",
age: 18,
address: {
city: "Delhi"
}
}
{
name: "Rahul",
age: 16,
address: {
city: "Delhi"
}
}
Explanation:
The query:
- Finds students from Delhi.
- Sorts them by age in descending order.
- Returns only the first two documents.
Q10. Get the Latest 3 Students by Registration Date
Problem Statement:
Suppose each student has a registeredAt field. Find the three students who registered most recently.
MongoDB Query:
db.students.find().sort({
registeredAt: -1
}).limit(3)
Expected Output:
{
name: "Neha",
registeredAt: ISODate("2026-09-12")
}
{
name: "Aman",
registeredAt: ISODate("2026-09-10")
}
{
name: "Rahul",
registeredAt: ISODate("2026-09-08")
}
Explanation:
Sorting the date in descending order puts the newest dates first. limit(3) then returns the three latest records.
Key Takeaways
- MongoDB uses the
limit()method to restrict the number of returned documents. limit(3)returns a maximum of 3 documents.limit()in MongoDB can be combined withfind().- You can use
limit()after filtering documents. limit()in MongoDB works well withsort()for finding top or bottom results.sort()+limit()is useful for finding top students, cheapest products, latest records, and more.- Projection can be combined with
limit()in MongoDB to control both fields and document count. limit()in MongoDBdoes not delete or modify documents.- If fewer documents are available than the specified limit, MongoDB returns the available documents.
FAQs
1. What is limit() in MongoDB?
limit() is a MongoDB cursor method used to restrict the maximum number of documents returned by a query.
db.students.find().limit(3)
This returns a maximum of 3 documents.
2. How do you limit in MongoDB query results?
Use the limit() method:
db.students.find().limit(5)
3. Can limit() in MongoDB be used with a filter?
Yes. For example:
db.students.find({
course: "Python"
}).limit(2)
This returns a maximum of two Python students.
4. Can sort() and limit() in MongoDB be used together?
Yes. This is one of the most common MongoDB patterns.
db.students.find().sort({
marks: -1
}).limit(3)
This finds the top three students by marks.
5. Does limit() delete documents from MongoDB?
No. limit() only controls how many documents are returned in the query result. The original documents remain unchanged in the collection.
6. What happens if limit() is greater than the number of documents?
MongoDB returns all available matching documents.
For example:
db.students.find().limit(100)
If only 20 matching documents exist, MongoDB returns those 20 documents.
7. Can limit() be used with projection?
Yes. You can use filtering, projection, sorting, and limiting together.
db.students.find(
{ course: "Python" },
{ _id: 0, name: 1, marks: 1 }
).sort({
marks: -1
}).limit(3)
This returns the top three Python students while displaying only their name and marks.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
