简体   繁体   中英

Is there a way to render a BasicDBObject into Map<String,String> with MongoDB/Java?

Is there a way to render a BasicDBObject into Map<String,String> with MongoDB/Java?

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;
}

您可以在BasicBSONObject上调用toMap()函数,它返回LinkedHashMap<String,Object>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM