我有来自 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
}
从第1行
我在控制台上打印出以下内容:
key: mystic value: [ "notAThingToSee" , "everyThingIsPossible"]
现在,我需要以这样的方式迭代 test.get(key)
,以便它在新行中一一打印出数据,如下所示 -
notAThingToSee
everyThingIsPossible
这可以做到吗?我尝试这样做,但出现错误 foreach 不适用于 java.lang.Object
for (Object ss : test.get("mystic")) {
System.out.println("SS: " + ss); //Line 2
}
最佳答案
value
数组将以 BasicDBList 的形式返回,而不是对象数组。尝试类似的东西(还没有编译这个...):
DBObject test = container.findOne(f1);
BasicDBList values = (BasicDBList) test.get("value");
for (Object value : values) {
System.out.println(value);
}
关于java - 如何从 mongoDB 对象打印迭代值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003281/