使用Java从mongodb检索数组值
我有以下代码:使用Java从mongodb检索数组值,java,arrays,mongodb,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
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。请帮忙 尝试使用
BasicDBList e = (BasicDBList) curssc.next().get("CoursesRegistered");
而不是
DBObject e = curssc.next();
然后从e.getIndex(index)中获取值 它不起作用。我得到的异常如下:线程“main”java.lang.ClassCastException中的异常:com.mongodb.BasicDBObject无法在modules.RegistrationSystem.main(RegistrationSystem.java:80)处强制转换为com.mongodb.BasicDBList,仍然存在异常。线程“main”java.lang.IllegalArgumentException中的异常:BasicBonlist只能使用数字键,而不能在org.bson.types.BasicBonlist.\u getInt(BasicBonlist.java:161)在org.bson.types.BasicBonlist.\u getInt(BasicBonlist.java:152)在org.bson.types.BasicBonlist.get(BasicBonlist.java:104)中使用在modules.RegistrationSystem.main(RegistrationSystem.java:82)中,这样做了。DBCollection collsc=db.getCollection(“学生课程”);BasicDBObject querysc=新的BasicDBObject(“学生id”,id);DBCursor curssc=collsc.find(querysc);while(curssc.hasNext()){//DBObject e=curssc.next();BasicDBList e=(BasicDBList)curssc.next().get(“CoursesRegistered”);System.out.println(“您当前注册了以下模块:”);for(int i=0;i如何使用mongo java 3.0驱动程序执行此操作?