Java MongoDB Read Document Example

This tutorial shows how to read documents from a collection in MongoDB using a Java program.

MongoDB is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document.
Learn MongoDB with Java at https://www.javaguides.net/p/java-mongodb-tutorial.html

Tools and Technologies Used

  • Java (JDK) 10
  • Maven 3.5+
  • Eclipse Neon
  • MongoDB 3.12.0

Installing MongoDB

Use the following article to install MongoDB on Windows 10.
Make sure that you have installed MongoDB and started MongoDB server on default port 27017.

Database Setup

In the previous tutorial, we have created MongoDB database, collection and inserts few documents into a collection.

Java MongoDB Driver

We use the following Maven declaration to include the MongoDB Java Driver in our maven project.
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
 <groupId>org.mongodb</groupId>
 <artifactId>mongo-java-driver</artifactId>
 <version>3.12.0</version>
</dependency>
Note that it is an all-in-one JAR, which embeds the core driver and BSON. BSON, short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments.

Java MongoDB Read Document Example - find() Method

In this example, we iterate over all data of the "users" collection and print to the console.
package net.javaguides.mongodb.document;

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;

import java.util.ArrayList;

/**
 * MongoDB Read Documents Example
 * @author Ramesh Fadatare
 *
 */

public class MongoReadAll {

    public static void main(String[] args) {

        try (var mongoClient = MongoClients.create("mongodb://localhost:27017")) {

            var database = mongoClient.getDatabase("javaguides");

            MongoCollection < Document > collection = database.getCollection("users");

            try (MongoCursor < Document > cur = collection.find().iterator()) {

                while (cur.hasNext()) {

                    var doc = cur.next();
                    var users = new ArrayList < > (doc.values());

                    System.out.printf("%s: %s%n", users.get(1), users.get(2));
                }
            }
        }
    }
}
Output:
Tony: Stark
Tom: Cruise
Amir: Khan
Umesh: Fadatare
Ramesh: Pawar
We retrieve the "users" collection with the getCollection() method:
MongoCollection<Document> collection = database.getCollection("users");
We iterate through the documents of the collection. The find() method finds all documents in the collection:
try (MongoCursor<Document> cur = collection.find().iterator()) {

    while (cur.hasNext()) {

        var doc = cur.next();
        var cars = new ArrayList<>(doc.values());

        System.out.printf("%s: %s%n", cars.get(1), cars.get(2));
    }
}

Java MongoDB Tutorials

All Java MongoDB tutorials at https://www.javaguides.net/p/java-mongodb-tutorial.html

Comments

  1. Replies
    1. I was using Java 10 which provides var keyword to declare variable.
      You can use type (primitive types or objects) to declare variables instead of var keyword.

      Delete

Post a comment