I am new to MongoDB and trying to get a collection's result into java class object. While collection.find() returns a cursor to Document class and most of the operations are done on this class only, I tried putting a codecRegistry to my POJO. I can get some functions working but I dont have the flexibility as most of the methods work on Document class and I am unable to cast to my class.
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());
}
In this code, I am able to iterate over the cursor and I am getting the output as
id is {"_id": {"$oid": "5e93034001c18267dde36f5c"}, "bookingDay": "Tuesday", "bookingEndTime": "12:00", "bookingId": 1, "bookingPerson": "piyush.411031@gmail.com", "bookingStartTime": "10:00", "bookingTeam": "ADM"}
What I want to achieve is getting these output in an object of the class bookingMaster
and use it the way I want.
Is there a way to do that??