使用 Java 从 mongodb 中检索数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9654127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:41:28  来源:igfitidea点击:

Retrieve array values from mongodb with Java

javaarraysmongodb

提问by

i have the following code :

我有以下代码:

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")) ; 

}

This outputs:

这输出:

You are currently registered for the following modules: 
[ "DigitalLogic" "OperatingSystems" , "FundamentalsCSE"]

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?

但是,我只希望从数组中返回值,即 DigitalLogic、OperatingSystems 和 FundamentalsCSE。我将使用这些值来填充 JList。请帮忙?

采纳答案by Zoon Nooz

Try to use

尝试使用

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

instead of

代替

DBObject e = curssc.next();

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

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