retrieve documents from mongodb using java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Previously we saw how to insert data in mongodb using java. Here we will see how we can retrieve documents from mongodb using java

Retrieve Documents from Mongodb Using Java:

We are java developers and need not write mongodb queries. Mongodb java driver provides functionality to retrieve documents from mongodb using java. We can make use of find operation. Javadoc says

Finds all documents in the collection.
Returns:
the find iterable interface

As you can see, the find method returns a FindIterable which again provides method to iterate after we retrieve documents from mongodb using java.

Let us try a simple example

Here is what we have done

  • connect to mongodb using new MongoClient(“localhost”,27017);
  • connect to database using client.getDatabase(“employee_db”);
  • Get collection using database.getCollection(“employees”);

Now, collection.find() method retrieves all documents from collection. We have called into() method in it by passing a new instance of arraylist. It gives us an arraylist directly instead of a FindIterable. We, as java developers will be comfortable in performing operations on list.

Above code prints out all documents as below

I hope this article helped understand how to retrieve documents from mongodb using java.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

17 thoughts on “retrieve documents from mongodb using java

  • Pingback:Retrieve array from mongodb using java - theJavaGeek

  • March 13, 2016 at 3:05 am
    Permalink

    Really Appreciate How You Have Modified It As per Java Dev…

    Reply
  • March 13, 2016 at 3:05 am
    Permalink

    Really Appreciate, The Way You Have Modified It As per Java Dev…

    Reply
  • September 15, 2016 at 4:59 pm
    Permalink

    how to display an image and content about image underneath it retreiving from mongodb using java?

    Reply
  • October 6, 2016 at 10:47 am
    Permalink

    I am new to mongodb and java, i have error in this line plz help me to progress

    List documents = (List) collection.find().into(
    new ArrayList());

    Reply
  • November 8, 2016 at 8:15 pm
    Permalink

    into is not working

    Reply
  • February 21, 2017 at 2:52 pm
    Permalink

    Not coming Answer Sir.

    Reply
  • April 21, 2017 at 6:12 pm
    Permalink

    Hi prasad ,

    Its good article if you want to fetch complete collection.
    what if you want to query the databaase and get the records.
    how we can do this ?

    appreciate your help.

    Reply
  • September 12, 2017 at 6:17 pm
    Permalink

    The method into(A) in the type MongoIterable is not applicable for the arguments (ArrayList)

    I am getting this eclipse while using into()

    Reply
  • November 18, 2017 at 12:23 am
    Permalink

    Thank you so much! Exactly as I needed!

    Reply
  • December 15, 2017 at 2:19 pm
    Permalink

    I have many collections in my database. from each collection , I want to fetch one type of data(for example: i want to fetch “from” field from each table)..how can i do that??

    Reply
  • December 18, 2017 at 9:15 am
    Permalink

    waiting for a reply

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *