Manipulate with MongoDB in Java application

MongoDB is a NoSQL database that allows us to store complex, unstructured data types. In this tutorial, I will walk you through the basic steps to work with MongoDB in your Java applications.

First, I will create a new Maven project as an example:

Manipulate with MongoDB in Java application

Similar to other database systems, in order to work with Java, we need to declare to use the MongoDB driver in our application.

OK, let’s get started.



To connect to MongoDB, we will use the MongoClient object from its Driver library with the following declaration:

Where the MongoClientURI object is the object that will manage the MongoDB information including the IP address and port that MongoDB is running. The MongoClient object will be the object responsible for connecting to MongoDB.

In case you are using MongoDB server with authentication mode, then you need to declare username and password are assigned to the database that you are going to use.

Then, connect to MongoDB with the following code:

I will use MongoDB server without authentication mode in this tutorial.

By default, MongoDB will run on port 27017 and if you are installing MongoDB on your local machine then we do not need to declare the MongoClientURI object. You simply declare the following:

I’m using MongoDB on my machine so I just declare it like that!

At this point, if you run the application:

You will see the following results:

Manipulate with MongoDB in Java application

Look at the log, you can see that our application is connected to MongoDB.


Once connected to MongoDB, now you can work with it.

First, you can get a list of existing databases in your current MongoDB using the listDatabaseNames() method.

For example:

Result:

Manipulate with MongoDB in Java application

We can select the database to use the getDatabase() method of the MongoClient object.

For example, if I have a database called mongodb in MongoDB, my code will look like this:

Just like when using the command line with MongoDB, if the database we choose to manipulate does not exist, this database will be automatically created.

MongoDatabase object will hold all information about the Collections in the database that we are using. Therefore, from this MongoDatabase object, you can get the names of all Collections using the listCollectionNames() method.

My database “mongodb” is having the following Collections:

Manipulate with MongoDB in Java application

The result would be:

Manipulate with MongoDB in Java application


To work with a Collection, you can use the getCollection() method of the MongoDatabase object to get the MongoCollection object for the Collection.

My example is as follows:

If you want to insert a Document into the Collection you can use the insertOne() method to insert a Document or insertMany() to insert multiple Documents at the same time.

A Document is a JSON string and we will use the Document object of the MongoDB Driver API to build it. For example, now I want to build a Document of student information including name and age, I will create Document object as follows:

then write code to insert a new Document:

Result:

Manipulate with MongoDB in Java application


Of course, you can also check the result from the code by using the find() method of the MongoCollection object.

This find() method has a Document object argument, which is the criteria for finding all the Documents in the current Collection with similar content.

In the example above, I looked for the Student Document in terms of name and age. You can expand the search results by one of these two criteria also.

Result:

Manipulate with MongoDB in Java application


You can also update one or more Documents at the same time.

We will use the updateOne() or updateMany() methods of the MongoCollection object to do this.

There are many methods of overloading these two methods with different parameters. But basically, there are two main parameters, the Document object to find the Document need to update, the second Document object contains the content to update.

In my example, suppose I need to update the age of students named Khanh to 32, then I will write the code as follows:

As you can see, the Document object containing the content to be updated needs to be built with the keyword “$ set” and its value is the information the student needs to update.

Result:

Manipulate with MongoDB in Java application


And you can also delete one or more documents at once using the deleteOne() and deleteMany() methods.

For example:

Result:

Manipulate with MongoDB in Java application

 

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment