MongoDB with Java
MongoDB is a powerful NoSQL database that provides high performance, high availability, and easy scalability. It works on the concept of collections and documents. In this tutorial, we will discuss how to use MongoDB with Java. If you are a Java developer and want to get started with MongoDB, this tutorial is for you.
What You Will Need
Before we begin, you need to have a few things set up:
- Java SE Development Kit (JDK): You can download the latest version from Oracle's official website.
- MongoDB: You can download MongoDB Community Server from MongoDB's official website.
- MongoDB Java Driver: It's a MongoDB-supported library that provides a high-level API to interact with MongoDB using Java.
Setting Up MongoDB Java Driver
To use MongoDB in your Java programs, you need to include MongoDB Java Driver in your project.
If you're using a project management tool like Maven, include the following in your pom.xml
:
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.5</version>
</dependency>
</dependencies>
If you're not using any project management tool, download the JAR file and manually add it to your project's classpath.
Connecting to MongoDB
Let's start by connecting to MongoDB. The MongoClients.create()
method is used to create a new Mongo Client.
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
public class ConnectToDB {
public static void main( String args[] ) {
MongoClient mongoClient = MongoClients.create();
System.out.println("Connected to the database!");
}
}
Creating a Database
Once connected, you can create a new database using the getDatabase()
method.
import com.mongodb.client.MongoDatabase;
public class CreateDB {
public static void main( String args[] ) {
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("myDatabase");
System.out.println("Database created!");
}
}
Creating a Collection
In MongoDB, collections are analogous to tables in relational databases. You can create a collection using the getCollection()
method.
import com.mongodb.client.MongoCollection;
public class CreateCollection {
public static void main( String args[] ) {
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("myCollection");
System.out.println("Collection created!");
}
}
Inserting Documents
Documents are similar to rows in a relational database. You can insert documents into a collection using the insertOne()
method.
import org.bson.Document;
public class InsertDocument {
public static void main( String args[] ) {
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("myCollection");
Document doc = new Document("name", "John Doe")
.append("age", 45)
.append("email", "[email protected]");
collection.insertOne(doc);
System.out.println("Document inserted!");
}
}
Querying Documents
You can query documents from a collection using the find()
method.
import com.mongodb.client.MongoCursor;
public class QueryDocument {
public static void main( String args[] ) {
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("myCollection");
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
}
}
This is a basic introduction to using MongoDB with Java. We have covered how to connect to MongoDB, create a database and a collection, insert a document, and query documents. There are many more operations you can perform with MongoDB, such as updating and deleting documents. I encourage you to explore the MongoDB Java Driver documentation to learn more. Happy coding!