Map document to object

孤者浪人 提交于 2019-12-12 02:12:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!