将文档映射到对象 [英] Map document to object

查看:73
本文介绍了将文档映射到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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());
    }
}

我想在信息类的新对象上插入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);
    }
}

我明白了,但现在有一个新问题,最后一个项目覆盖了列表的另一个对象,并且有一个包含64个相同项目的列表...

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...

推荐答案

您只需要迭代结果集并将Document映射到您的对象即可.

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"));
}

这篇关于将文档映射到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆