2013-09-21 53 views
0

喜有下列文件轉換MongoDB的查詢在Java

{ 
     "_id" : ObjectId("523db8f324c8fa2acac03703"), 
     "name" : "abc", 
     "timestamp" : 1379776720000 
} 
{ 
     "_id" : ObjectId("523db8f324c8fa2acac03704"), 
     "name" : "abc", 
     "timestamp" : 1379776730000 
} 
{ 
     "_id" : ObjectId("523db8f324c8fa2acac03705"), 
     "name" : "abc1", 
     "timestamp" : 1379776800000 
} 

而我尋找給定名稱的最大時間戳如下

db.collections_name.find({"name":"abc"}).sort({"timestamp":-1}).limit(1) 

寫蒙戈查詢它工作在蒙戈外殼罰款我蒙戈集合但我想在Java代碼中實現此查詢,我如何在Java中編寫相同的查詢。

回答

3
MongoClient mc = new MongoClient(); 
DB db = mc.getDB("..."); 
DBCollection collection = db.getCollection("..."); 
DBCursor c = collection.find(new BasicDBObject("name","abc")).sort(new BasicDBObject("timestamp",-1)).limit(1); 

不要忘記把你的數據庫和集合名稱