DBObject在java中映射

时间:2013-08-27 10:17:03

标签: java mongodb

我需要将MongoDB的结果放在Map中。我的代码是

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

结果Json是:

  

{“GraphLabel”:“工作量订单”,“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其中fsf.season_id =   sd.season_id group by sd.season_id“}

需要使用MAP或POJO放置值。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:7)

DBObject有一个toMap()方法,可将其转换为地图

答案 1 :(得分:0)

以下代码可以:

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;