Looking for mongodb-java Answers? Try Ask4KnowledgeBase
Looking for mongodb-java Keywords? Try Ask4Keywords

mongodb-javaGetting started with mongodb-java


This section provides an overview of what mongodb-java is, and why a developer might want to use it.

It should also mention any large subjects within mongodb-java, and link out to the related topics. Since the Documentation for mongodb-java is new, you may need to create initial versions of those related topics.



Installation or Setup


A running MongoDB server, more details on that here.

Setting Up a Java Project

The MongoDB Java Driver is supplied as a JAR file and can be included in a project just like any other JAR. For example:

  • Maven: add an entry to the <dependencies> section of your pom.xml .
  • Gradle: add the following dependency to your build.gradle

compile 'org.mongodb:mongo-java-driver:3.4.2'

  • Manual Inclusion: download the JAR file from here and add it to your project's classpath.

Note: the version shown in the examples above is the latest stable version at the time of writing but you can choose any version available here.

Using the MongoDB Java Driver

Let's create a MongoClient , the simplest invocation is:

MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));

This presumes that the MongoDB server is running on localhost at the default port and that it is unsecured. There are numerous variants of this for authenticated access, custom connection options, connecting to replicasets and sharded clusters all covered in detail in the Mongo DB Java Driver docs.

Now that we're connected let's create a database and a collection. MongoDB implicitly creates a collection when the first document is saved into it so let's do that:

MongoDatabase database = mongoClient.getDatabase("starter");
MongoCollection<Document> collection = database.getCollection("superheroes");
Document document = new Document("name", "Superman");

Note: you can explicitly create a collection but the above usage is more common and certainly fits better with a 'getting started' topic.

Now let's see if we can find the document we created:

FindIterable<Document> documents = purchases.find(Filters.eq("name", "Superman"));
for (Document found : documents) {

The above code will output the following (the _id field is MongoDB's auto generated document key):

Found document: { "_id" : { "$oid" : "5981bd586d47c203904a9cf9" }, "name" : "Superman" }

Now that we've verified that we successfully created a document let's delete it:

DeleteResult deleteResult = purchases.deleteOne(Filters.eq("name", "Superman"));
System.out.println(String.format("Deleted %s document(s)", deleteResult.getDeletedCount()));

The above code will output the following:

Deleted 1 document(s)

Complete Example

Putting the various pieces together we get:

import com.google.common.collect.Lists;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import org.bson.Document;

import java.util.List;

public class IntroducingTheMongoDBJavaDriver {

    public static void main(String... args) throws Exception {
        MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));

        MongoCollection<Document> superheroes = mongoClient.getDatabase("starter").getCollection("superheroes");

        Document document = new Document("name", "Superman");

        FindIterable<Document> documents = superheroes.find(Filters.eq("name", "Superman"));
        for (Document found : documents) {
            System.out.println(String.format("Found document: %s", found.toJson()));

        DeleteResult deleteResult = superheroes.deleteOne(Filters.eq("name", "Superman"));
        System.out.println(String.format("Deleted %s document(s)", deleteResult.getDeletedCount()));

        long count = superheroes.count();
        System.out.println(String.format("Number of superheroes: %s", count));