![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is there any way to skip some documents in MongoDB?
Yes, you can skip some documents using skip() in MongoDB. Use limit() to display how many documents you want to display after skipping some. Let us create a collection with documents −
> db.demo682.insertOne({FirstName:"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462a804263e90dac94402") } > db.demo682.insertOne({FirstName:"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462ac04263e90dac94403") } > db.demo682.insertOne({FirstName:"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462af04263e90dac94404") } > db.demo682.insertOne({FirstName:"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462b304263e90dac94405") } > db.demo682.insertOne({FirstName:"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462ba04263e90dac94406") } > db.demo682.insertOne({FirstName:"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462be04263e90dac94407") } > db.demo682.insertOne({FirstName:"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462c404263e90dac94408") }
Display all documents from a collection with the help of find() method −
> db.demo682.find();
This will produce the following output −
{ "_id" : ObjectId("5ea462a804263e90dac94402"), "FirstName" : "John" } { "_id" : ObjectId("5ea462ac04263e90dac94403"), "FirstName" : "Sam" } { "_id" : ObjectId("5ea462af04263e90dac94404"), "FirstName" : "Bob" } { "_id" : ObjectId("5ea462b304263e90dac94405"), "FirstName" : "David" } { "_id" : ObjectId("5ea462ba04263e90dac94406"), "FirstName" : "Adam" } { "_id" : ObjectId("5ea462be04263e90dac94407"), "FirstName" : "Chris" } { "_id" : ObjectId("5ea462c404263e90dac94408"), "FirstName" : "Carol" }
Following is the query to skip 3 documents and display 2 −
> db.demo682.find().skip(3).limit(2);
This will produce the following output −
{ "_id" : ObjectId("5ea462b304263e90dac94405"), "FirstName" : "David" } { "_id" : ObjectId("5ea462ba04263e90dac94406"), "FirstName" : "Adam" }
Advertisements