有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何将mongoDB的输出转换为java对象(使用对象工厂)

我是MongoDB新手,尝试将集合的结果放入java类对象中。而收集。find()将游标返回到Document类,并且大多数操作仅在该类上完成,我尝试将codecRegistry放入POJO。我可以让一些函数工作,但我没有灵活性,因为大多数方法都在Document类上工作,我无法转换到我的类

FindIterable<Document> iterable = database.getCollection("bookingTest").find()
            .sort(Sorts.ascending("bookingId"));
    MongoCursor<Document> cursor1 = iterable.iterator();

    List<Document> list1 = new ArrayList<Document>();
try {
        while (cursor1.hasNext()) {

            list1.add(cursor1.next());

        }
    }

    finally {
        cursor1.close();
    }

    for (Document document : list1) {

        bookingMaster bm = new bookingMaster();
        System.out.println("id is " + document.toJson().toString());

}

在这段代码中,我能够在光标上进行迭代,得到如下输出:

id is {"_id": {"$oid": "5e93034001c18267dde36f5c"}, "bookingDay": "Tuesday", "bookingEndTime": "12:00", "bookingId": 1, "bookingPerson": "piyush.411031@gmail.com", "bookingStartTime": "10:00", "bookingTeam": "ADM"}

我想要实现的是在类bookingMaster的对象中获得这些输出,并以我想要的方式使用它。 有没有办法做到这一点


共 (0) 个答案