问题
public void SearchForDate(String date) {
Information object = new Information();
List<Information> list = new ArrayList<Information>();
MongoCursor<Document> cursor = collection.find(eq("date", date)).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
}
Hi, I want inserting the cursor.next().toJson() information on a new object of the information class, how I could do it? the cursor just return a String...
public void BuscarPorPalabra(String date) {
MongoCursor<Document> cursor = collection.find(eq("date", date)).iterator();
basicBOE b = new basicBOE();
while (cursor.hasNext()) {
Document element = cursor.next();
b.setDepartament(element.getString("departament"));
b.setText(element.getString("text"));
b.setTitle(element.getString("title"));
list.add(b);
}
}
I got it , but I now have a new problem, the last item overwrite the other object of the list and have a list with 64 same items...
回答1:
You just need to iterate the resultset and map the Document
to your object.
while (cursor.hasNext()) {
Document document = cursor.next();
Information object = new Information();
object.setSomeField(document.getString("somefield"));
}
来源:https://stackoverflow.com/questions/42031247/map-document-to-object