2016-06-21 75 views
0

我有點麻煩通過id獲取單個文檔(我插入了我自己的ID)。MongoDb Java通過自定義獲取單個文檔_id

我已經看過驅動程序的文檔,但我無法找到任何告訴我如何獲得單個文檔。

我收藏看起來像這樣

{ "_id" : NumberLong(1), "currency_pair" : "GBP/USD", "date" : "2009-05-01 00:00:00", "sell" : "1.47993", "buy" : "1.48076", "spread" : 0.0008300000000001084 } 
{ "_id" : NumberLong(2), "currency_pair" : "GBP/USD", "date" : "2009-05-01 00:00:00", "sell" : "1.48023", "buy" : "1.48076", "spread" : 0.0005300000000001415 } 
{ "_id" : NumberLong(3), "currency_pair" : "GBP/USD", "date" : "2009-05-01 00:00:01", "sell" : "1.48023", "buy" : "1.48077", "spread" : 0.0005399999999999849 } 

我當前的查詢看起來是這樣的。

 if (!Main.REMOTE) { 
      server = "localhost"; 
     } 

     MongoClient mongoClient = new MongoClient(server); 
     MongoDatabase db = mongoClient.getDatabase("rates"); 
     MongoCollection<Document> coll = db.getCollection(tableName); 

     BasicDBObject query=new BasicDBObject("_id", 2); 
     coll.find(query); 

回答

0

嘗試:

BasicDBObject query=new BasicDBObject("_id", NumberLong(2)); 
相關問題