MongoDB Tutorial
- Home
- Introduction
- Installation
- Connection & Databases
- CRUD Operations
- Comparison Operators
- Logical Operators
- Array Operators
- Evaluation Operators
- Update Operators
- Projection & Sorting
- Cursor Methods
- Querying Nested Docs
- Upserts & Indexes
- Aggregation Framework
- Data Modeling
- Validations & Transactions
- Geospatial Queries
- Advanced Indexing
- MongoDB with Next.js
- Best Practices
Cursor Methods
When you run `find()`, MongoDB returns a Cursor. You can chain various methods to this cursor to modify the result set.
1. limit()
Specifies the maximum number of documents the cursor will return.
db.users.find().limit(5)2. skip()
Specifies the number of documents to skip before returning the result.
db.users.find().skip(10)Using Limit & Skip for Pagination
Pagination is the most common use case for these methods.
// Page 1 (items 1-10)
db.users.find().limit(10)
// Page 2 (items 11-20)
db.users.find().skip(10).limit(10)
// Page 3 (items 21-30)
db.users.find().skip(20).limit(10)3. countDocuments()
Returns the count of documents that match the query criteria.
db.users.countDocuments({ status: "active" })4. next() and hasNext()
In scripts or shell, you can manually iterate through a cursor.
const myCursor = db.users.find();
while (myCursor.hasNext()) {
printjson(myCursor.next());
}Performance Note: Using large `skip()` values can be slow because MongoDB still has to scan the skipped documents. For high-performance deep pagination, consider using "Range-based Pagination" (querying based on the last ID).