
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Achieve Pagination with MongoDB?
You can achieve pagination with the help of limit() and skip() in MongoDB.
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.paginationDemo.insertOne({"CustomerName":"Chris","CustomerAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c949de44cf1f7a64fa4df52") } > db.paginationDemo.insertOne({"CustomerName":"Robert","CustomerAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c949df14cf1f7a64fa4df53") } > db.paginationDemo.insertOne({"CustomerName":"David","CustomerAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c949dfc4cf1f7a64fa4df54") } > db.paginationDemo.insertOne({"CustomerName":"Carol","CustomerAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c949e3e4cf1f7a64fa4df55") } > db.paginationDemo.insertOne({"CustomerName":"Bob","CustomerAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c949e474cf1f7a64fa4df56") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.paginationDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c949de44cf1f7a64fa4df52"), "CustomerName" : "Chris", "CustomerAge" : 23 } { "_id" : ObjectId("5c949df14cf1f7a64fa4df53"), "CustomerName" : "Robert", "CustomerAge" : 26 } { "_id" : ObjectId("5c949dfc4cf1f7a64fa4df54"), "CustomerName" : "David", "CustomerAge" : 24 } { "_id" : ObjectId("5c949e3e4cf1f7a64fa4df55"), "CustomerName" : "Carol", "CustomerAge" : 28 } { "_id" : ObjectId("5c949e474cf1f7a64fa4df56"), "CustomerName" : "Bob", "CustomerAge" : 29 }
Here is the query for pagination with MongoDB −
> db.paginationDemo.find().skip(0).limit(3);
The following is the output −
{ "_id" : ObjectId("5c949de44cf1f7a64fa4df52"), "CustomerName" : "Chris", "CustomerAge" : 23 } { "_id" : ObjectId("5c949df14cf1f7a64fa4df53"), "CustomerName" : "Robert", "CustomerAge" : 26 } { "_id" : ObjectId("5c949dfc4cf1f7a64fa4df54"), "CustomerName" : "David", "CustomerAge" : 24 }
- Related Articles
- How to index and sort with pagination using custom field in MongoDB?
- Controlling Pagination with CSS
- Add hoverable pagination with CSS
- Is it possible to achieve a slice chain in MongoDB?
- Achieve Glow Effect with CSS Filters
- Achieve X-Ray effect with CSS
- Create a responsive pagination with CSS
- Set style for pagination with CSS
- Disable a pagination link with Bootstrap
- Achieve Responsiveness for background image with CSS
- Add space between pagination links with CSS
- How to create a pagination with CSS?
- Change the size of the pagination with CSS
- Center pagination on a web page with CSS
- Create a transition effect on hover pagination with CSS

Advertisements