Today we will write a sample program using MongoDB java driver to sort a collection.
Assume that we have a collection like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
C:\Users\sanju.org>mongo MongoDB shell version: 2.2.0 connecting to: test > use cricket switched to db cricket > db.batsman.insert({“name” : “Sachin Tendulkar”, “Tests Played” : 155, “Total Score” : 15000, “Average” : 50.12}) > db.batsman.insert({“name” : “Rahul Dravid”, “Tests Played” : 115, “Total Score” : 12000, “Average” : 48.12}) > db.batsman.insert({“name” : “VVS Laxman”, “Tests Played” : 110, “Total Score” : 11000, “Average” : 38.12}) > db.batsman.find().sort({“Total Score” : 1}) { “_id” : ObjectId(“51503763f410beb3bef16968″), “name” : “VVS Laxman”, “Tests Played” : 110, “Total Score” : 11000, “Average” : 38.12 } { “_id” : ObjectId(“51503749f410beb3bef16967″), “name” : “Rahul Dravid”, “Tests Played” : 115, “Total Score” : 12000, “Average” : 48.12 } { “_id” : ObjectId(“51503727f410beb3bef16966″), “name” : “Sachin Tendulkar”, “Tests Played” : 155, “Total Score” : 15000, “Average” : 50.12 } |
Sorting or ordering by a particular column is really simple using the console, The sort function takes column(s) name and order in which the sorting needs to be done. (-1 == descending and 1 == ascending)
Sample program assume that the MongoDB installed in your local machine and running on default port. Please change the host and port if your MongoDB server is running in a different environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.ourownjava.mongo; import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; /** * @author ourownjava.com */ public class MongoDBOrderByExample { private static final String HOST = "localhost"; private static final int PORT = 27017; /** * @return Mongo * @throws UnknownHostException */ public static Mongo getConnection() throws UnknownHostException { return new Mongo(HOST, PORT); } /** * @param args * @throws UnknownHostException */ public static void main(final String[] args) throws UnknownHostException { final Mongo mongo = getConnection(); final DB db = mongo.getDB("cricket"); final DBCollection collections = db.getCollection("batsman"); final BasicDBObject searchQuery = new BasicDBObject(); final DBCursor cursor = collections.find(searchQuery); final BasicDBObject orderBy = new BasicDBObject(); orderBy.put("Total Score", 1); cursor.sort(orderBy); printCursor(cursor); } private static void printCursor(final DBCursor cursor) { while (cursor.hasNext()) { System.out.println(""); final DBObject dbObject = cursor.next(); for (final String key : dbObject.keySet()) { System.out.print(key); System.out.print(" >> "); System.out.print(dbObject.get(key)); } } } } |
Run the program and see the console output.
1 2 3 |
_id >> 51503763f410beb3bef16968name >> VVS LaxmanTests Played >> 110.0Total Score >> 11000.0Average >> 38.12 _id >> 51503749f410beb3bef16967name >> Rahul DravidTests Played >> 115.0Total Score >> 12000.0Average >> 48.12 _id >> 51503727f410beb3bef16966name >> Sachin TendulkarTests Played >> 155.0Total Score >> 15000.0Average >> 50.12 |
The collections sorted or ordered by the key “Total Score”
Pingback: MongoDB – How to write order-by clause using MongoDB java drivers? | Clean Java