
- 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
How to index and sort with pagination using custom field in MongoDB?
Let us first create a collection with documents −
> db.demo373.createIndex({"Name":1,"CountryName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo373.insertOne({"Name":"Chris","Age":22,"CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffde2ae06a1609a00aff") } > db.demo373.insertOne({"Name":"David","Age":21,"CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffe82ae06a1609a00b00") } > db.demo373.insertOne({"Name":"Bob","Age":23,"CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59fff42ae06a1609a00b01") } > db.demo373.insertOne({"Name":"John","Age":21,"CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffff2ae06a1609a00b02") } > db.demo373.insertOne({"Name":"Carol","Age":23,"CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5a00082ae06a1609a00b03") }
Display all documents from a collection with the help of find() method −
> db.demo373.find();
This will produce the following output −
{ "_id" : ObjectId("5e59ffde2ae06a1609a00aff"), "Name" : "Chris", "Age" : 22, "CountryName" : "US" } { "_id" : ObjectId("5e59ffe82ae06a1609a00b00"), "Name" : "David", "Age" : 21, "CountryName" : "AUS" } { "_id" : ObjectId("5e59fff42ae06a1609a00b01"), "Name" : "Bob", "Age" : 23, "CountryName" : "UK" } { "_id" : ObjectId("5e59ffff2ae06a1609a00b02"), "Name" : "John", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e5a00082ae06a1609a00b03"), "Name" : "Carol", "Age" : 23, "CountryName" : "AUS" }
Following is the query to implement index and sorting with pagination in MongoDB −
> db.demo373.find().sort({"Name":1, "Age":1}).limit(4).skip(2);
This will produce the following output −
{ "_id" : ObjectId("5e59ffde2ae06a1609a00aff"), "Name" : "Chris", "Age" : 22, "CountryName" : "US" } { "_id" : ObjectId("5e59ffe82ae06a1609a00b00"), "Name" : "David", "Age" : 21, "CountryName" : "AUS" } { "_id" : ObjectId("5e59ffff2ae06a1609a00b02"), "Name" : "John", "Age" : 21, "CountryName" : "US" }
- Related Articles
- How to perform custom sort by field value in MySQL?
- Achieve Pagination with MongoDB?
- Set MongoDB compound index with a fixed value field
- How to sort volley arraylist with custom object in android?
- Add field that is unique index to collection in MongoDB?
- How to create an index with MongoDB?
- How to drop an index in MongoDB using Java?
- How to create an index in MongoDB using Java?
- How can I sort documents in MongoDB 4 and display only a single field?
- Sort MongoDB field which contains both integer and decimal values?
- How to define custom sort function in JavaScript?
- How to sort in MongoDB?
- Creating an index on a nested MongoDB field?
- Get index of given element in array field in MongoDB?
- How to improve MongoDB queries with multikey index in array?

Advertisements