1

I am storing hashtable in mongodb using BasicDBObject. And now I want to retrieve this hashtable.How can I do this?

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

It is stored properly without errors but how can I retrieve a particulat hashTable. Suppose I want to retrieve loginHashTable. So, how can I do this?

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

My login class is like this:

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

My other class is like this:

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

1 に答える 1

2

これは、HashTableをBasicDBObject(HashMap形式)としてDBに追加します。したがって、データを取得するには、最初にHashTableをHashMapとして取得してから、手動でHashTableに変換する必要があります。これは、データをHashTable形式で取得する方法を示していますHashTable<String,String>

LoginクラスをMongoに挿入するには、Morphiaなどのオブジェクトマッピングライブラリを使用できます。2番目の方法は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);
    }
}
于 2012-06-22T06:52:26.410 に答える