user2572739
user2572739

Reputation: 309

DBObject to Map in java

I need to put the result from MongoDB in Map. My code is

DBCollection collection = db.getCollection("template");
DBCursor cursor = collection.find(allQuery, removeIdProjection); 
DBObject resultElement = null;
resultElement = cursor.next();

and the result Json is:

{ "GraphLabel" : "Volume Of Work Orders" , "XaxisLabel" : "2012" , "YaxisLabel" : "volume(k)" , "ShowLegend" : "FALSE" , "query" : "select sd.season_id,sd.season, count(fsf.defect_type_id) from m2m.season_dim sd ,m2m.field_service_fact fsf where fsf.season_id = sd.season_id group by sd.season_id"}

Need to put the values with MAP or POJO .. Can somebody help please?

Upvotes: 3

Views: 7196

Answers (2)

Andrews
Andrews

Reputation: 11

Below Code would do:

DBCollection collection = db.getCollection("template");
DBCursor cursor = collection.find(allQuery, removeIdProjection); 
Document doc = cursor.next(); // Which is already a Map compatible object

Map <String, Object> mDoc = doc;

Upvotes: 1

Ori Dar
Ori Dar

Reputation: 19000

The DBObject has a toMap() method which transforms it into map

Upvotes: 9

Related Questions