4

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

1 Answer 1

9

You can call toMap() function on BasicBSONObject which returns LinkedHashMap<String,Object>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.