Java 从mongoDB中的数组访问子文档

Java 从mongoDB中的数组访问子文档,java,mongodb,Java,Mongodb,这是我的sampale mongo医生 "Notification" : [ { "date_from" : ISODate("2013-06-30T18:30:00Z"), "date_too" : ISODate("2013-07-23T18:30:00Z"), "description" : "bbbbbbbbbbbbbbb", "url" : "bbbbbbbbbbbbbb"

这是我的sampale mongo医生

"Notification" : [
        {
            "date_from" : ISODate("2013-06-30T18:30:00Z"),
            "date_too" : ISODate("2013-07-23T18:30:00Z"),
            "description" : "bbbbbbbbbbbbbbb",
            "url" : "bbbbbbbbbbbbbb"
        },
        {
            "date_from" : ISODate("2013-07-07T18:30:00Z"),
            "date_too" : ISODate("2013-07-16T18:30:00Z"),
            "description" : "ddd",
            "url" : "ddd"
        },
        {
            "date_from" : ISODate("2013-07-02T18:30:00Z"),
            "date_too" : ISODate("2013-07-29T18:30:00Z"),
            "description" : "cccc",
            "url" : "cccccccccccccc"
        }
    ],
我正在尝试访问“通知”:数组有3个子文档。我想分别检索每个文档。 我的java代码是

notification=(BasicDBList) f.curr().get("Notification");

感谢您的帮助。

BasicDBList
扩展了
basicbonlist
,后者反过来又扩展了
ArrayList
。您可以使用索引从列表中获取单个元素

BasicDBList notifications = (BasicDBList) cursor.curr().get("Notification");

BasicDBObject first = (BasicDBObject) notifications.get(0); //first document
BasicDBObject second = (BasicDBObject) notifications.get(1); //second document
BasicDBObject third = (BasicDBObject) notifications.get(2); //third document

System.out.println(first.get("description")); // bbbbbbbbbbbbbbb
System.out.println(second.get("url")); // bbbbbbbbbbbbbb
相反,您可能希望对它们进行迭代:

for(Object o : notifications) {
  BasicDBObject obj = (BasicDBObject) o;
  System.out.println(obj.get("url")); //prints the URL of every document in notifications
}