Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
MongoDB query to skip documents
To skip documents in MongoDB, use the skip() method. This method allows you to skip a specified number of documents from the beginning of the result set, useful for pagination and result navigation.
Syntax
db.collection.find().skip(number)
Sample Data
db.demo263.insertMany([
{_id: 100},
{_id: 200},
{_id: 300}
]);
{
"acknowledged": true,
"insertedIds": {
"0": 100,
"1": 200,
"2": 300
}
}
Display all documents from the collection ?
db.demo263.find();
{ "_id": 100 }
{ "_id": 200 }
{ "_id": 300 }
Example: Skip First 2 Documents
db.demo263.find().skip(2);
{ "_id": 300 }
Example: Combining Skip with Limit
Skip the first document and return only 1 document ?
db.demo263.find().skip(1).limit(1);
{ "_id": 200 }
Key Points
-
skip()must be used withfind()or in aggregation pipelines with$skip - Combine with
limit()for effective pagination - Order matters: use
sort()beforeskip()for consistent results
Conclusion
The skip() method efficiently excludes a specified number of documents from query results. Combined with limit(), it provides powerful pagination capabilities for large datasets.
Advertisements
