1

我使用 BasicDBObject 将哈希表存储在 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;
}
4

1 回答 1

2

它将 HashTable 作为 BasicDBObject(HashMap 格式)添加到数据库中。因此,为了检索数据,您应该首先将 HashTable 作为 HashMap 获取,然后手动将其转换为 HashTable。这里的示例显示了如何以 HashTable 格式检索数据。我尝试使用HashTable<String,String>.

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

登录类:

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);
    }
}
于 2012-06-22T06:52:26.410 回答