2011-12-12 63 views
3

有沒有辦法用MongoDB/Java將BasicDBObject渲染成Map<String,String>有沒有辦法使用MongoDB/Java將BasicDBObject渲染爲Map <String,String>?

public Map<String,String> getObjectByKeyValue(String dbname, String collname, String key, String value) { 

    Map<String,String> result = new HashMap<String, String>(); 

    DB db = mongo.getDB(dbname); 
    DBCollection coll = db.getCollection(collname); 

    BasicDBObject query = new BasicDBObject(); 
    query.put(key, value); 
    DBCursor cur = coll.find(query); 
    while (cur.hasNext()){ 
     DBObject obj = (DBObject) cur.next(); // Correct, or use BasicDBObject? 
     // How to get each pair stored in the object be pushed into a Map<String,String> ? 
    } 
    return result; 
} 

回答

8

您可以撥打BasicBSONObject返回LinkedHashMap<String,Object>

toMap()功能
相關問題