0

I have the following objects in my mongo db:

{
  "type" : "timetype"
  "time" : "18"
}
{
  "type" : "timetype"
  "time" : "5"
}
{
  "type" : "timetype"
  "time" : "43"
}
{
  "type" : "timetype"
  "time" : "23"
}

And my java code looks like:

BasicDBObject query = new BasicDBObject();
query.put("type", "timetype");

 //I don't know what to put in the (...)
DBCursor cursor = Collection.find(query).sort(...).limit(1);

I would like to find the entry with the smallest time in my mongo database that meets the query parameter but I don't know what to put in sort to make that happen.

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
Grammin
  • 11,808
  • 22
  • 80
  • 138
  • 1
    What does the documentation say? – Sergio Tulentsev May 07 '12 at 16:19
  • http://api.mongodb.org/java/2.0/com/mongodb/DBCursor.html#sort(com.mongodb.DBObject) – nicholas.hauschild May 07 '12 at 16:21
  • 1
    How would I know about sort and limit if I didn't read the documentation? I don't know what the goes in the sort is it something like: {"time" : 1}? Down-voting and then linking the documentation isn't helpful. – Grammin May 07 '12 at 16:25
  • @Grammin It is easy to try out these kinds of things in mongo shell. Learning basic use of the shell will speed up the development as you can iteratively try out e.g. new kind of queries. – jhonkola May 08 '12 at 12:13

1 Answers1

3

Something along the lines of

Collection.find(query).sort(new BasicDBObject( "time" , 1 )).limit(1);

should work.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Dsynomic
  • 102
  • 7