![]() |
VOOZH | about |
The MongoDB limit() method restricts the number of documents returned by a query, similar to the SQL LIMIT clause. It accepts a single numeric argument to specify how many documents should be displayed, helping improve query efficiency and manage result size.
Syntax:
db.collectionName.find(<query>).limit(<number>)
In the above syntax:
To better understand how the limit() method works, let's look at some practical examples. We will use a collection named gfg from a MongoDB database geeksforgeeks, which contains documents with a content field.
db.gfg.find().limit(2)Output:
👁 ImageExplanation:
gfg collection. db.gfg.find({"content":/c/i}).limit(2)Output:
👁 ImageExplanation:
db.gfg.find({"content":/c/i}).limit(3)Output:
Explanation:
The cursor.limit() method in MongoDB is used to restrict the number of documents returned by a query. It is applied to a cursor object, which is the result of a find() query.
Syntax:
cursor.limit(<number>);In the above syntax:
Example: Suppose we have a students collection:
db.students.insertMany([
{ name: "Alice", age: 20 },
{ name: "Bob", age: 22 },
{ name: "Charlie", age: 21 },
{ name: "David", age: 23 },
{ name: "Eve", age: 20 }
]);
Using cursor.limit():
const cursor = db.students.find({ age: { $gt: 20 } });
cursor.limit(2);
cursor.forEach(doc => printjson(doc));
Output:
{ "_id": ObjectId("..."), "name": "Bob", "age": 22 }
{ "_id": ObjectId("..."), "name": "Charlie", "age": 21 }
In this example:
In MongoDB, the $limit stage in an aggregation pipeline restricts how many documents are passed on to the next stage. It accepts a single positive integer that represents the maximum number of documents to return.
Syntax:
{ $limit: <positive integer> }In the above syntax:
Example: Suppose we have a students collection:
db.students.insertMany([
{ name: "Alice", age: 20, score: 85 },
{ name: "Bob", age: 22, score: 90 },
{ name: "Charlie", age: 21, score: 88 },
{ name: "David", age: 23, score: 92 },
{ name: "Eve", age: 20, score: 87 }
]);
Using $limit in an aggregation pipeline:
db.students.aggregate([
{ $sort: { score: -1 } }, // Sort by score descending
{ $limit: 3 } // Limit to top 3 documents
]);
Output:
{ "_id": ObjectId("..."), "name": "David", "age": 23, "score": 92 }
{ "_id": ObjectId("..."), "name": "Bob", "age": 22, "score": 90 }
{ "_id": ObjectId("..."), "name": "Charlie", "age": 21, "score": 88 }
Explanation:
While the limit() method is a great tool for improving query performance, there are some best practices to consider:
1. Use Indexes: Index fields used in find(), sort(), and limit() to fetch results quickly without scanning the full collection.
2. Handle Large Result Sets: For big datasets, use allowDiskUse(true) to let MongoDB spill results to disk:
db.gfg.find().limit(1000).allowDiskUse(true)
3. Limit Meaningfully: Ensure limited results are relevant and not truncating important data.
4. Pagination: Combine limit() with skip() to fetch pages of data:
db.gfg.find().skip(10).limit(10)limit() Method The limit() method in MongoDB allows developers to: