如何从mongodb检索哈希表



我正在使用BasicDBObject将hashtable存储在mongodb中。现在我想检索这个哈希表。我该怎么做?

HashTable<String, Login> loginHashTable;
HashTable<String, Other> otherHashTable;
DBCollection coll = db.getCollection(users);
BasicDBObject obj = new BasicDBObject();
obj.insert("HashTable1", loginHashTable);
obj.insert("HashTable2", otherHashTable);
coll.insert(obj);

它被正确存储而没有错误,但我如何检索一个特定的哈希表。假设我想检索loginHashTable。那么,我该怎么做呢?

Somewhat like this:
HashTable <String,Login> retrieveTable = obj.get("HashTable1");

我的登录类是这样的:

public class Login extends ReflectionDBObject implements Serializable
{
    /** Enterprise user name used for authentication. */
    public String username = null;
    /** Enterprise password used for authentication. */
    public String password = null;
    /**Name of manufacturer of mobile phone*/
    public String manufacturer = null;
    /**Name of model of mobile phone*/
    public String model = null;
    /**Language in which Mobile software release is needed*/
    public String language = null;
    /**Current version of the software*/
    public String version = null;
    public static String id;
}

我的另一节课是这样的:

public class Other extends ReflectionDBObject implements Serializable
{
   public String sessionID = null;
}

它将HashTable作为BasicDBObject(HashMap格式)添加到数据库中。因此,为了检索数据,您应该首先将HashTable作为HashMap获取,然后将其手动转换为HashTable。我用HashTable<String,String>试过了.

为了将 Login 类插入到 Mongo 中,您可以使用对象映射库,例如 Morphia 。第二种方式可以使用ReflectionDBObject。如果扩展 ReflectionDBObject,则可以添加 Login 对象。

登录类 :

public class Login extends ReflectionDBObject {
    /** Enterprise user name used for authentication. */
    private String username;
    /** Enterprise password used for authentication. */
    private String password;
    /**Name of manufacturer of mobile phone*/
    private String manufacturer;
    /**Name of model of mobile phone*/
    private String model;
    /**Language in which Mobile software release is needed*/
    private String language;
    /**Current version of the software*/
    private String version;
    private String id;
    // Getters & Setters
}

其他类别 :

public class Other extends ReflectionDBObject{
    public String sessionID;
    // Getter & Setter
}

插入和查找方法

public void insert() {
    Login login = new Login();
    login.setUsername("test");
    login.setPassword("12345");
    login.setLanguage("english");
    Other other = new Other();
    other.setSessionID("111111");
    Hashtable<String, Login> loginHashTable = new Hashtable<String, Login>();
    loginHashTable.put("login", login);
    Hashtable<String, Other> otherHashTable = new Hashtable<String, Other>();
    otherHashTable.put("other", other);
    DBObject obj = new BasicDBObject();
    obj.put("HashTable1", loginHashTable);
    obj.put("HashTable2", otherHashTable);
    coll.insert(obj);
}
public void find() {
    DBObject query = new BasicDBObject();
    DBCursor cur = coll.find(query);
    for (DBObject obj : cur) {
        HashMap<String, Login> loginHashMap = (HashMap<String, Login>) obj.get("HashTable1");
        Hashtable<String, Login> loginHashTable = new Hashtable<String, Login>();
        loginHashTable.putAll(loginHashMap);
        HashMap<String, Other> otherHashMap = (HashMap<String, Other>) obj.get("HashTable2");
        Hashtable<String, Other> otherHashTable = new Hashtable<String, Other>();
        otherHashTable.putAll(otherHashMap);
    }
}

最新更新