使用Java从mongodb检索数组值

时间:2021-09-19 13:27:36

i have the following code :

我有以下代码:

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")) ; 

}

This outputs:

这输出:

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

However i want only the values to be returned from the array, i.e, DigitalLogic, OperatingSystems and FundamentalsCSE. I will use these values to populate a JList. Help please?

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

1 个解决方案

#1


17  

Try to use

尝试使用

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

instead of

代替

DBObject e = curssc.next();

and then get value from e.getIndex(index);

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

#1


17  

Try to use

尝试使用

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

instead of

代替

DBObject e = curssc.next();

and then get value from e.getIndex(index);

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