
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to sort the documents of a MongoDB collection using java?
While retrieving records from a MongoDB collection, you can sort the records in the result using the sort() method.
Syntax
db.COLLECTION_NAME.find().sort({KEY:1})
The Java MongoDB library provides a method with the same name, to limit the number of records invoke this method (on the result of the find() method) by passing the type of the sort (ascending or descending) and the field name based on which you want to sort the records as −
sort(Sorts.ascending("age");
Example
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Sorts; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class SortingRecords { 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().sort(Sorts.ascending("age")); Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
Output
Documents Inserted Document{{_id=5e88843d5c851b345fce2a33, name=Ram, age=26, city=Hyderabad}} Document{{_id=5e88843d5c851b345fce2a34, name=Robert, age=27, city=Vishakhapatnam}} Document{{_id=5e88843d5c851b345fce2a36, name=Radha, age=28, city=Mumbai}} Document{{_id=5e88843d5c851b345fce2a35, name=Rhim, age=30, city=Delhi}} Document{{_id=5e88843d5c851b345fce2a37, name=Ramani, age=45, city=Pune}}
- Related Articles
- How to retrieve all the documents from a MongoDB collection using Java?
- How to Update multiple documents in a MongoDB collection using Java?
- How to count the number of documents in a MongoDB collection?
- How to delete multiple documents from a collection using Java?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- How to create a MongoDB collection using Java?
- How to drop a MongoDB Collection using Java?
- How to return documents of a collection without objectId in MongoDB?
- How to delete all the documents from a collection in MongoDB?
- How to update or modify the existing documents of a collection in MongoDB?
- How to find two random documents in a MongoDB collection of 6?
- Limit the number of documents in a collection in MongoDB?
- How to sum the value of a key across all documents in a MongoDB collection?
- How to add a new field to all the documents in a MongoDB collection

Advertisements