[Solved] Read data from mongodb using java [closed]


You should be able to cast DBObject to one of it’s implementation (see all implementations here : https://api.mongodb.com/java/3.0/com/mongodb/DBObject.html), in this case BasicDBList, which extends ArrayList , so you can use all methods from arrayList there.

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("test");// Use DB 
DBObject allQuery = new BasicDBObject(); 
DBCollection collection = db.getCollection("dimensions"); 
DBCursor curs = collection.find(); 
Iterator<DBObject> fields = curs.iterator(); 
while(fields.hasNext()){ //
   BasicDBList geoList = (BasicDBList) fields.next().get("GeoLevels");
   BasicDBObject object = (BasicDBObject) geoList.get(0); // this should return {"5": "Continent_Name"}
   Object value = object.get("5); // value should contain "Continent_Name"
} 

I also suggest you to look at Spring-data mongodb which allows you to map object from MongoDB directly to POJOs so it is then much easier to work with:

http://projects.spring.io/spring-data-mongodb/

4

solved Read data from mongodb using java [closed]