2012-07-27 99 views
0

我需要有以下查詢轉換到Java代碼查詢具體的MongoDB領域

使用DBNAME
db.collectionName.find({},{參考:1})

我想爲關鍵的「參考」

獲取值我試過但這似乎以下不按照要求進行調整

Mongo dbConnection; 
     DB dbobject; 
     dbConnection = new Mongo("localhost", 27017); 
     dbobject = dbConnection.getDB("dbName"); 
     DBCollection profileCollection = dbobject.getCollection("collectionName"); 
     BasicDBObject query = new BasicDBObject(); 
     query.put("{},{reference:1}"); 
     DBCursor mongocursor =profileCollection.find("{}, {reference:1}"); 
     try { 
      while(mongocursor.hasNext()) { 
       System.out.println(mongocursor.next().get("reference")); 
      } 
     } finally { 
      mongocursor.close(); 
     } 

回答

0

請嘗試以下

basicDBObject uqery = new BasicDBObject(); 
query.put("reference", 1); 
DBCursor cursor = profilecollection.find(query); 

你不需要指定{} ...這是返回所有的字段的默認選項