mongodb java read array code example

Example: mongodb java read array

MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase database = client.getDatabase("employee_db");
MongoCollection collection = database
				.getCollection("employees");

List employees = (List) collection.find().into(
  new ArrayList());

for (Document employee : employees) {

  // courses is a List in a DB
  List courses = (List) employee.get("courses");
  for (Document course : courses) {
    System.out.println("name = " + employee.getString("name")
                       + " course details below");
    System.out.println("course name = " + course.getString("name"));
    System.out.println("cours type = " + course.getString("type"));
  }

}

Tags:

Misc Example