How to Create a MongoDB Database and Collection using Java in the Eclipse IDE


If you’re planning to work with MongoDB, you’ll need to set up a database and at least one collection before you can do anything else. With the help of the MongoDB Java driver, it’s easy to accomplish these tasks using Java. All it takes is some simple code, which can be written in the Eclipse IDE, to access MongoDB, create a new document and insert that document into a collection. In this article, we’ll provide step-by-step instructions for creating a MongoDB database and collection using Java in Eclipse IDE.


Before we can delve into the Java code needed to work with MongoDB, it’s important to review the system requirements for this task. There are a couple of key prerequisites:

  • First, you need to confirm that both MongoDB and the MongoDB Java driver have been properly configured beforehand.

  • You’ll also need to confirm that the latest Java JDK has been properly installed and configured beforehand.

  • In this article, Eclipse IDE will be used. While you can use other IDEs to write your code, it will be easier to follow along with the instructions if you’re using Eclipse.

Starting the MongoDB Database Daemon

Once you’ve made sure that all the system requirements are met, you can proceed to the next step: starting the MongoDB database daemon. To do this, open your terminal by pressing Ctrl + Alt + T. You can start the MongoDB service and check the status of it with the command shown below. It’s a good idea to use sudo when executing this command to avoid permission-related problems when starting up the MongoDB service:

sudo systemctl start mongod
sudo systemctl status mongod

The output should look something like this:

‚óè mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
Active: active (running) since Mon 2019-04-22 19:27:02 PST; 8min ago
Main PID: 6598 (mongod)
CGroup: /system.slice/mongod.service
└─6598 /usr/bin/mongod --config /etc/mongod.conf

Apr 22 19:27:02 teamsolo systemd[1]: Started MongoDB Database Server.
lines 1-9/9 (END)

NOTE: As of May 2019, the most recent version of MongoDB is 4.0, and the MongoDB Java Driver version is 3.8.2.

Connecting to the MongoClient

Let’s turn to our Java code at this point and see how we can talk to MongoDB. To connect a MongoClient instance to your MongoDB deployment, use the following command:

MongoClient mongo = MongoClients.create("mongodb://");

Accessing the Database

Once you’ve connected the MongoClient instance to a MongoDB deployment, you can use the command shown below to access the database:

MongoDatabase db = mongo.getDatabase("PlayerDB");

You can see that the getDatabase() method accepts the name of the database that you’d like to connect to.

NOTE: If the database you specify does not exist, MongoDB will create the database with that name when the first data is stored in it.

Creating a MongoDB Document

Next, we’ll add some code to create a MongoDB document. To accomplish this task, a Document object that contains a field and value must be instantiated. Then, you can use the append() method to add additional fields and values to the document object.

Document doc = new Document("playerName", "Ronaldo")
.append("age", 25)
.append("nationality", "Filipino")
.append("JerseyNumber", 23)
.append("position", "Guard");

Inserting a MongoDB Document

Now that we’ve created a document, let’s insert it into a MongoDB collection. To insert a single document, use the insertOne() method as shown below:


NOTE: If the collection you specify does not exist, MongoDB will create the collection when the first data is inserted into it.

The Code

Throughout this tutorial, we’ve looked at the Java code one section at a time. Below you can find the entire code needed to connect to MongoDB, access a database, create a document and add it to a collection. This code can be placed in either the Main class or a function:

MongoClient mongo = MongoClients.create("mongodb://");

MongoDatabase db = mongo.getDatabase("PlayerDB");
Document doc = new Document("playerName", "Ronaldo")
.append("age", 25)
.append("nationality", "Filipino")
.append("JerseyNumber", 23)
.append("position", "Guard");


The results will look something like this in the Mongo shell:

"_id" : ObjectId("5cd56e315623d0556fb0b45e"),
"playerName" : "Ronaldo",
"age" : 25,
"nationality" : "Filipino",
"JerseyNumber" : 23,
"position" : "Guard"

NOTE: If no specified value is provided for _id in the document, MongoDB will automatically generate one and insert that value in the document.


If you’re a Java developer getting started with MongoDB, there are a few basic tasks that you need to be able to accomplish before you can start querying data. Knowing how to create and access both a database and a collection are essential skills that will allow you to perform a wide range of MongoDB operations. With the instructions provided in this article, you’ll have no trouble creating a MongoDB database and collection using Java in Eclipse IDE.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.