Skip to content

Instantly share code, notes, and snippets.

@shamilnabiyev
Created July 6, 2019 17:56
Show Gist options
  • Save shamilnabiyev/942122aeb47e0f0e85c549d14c95cda5 to your computer and use it in GitHub Desktop.
Save shamilnabiyev/942122aeb47e0f0e85c549d14c95cda5 to your computer and use it in GitHub Desktop.
Java MongoDB Driver, sort by date
DB db = mongo.getDB("yourDbName");
DBCollection coll = db.getCollection("person");
DBObject query = new BasicDBObject();
query.put("dateCreated", new BasicDBObject($exists : true));
DBCursor cur = coll.find(query).sort(new BasicDBObject("dateCreated", 1)).limit(10);
while(cur.hasNext()) {
DBObject obj = cur.next();
// Get data from the result object here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment