使用Java从mongodb检索数组值

前端之家收集整理的这篇文章主要介绍了使用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)获取值;

原文链接:https://www.f2er.com/java/126616.html

猜你在找的Java相关文章