我正在尝试查询集合中的多个字段,如下所示:
{ "_id" : ObjectId("54ee37f3a4cd7c1ce4483124"), "imageblock" : "http://someimage.jpg", "textblock" : "Lorem ipsum dolor sit amet, in eget aliquam, odio ut leo mi turpis a felis, adipiscing neque maecenas ac nulla nec curabitur. A hac neque magnis integer arcu ut, per bibendum, aliquet metus nec ante orci amet. Feugiat quis sit" }
检索“imageblock”和“textblock”键的值的最佳方法是什么?使用MongoDB非常新。
Mongo mg = new Mongo("localhost", 27017);
DB db = mg.getDB("deepdivedb");
DBCollection collection = db.getCollection("contentblocks");
DBCursor cursor = collection.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().get("imageblock"));
}
} finally {
cursor.close();
}
答案 0 :(得分:1)
如果您知道要访问的键的名称,可以在try / catch块中执行以下操作。
while (cursor.hasNext()) {
DBObject document = cursor.next();
System.out.println(document.get("imageblock"));
System.out.println(document.get("textblock"));
}