2016-04-06 85 views
0

所以我工作的一段代碼用if聲明,並在聲明if我想檢查我的MongoDB的值等於"test"的MongoDB(Java)的if語句等於值

我現在有這這是不是很多,但我真的不知道該怎麼辦我的MongoDB這

if (DBManager.getInstance().getTestCollection()) { 

{ 
"_id": { 
    "$oid": "56fd47ea18de75129095d841" 
}, 
"foo": "test" 
} 
+0

getTestCollection()返回什麼? – Idos

+0

'public MongoCollection getTestCollection(){ \t \t return database.getCollection(「test_collection」); \t}' – PxlPenguin

+0

雖然你的意思是*我的MongoDB的值?顯示您想要查找的數據和字段/項目的示例。 (編輯你的問題的相關信息) – Idos

回答

0

一旦GE t集合,你需要Aquire光標在它上面循環。見下文。

DBCollection testCollection = DBManager.getInstance().getTestCollection() 
    DBCursor cursor = testCollection.find(); 

     while (cursor.hasNext()) { 
      BasicDBObject obj = (BasicDBObject) cursor.next(); 
      if ("test".equals(obj.get("foo")) { 
       System.out.println("Found"); 
      } 
     } 
+0

'DBCollection testCollection = DBManager.getInstance()。getTestCollection()' 想讓我投'com.mongodb.DBCollection;' – PxlPenguin

+0

你試過投射嗎? –

+0

是的,'DBCollection testCollection =(DBCollection)DBManager.getInstance()。getTestCollection();' 但是現在我得到這個錯誤'由於:java.lang.ClassCastException:com.mongodb.MongoCollectionImpl無法轉換爲com。 mongodb.DBCollection' – PxlPenguin