我使用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);
它存储没有错误,但我如何检索特定的hashTable。假设我想要检索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;
}
答案 0 :(得分:2)
它将HashTable作为BasicDBObject(HashMap格式)添加到DB中。因此,为了检索数据,您应首先将HashTable作为HashMap,然后手动将其转换为HashTable。这个示例显示了如何以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
}
插入&amp;查找方法
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);
}
}