Quarkus — MongoDB Integration

November 3, 2022

Quarkus — MongoDB Integration

On one side we have supersonic, subatomic Java framework, Quarkus and on the other side cross-platform document oriented NoSql database, MongoDB. So, it must be fun to integrate these two platforms with each other and executes our application in a supersonic way.

What is Quarkus?

Quarkus is being developed by Red Hat in Java and it is a full-stack, Kubernetes-native Java application framework.

Embedded content: https://quarkus.io

The best two functionalities that I like about Quarkus are;

  • It is a combination of imperative coding & reactive coding,pls read the following link how it combines both;

Embedded content: https://quarkus.io/continuum/

  • It has really fast boot time comparing with other frameworks

I will not mention about how to setup your first Quarkus project because there are plenty of informative examples in its website. In order to react these examples about Quarkus, pls follow Quarkus link.

Quarkus & MongoDB Connection

I have my MongoDB image in my local Docker and run my MongoDB container from this image. As MongoDB driver is not implicitly embedded in Quarkus, I am using Panache library for the MongoDB connection.

First, add our db connection to our application.properties file. My MongoDB runs on 27017 port & my database name is enterprise.

Embedded content: https://gist.github.com/evrentan/22068213d206855f26208cfea5dd0821#file-application-properties

Creating our Entity with Panache

Panache works on top of MongoDB Client extension and it really makes life easier for you while creating entities in MongoDB. Just you need to extend your entity with “PanacheMongoEntity” and add @MongoEntity annotation to your class.

Here my collection name is enterprise in my enterprise DB. So, my entity has name attribute as notNull and with size restrictions and a webPage attribute. Also, I have ObjectId attribute in my enterprise DB which will see closer while creating our service to findById.

Embedded content: https://gist.github.com/evrentan/db523b0b09c5953ee3534d630d9adfce#file-enterpriseentity-java

Creating Interface & Service Class

Now, I create an interface for my abstract methods and I will implement these in my service class.

Embedded content: https://gist.github.com/evrentan/45d5f3461bdf1ebe9889f5cd30517c68#file-enterpriseinterface-java

I will not dive into details for my service class but important points are @ApplicationScoped & @Transactional annotations.

Also, we need to define our id as ObjectId in order to filter the collection values in our MongoDB.

Embedded content: https://gist.github.com/evrentan/7b9d5c60e4851838ed853be6e7a6f42a#file-enterpriseservice-java

Creating Resource Class

Reourse class is like our controller class in Quarkus. I implement APIs with RESTEasy, JBoss implementation of JAX-RS. JAX-RS stands for Java API for RESTful Web Services and it is a set of JAVA APIs in order to create REST webServices in our applications. You can find more detail about it from the below link.

Embedded content: https://docs.jboss.org/resteasy/docs/3.0.6.Final/userguide/html_single/index.html

It is easy to use and makes our code faster while implementing APIs.

Below you can find my resource class that is serving my enterprise collection in my MongoDB.

Embedded content: https://gist.github.com/evrentan/8ee155cd56feca5be4ed58d595418541#file-enterpriseresource-java

After that point, you only need to run your application and test your APIs serving to our collection.


To create a new enterprise instance in our collection;

create-new-enterprise-instance

To search for a specific enterprise in our collection;

search-enterprise-instance

To search for all enterprises in our collection;

search-all-enterprise-instances

To Sum Up

Integrating Quarkus with MongoDB is easy and you can design & use your entities quickly with Panache. And with the help of RESTEasy, it is really fast to implement APIs from your application.

You can find the code base of this example from the below link;

Embedded content: https://github.com/evrentan/my-quarkus-mongodb-example

Share: