使用Java从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。 请帮助?
尝试使用
BasicDBList e = (BasicDBList) curssc.next().get("CoursesRegistered");
代替
DBObject e = curssc.next();
然后从e.getIndex(index)获取值;