user2325154 user2325154 - 1 month ago 7x
Java Question

How to return an ArrayList of objects in MongoDB and Java?

I have a method in my DAO which looks like this

public List<Document> getAllProducts() {
return mongoCollection.find().into(new ArrayList<Document>());

What I would like instead is

//return a list of Product instead of Document
public List<Product> getAllProducts() {
return mongoCollection.find().into(new ArrayList<Product>());



How can I achieve this?


There is a few java libs that can help you with it:

Morhpia -

Spring Data for MongoDB -

Also, you can do something like:

public List<Product> getAllProducts() {
    DBCursor cur = mongoCollection.find();
    List<Product> products = new ArrayList<Product>();
    while(cur.hasNext()) {
    return products;