- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to skip documents while retrieving data from MongoDB, using java?
While retrieving records from a MongoDB collection, you can skip records in the result using the skip() method.
Syntax
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
The Java MongoDB library provides a method with the same name, to skip records, invoke this method (on the result of the find() method) bypassing an integer value representing the number of records to skip.
Example
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class SkipRecords { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("myDatabase"); //Creating a collection object MongoCollection<Document>collection = database.getCollection("students"); Document document1 = new Document("name", "Ram").append("age", 26).append("city", "Hyderabad"); Document document2 = new Document("name", "Robert").append("age", 27).append("city", "Vishakhapatnam"); Document document3 = new Document("name", "Rhim").append("age", 30).append("city", "Delhi"); Document document4 = new Document("name", "Radha").append("age", 28).append("city", "Mumbai"); Document document5 = new Document("name", "Ramani").append("age", 45).append("city", "Pune"); //Inserting the created documents List<Document> list = new ArrayList<Document>(); list.add(document1); list.add(document2); list.add(document3); list.add(document4); list.add(document5); collection.insertMany(list); System.out.println("Documents Inserted"); //Retrieving a collection object collection = database.getCollection("students"); //Retrieving the documents with a limit FindIterable<Document> iterDoc = collection.find().skip(2); Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
Output
Documents Inserted Documents Inserted Document{{_id=5e8886f97631225872d13217, name=Rhim, age=30, city=Delhi}} Document{{_id=5e8886f97631225872d13218, name=Radha, age=28, city=Mumbai}} Document{{_id=5e8886f97631225872d13219, name=Ramani, age=45, city=Pune}}
- Related Articles
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- MongoDB query to skip documents
- Retrieving specific documents from collection by _id in MongoDB
- MongoDB query to skip n first documents?
- How to retrieve all the documents from a MongoDB collection using Java?
- Is there any way to skip some documents in MongoDB?
- How to sort the documents of a MongoDB collection using java?
- How to Update multiple documents in a MongoDB collection using Java?
- Get a count of total documents with MongoDB while using limit?
- Retrieving a Subset of Fields from MongoDB
- Restrict returned data in MongoDB to display only specific values from documents
- How to delete multiple documents from a collection using Java?
- How to get items with a specific value from documents using MongoDB shell?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?

Advertisements