How to retrieve all the documents from a MongoDB collection using Java?

JavaObject Oriented ProgrammingProgramming

You can retrieve documents from an existing collection in MongoDB using the find() method.

Syntax

db.coll.find()

Where,

  • db is the database.

  • coll is the collection (name) in which you want to insert the document

Example

Assume we have a collection named students in the MongoDB database with the following documents −

{name:"Ram", age:26, city:"Mumbai"}
{name:"Roja", age:28, city:"Hyderabad"}
{name:"Ramani", age:35, city:"Delhi"}

The following query retrieves all the documents from the collected sample.

> use myDatabase()
switched to db myDatabase()
> db.createCollection(sample)
{ "ok" : 1 }
> > db.sample.find()
{ "_id" : ObjectId("5e870492af638d501865015f"), "name" : "Ram", "age" : 26, "city"
: "Mumbai" }
{ "_id" : ObjectId("5e870492af638d5018650160"), "name" : "Roja", "age" : 28,
"city" : "Hyderabad" }
{ "_id" : ObjectId("5e870492af638d5018650161"), "name" : "Ramani", "age" : 35,
"city" : "Delhi" }
>

Using Java program

In Java, you can retrieve all the documents in the current collection using the find() method of the com.mongodb.client.MongoCollection interface. This method returns an iterable object containing all the documents.

Therefore to create a collection in MongoDB using Java program −

  • Make sure you have installed MongoDB in your system

  • Add the following dependency to its pom.xml file of your Java project.

<dependency>
   <groupId>org.mongodb</groupId>
   <artifactId>mongo-java-driver</artifactId>
   <version>3.12.2</version>
</dependency>
  • Create a MongoDB client by instantiating the MongoClient class.

  • Connect to a database using the getDatabase() method.

  • Get the object of the collection from which you want to retrieve the documents, using the getCollection() method.

  • Retrieve the iterable object containing all the documents of the current collection by invoking the find() method.

Example

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
public class RetrievingAllDocuments {
   public static void main( String args[] ) {
      //Creating a MongoDB client
      MongoClient mongo = new MongoClient( "localhost" , 27017 );
      //Connecting to the database
      MongoDatabase database = mongo.getDatabase("myDatabase");
      //Creating a collection object
      MongoCollection<Document> collection = database.getCollection("students");
      //Retrieving the documents
      FindIterable<Document> iterDoc = collection.find();
      Iterator it = iterDoc.iterator();
      while (it.hasNext()) {
         System.out.println(it.next());
      }
   }
}

Output

Document{{_id=5e86db7012e9ad337c3aaef5, name=Ram, age=26, city=Hyderabad}}
Document{{_id=5e86db7012e9ad337c3aaef6, name=Robert, age=27, city=Vishakhapatnam}}
Document{{_id=5e86db7012e9ad337c3aaef7, name=Rahim, age=30, city=Delhi}}
raja
Published on 10-Apr-2020 11:50:03
Advertisements