user1259962
user1259962

Reputation:

Retrieve array values from mongodb with Java

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?

Upvotes: 10

Views: 19880

Answers (1)

Zoon Nooz
Zoon Nooz

Reputation: 5866

Try to use

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

instead of

DBObject e = curssc.next();

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

Upvotes: 17

Related Questions