本文介绍了有没有一种方法可以将BasicDBObject呈现为Map< String,String>.使用MongoDB/Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用MongoDB/Java将BasicDBObject渲染为Map<String,String>?

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>

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

这篇关于有没有一种方法可以将BasicDBObject呈现为Map&lt; String,String&gt;.使用MongoDB/Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:09