使用Java从mongodb中检索数组值

时间:2012-03-11 10:22:42

标签: java arrays mongodb

我有以下代码:

DBCollection collsc = db.getCollection("StudentCourses") ;
BasicDBObject querysc = new BasicDBObject("StudentID",id ); 
DBCursor curssc = collsc.find(querysc);

while(curssc.hasNext()) {

    DBObject e = curssc.next();
    System.out.println("You are currently registered for the following modules: ") ; 
    System.out.println(e.get("CoursesRegistered")) ; 

}

输出:

You are currently registered for the following modules: 
[ "DigitalLogic" "OperatingSystems" , "FundamentalsCSE"]

但是我只希望从数组中返回值,即DigitalLogic,OperatingSystems和FundamentalsCSE。我将使用这些值来填充JList。请帮忙?

1 个答案:

答案 0 :(得分:17)

尝试使用

BasicDBList e = (BasicDBList) curssc.next().get("CoursesRegistered");

而不是

DBObject e = curssc.next();

然后从e.getIndex(index)获取值;