Java and MongoDB are two of the most popular technologies for software development, so it's no surprise that they work well together. Whether you're developing on a local or on-premise MongoDB installation or going cloud-first with MongoDB Atlas, the MongoDB Java Driver and surrounding ecosystem make it a snap to integrate MongoDB into your development process.
Even though Java is well known to have the most robust collection of Object Relational Mapping (ORM) libraries for dealing with relational data, using documents in Java is much simpler and more powerful.
If you're starting a new project today, consider setting up on MongoDB Atlas from the get-go. Not only do you get a fully-managed, cloud-native database service, Atlas has integrated full-text search, charts, data navigation, and much more. If you're developing for iOS or Android, Atlas App Services makes mobile synchronization a snap. You can even build web applications on GraphQL directly on top of MongoDB Atlas.
Assuming you already have Java installed on your system, and your favorite IDE fired up and ready to go, the first thing you're going to want to do is get the MongoDB Java Driver. Most IDEs will pick up resources configured in Gradle or Maven, either of which can be used to install the driver.
For Gradle, add the following to your build.gradle file:
For Maven, add the following:
Note that as of 4.0, the MongoDB Java Driver package also includes the MongoDB Scala Driver and the MongoDB Java Reactive Streams Driver for non-blocking asynchronous access.
import com.mongodb.*; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.result.*; import org.bson.Document; import org.bson.types.ObjectId; import java.util.List; import java.util.Arrays; import java.util.ArrayList; import static com.mongodb.client.model.Filters.*; import static com.mongodb.client.model.Updates.*;
MongoClient client = MongoClients.create("<<MongoDB URI>>");
To connect to MongoDB on your local instance and default port, you can just omit the URI part of the above, or use a URI like 'mongodb://localhost:27017'. For MongoDB Atlas, you can find the connection string in the cluster console – and make sure you've whitelisted your development IP address for access.
Once you're connected, you're off to the races!
MongoDatabase database = client.getDatabase(“my_database”); MongoCollection<Document> toys = database.getCollection(“toys”);
Let’s try writing some data:
Document toy = new Document(“name”, “yoyo”)
.append(“ages”, new Document(“min”, 5));
ObjectId id = toys.insertOne(toy).getInsertedId().asObjectId().getValue();
Retrieving your document again is just as simple:
Document yoyo = toys.find(new Document(“name”, “yoyo”)).first();
If you want to use your own Plain Old Java Objects (POJOs) rather than documents, you can use the MongoDB Java Driver’s built in POJO support, as well.
Given the wide support for every type of software on the planet in the Java community, the real question for Java programmers working with Mongo is, what application framework are you building in?