2016-04-06 22 views
0

eşitse Yani bir if ifadeye kod parçası üzerinde çalışıyorum ve benim MongoDB bir değer Şu anda bu var "test"MongoDB (Java) deyimi değerini

eşitse if açıklamada ı kontrol etmek istiyorum çok değil ama gerçekten benim MongoDB bu

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

Örnek nasıl bilmiyorum

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

getTestCollection() nasıl döner? – Idos

+0

'halka MongoCollection getTestCollection() { \t \t dönüş database.getCollection ("test_collection"); \t} – PxlPenguin

+0

Ne demek istiyorsun * benim MongoDB * değerinin bir değeri? Verilerinizin ve bulmak istediğiniz alanın/öğenin bir örneğini gösterin. (Sorunuzu ilgili bilgilerle düzenleyin) – Idos

cevap

0

bir kez ge Bu koleksiyonda, üzerinde Aquire imleci gerekir ve bunun üzerinden döngü. aşağıya bakınız.

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

'u yayınlamayı denediniz mi? –

+0

Evet, 'DBCollection testCollection = (DBCollection) DBManager.getInstance(). GetTestCollection();' Ama şimdi bu hatayı alıyorum: 'Caused by: java.lang.ClassCastException: com.mongodb.MongoCollectionImpl com'a dönüştürülemiyor. mongodb.DBCollection' – PxlPenguin