java - 使用 Java 从 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("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)中获取值;

关于java - 使用 Java 从 mongodb 中检索数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654127/

相关文章:

java - mysql命令应该完成哪些流程,Java应该完成哪些流程?

Java OpenCV 绑定(bind)

java - 在 JavaFx 中,在向 ComboBox 对象添加操作后,如何使 ComboBox 可编辑

c++ - 数组上的 const_cast 给出错误

mongodb - 如何根据 Mongo 中的现有值更新对象数组中的属性?

node.js - Nodejs + Expressjs + EJS + Mongodb

java - JLabel 中图像的旋转 - Java

java - 如何将变量声明为常量数组的元素?

javascript - 在 for 循环内的数组上使用 .extend 时出现问题

node.js - 更新数组中的多个元素