🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 Binary JSON, is a binary-encoded serialization of JSON-like documents.
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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Why you can use var?
ReplyDeleteI was using Java 10 which provides var keyword to declare variable.
DeleteYou can use type (primitive types or objects) to declare variables instead of var keyword.