使用Java从mongodb检索数组值 [英] Retrieve array values from mongodb with Java

查看:658
本文介绍了使用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.请帮忙?

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?

推荐答案

尝试使用

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

代替

DBObject e = curssc.next();

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

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

这篇关于使用Java从mongodb检索数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆