次のコード フラグメントは、クエリの結果を反復処理するときにドキュメントの個々のフィールドを抽出する方法を示しています。必要に応じて、各フィールドを GUI のテキスト ボックスに入れることができます。
完全なコード サンプルはこちら: https://gist.github.com/3087822
private static void queryAndDisplayStudents(DBCollection students)
{
// Get all students (no query criteria).
DBCursor cursor = students.find();
// Iterate over the students.
while (cursor.hasNext())
{
// Display each student.
DBObject student = cursor.next();
// Get the individual fields of the student document.
// These individual fields could, for example,
// be put in text fields of a GUI.
String name = (String) student.get("Name");
Number sid = (Number) student.get("SID");
String university = (String) student.get("University");
// Given that we are not actually building a GUI,
// just display the fields on the command line.
System.out.printf("Student name: %s, SID: %d, University: %s%n",
name, sid, university);
}
}