Q:

Retrieve mongodb array element using java

I have this in my db. 
{
"_id" : ObjectId("59424f41baaacf1f40815ae8"),
"first_name" : "Yazid",
"last_name" : "Amir",
"gender" : "Male",
"hobby" : ["Memanah", "Business", "Fusal", "Makan"]
}

Let say that I want to retrieve the "Business" from array hobby. So my code will be like this
MongoCollection collection = db.getCollection("customers");
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("first_name", "Yazid");

MongoCursor<Document> cursor = collection.find(whereQuery).iterator();

try {
while (cursor.hasNext()) {
    Document str = cursor.next();

    out.println(str.get("hobby.0")); // display specific field
}
} finally {
cursor.close();
}

However, the result is null. 

A:

Use a List<Document> to store your array
while (cursor.hasNext()) {
    Document str = cursor.next();

    List<Document> list = (List<Document>)str.get("hobby");

    out.println(list.get(0)); // display specific field
}

