我有来自MongoDB并存储在DBObject中的值。之后,我正在对其进行find
,如下面的代码所示 -
for (String abc: listA) {
BasicDBObject f1 = new BasicDBObject("lk", abc);
DBObject test = container.findOne(f1);
for (String key: test.keySet()) {
System.out.println("key: " + key+ " value: " + test.get(key)); //Line1
}
// how to iterate test.get(key) and prints out all the values one by one
}
从Line 1
我在控制台上打印以下内容:
key: mystic value: [ "notAThingToSee" , "everyThingIsPossible"]
现在,我需要以这样的方式迭代test.get(key)
,以便在新行中逐个打印出数据,如下所示 -
notAThingToSee
everyThingIsPossible
这可能吗?我尝试过这样做,但我收到for each is not applicable to java.lang.Object
for (Object ss : test.get("mystic")) {
System.out.println("SS: " + ss); //Line 2
}
答案 0 :(得分:1)
value
数组将以BasicDBList的形式返回,而不是对象数组。尝试类似的东西(避免编译这个...):
DBObject test = container.findOne(f1);
BasicDBList values = (BasicDBList) test.get("value");
for (Object value : values) {
System.out.println(value);
}