Quarkus - Using the MongoDB Client

MongoDB is a well known NoSQL Database that is widely used.

In this guide, we see how you can get your REST services to use the MongoDB database.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.5.3+

  • MongoDB installed or Docker installed

Architecture

The application built in this guide is quite simple: the user can add elements in a list using a form and the list is updated.

All the information between the browser and the server is formatted as JSON.

The elements are stored in MongoDB.

Solution

We recommend that you follow the instructions in the next sections and create the application step by step.However, you can go right to the completed example.

Clone the Git repository: git clone https://github.com/quarkusio/quarkus-quickstarts.git, or download an archive.

The solution is located in the mongodb-quickstart directory.

Creating the Maven project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=mongodb-quickstart \
  4. -DclassName="org.acme.rest.json.FruitResource" \
  5. -Dpath="/fruits" \
  6. -Dextensions="resteasy-jsonb,mongodb-client"
  7. cd mongodb-quickstart

This command generates a Maven structure importing the RESTEasy/JAX-RS, JSON-B and MongoDB Client extensions.After this, the quarkus-mongodb-client extension has been added to your pom.xml.

Creating your first JSON REST service

In this example, we will create an application to manage a list of fruits.

First, let’s create the Fruit bean as follows:

  1. package org.acme.rest.json;
  2. import java.util.Objects;
  3. public class Fruit {
  4. private String name;
  5. private String description;
  6. public Fruit() {
  7. }
  8. public Fruit(String name, String description) {
  9. this.name = name;
  10. this.description = description;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getDescription() {
  19. return description;
  20. }
  21. public void setDescription(String description) {
  22. this.description = description;
  23. }
  24. @Override
  25. public boolean equals(Object obj) {
  26. if (!(obj instanceof Fruit)) {
  27. return false;
  28. }
  29. Fruit other = (Fruit) obj;
  30. return Objects.equals(other.name, this.name);
  31. }
  32. @Override
  33. public int hashCode() {
  34. return Objects.hash(this.name);
  35. }
  36. }

Nothing fancy. One important thing to note is that having a default constructor is required by the JSON serialization layer.

Now create a org.acme.rest.json.FruitService that will be the business layer of our application and store/load the fruits from the mongoDB database.

  1. package org.acme.rest.json;
  2. import com.mongodb.client.MongoClient;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.MongoCursor;
  5. import org.bson.Document;
  6. import javax.enterprise.context.ApplicationScoped;
  7. import javax.inject.Inject;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. @ApplicationScoped
  11. public class FruitService {
  12. @Inject MongoClient mongoClient;
  13. public List<Fruit> list(){
  14. List<Fruit> list = new ArrayList<>();
  15. MongoCursor<Document> cursor = getCollection().find().iterator();
  16. try {
  17. while (cursor.hasNext()) {
  18. Document document = cursor.next();
  19. Fruit fruit = new Fruit();
  20. fruit.setName(document.getString("name"));
  21. fruit.setDescription(document.getString("description"));
  22. list.add(fruit);
  23. }
  24. } finally {
  25. cursor.close();
  26. }
  27. return list;
  28. }
  29. public void add(Fruit fruit){
  30. Document document = new Document()
  31. .append("name", fruit.getName())
  32. .append("description", fruit.getDescription());
  33. getCollection().insertOne(document);
  34. }
  35. private MongoCollection getCollection(){
  36. return mongoClient.getDatabase("fruit").getCollection("fruit");
  37. }
  38. }

Now, edit the org.acme.rest.json.FruitResource class as follows:

  1. @Path("/fruits")
  2. @Produces(MediaType.APPLICATION_JSON)
  3. @Consumes(MediaType.APPLICATION_JSON)
  4. public class FruitResource {
  5. @Inject FruitService fruitService;
  6. @GET
  7. public List<Fruit> list() {
  8. return fruitService.list();
  9. }
  10. @POST
  11. public List<Fruit> add(Fruit fruit) {
  12. fruitService.add(fruit);
  13. return list();
  14. }
  15. }

The implementation is pretty straightforward and you just need to define your endpoints using the JAX-RS annotations and use the FruitService to list/add new fruits.

Configuring the MongoDB database

The main property to configure is the URL to access to MongoDB, almost all configuration can be included in the connection URI so we advise you to do so, you can find more information in the MongoDB documentation: https://docs.mongodb.com/manual/reference/connection-string/

A sample configuration should look like this:

  1. # configure the mongoDB client for a replica set of two nodes
  2. quarkus.mongodb.connection-string = mongodb://mongo1:27017,mongo2:27017

In this example, we are using a single instance running on localhost:

  1. # configure the mongoDB client for a single instance on localhost
  2. quarkus.mongodb.connection-string = mongodb://localhost:27017

If you need more configuration properties, there is a full list at the end of this guide.

Running a MongoDB Database

As by default, MongoClient is configured to access a local MongoDB database on port 27017 (the default MongoDB port), if you have a local running database on this port, there is nothing more to do before being able to test it!

If you want to use Docker to run a MongoDB database, you can use the following command to launch one:

  1. docker run -ti --rm -p 27017:27017 mongo:4.0

Creating a frontend

Now let’s add a simple web page to interact with our FruitResource.Quarkus automatically serves static resources located under the META-INF/resources directory.In the src/main/resources/META-INF/resources directory, add a fruits.html file with the content from this fruits.html file in it.

You can now interact with your REST service:

Reactive MongoDB Client

A reactive MongoDB Client is included in Quarkus.Using it is as easy as using the classic MongoDB Client.You can rewrite the previous example to use it like the following.

  1. package org.acme.rest.json;
  2. import io.quarkus.mongodb.ReactiveMongoClient;
  3. import io.quarkus.mongodb.ReactiveMongoCollection;
  4. import org.bson.Document;
  5. import javax.enterprise.context.ApplicationScoped;
  6. import javax.inject.Inject;
  7. import java.util.List;
  8. import java.util.concurrent.CompletionStage;
  9. @ApplicationScoped
  10. public class ReactiveFruitService {
  11. @Inject
  12. ReactiveMongoClient mongoClient;
  13. public CompletionStage<List<Fruit>> list(){
  14. return getCollection().find().map(doc -> {
  15. Fruit fruit = new Fruit();
  16. fruit.setName(doc.getString("name"));
  17. fruit.setDescription(doc.getString("description"));
  18. return fruit;
  19. }).toList().run();
  20. }
  21. public CompletionStage<Void> add(Fruit fruit){
  22. Document document = new Document()
  23. .append("name", fruit.getName())
  24. .append("description", fruit.getDescription());
  25. return getCollection().insertOne(document);
  26. }
  27. private ReactiveMongoCollection<Document> getCollection(){
  28. return mongoClient.getDatabase("fruit").getCollection("fruit");
  29. }
  30. }
  1. package org.acme.rest.json;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.*;
  4. import javax.ws.rs.core.MediaType;
  5. import java.util.List;
  6. import java.util.concurrent.CompletionStage;
  7. @Path("/reactive_fruits")
  8. @Produces(MediaType.APPLICATION_JSON)
  9. @Consumes(MediaType.APPLICATION_JSON)
  10. public class ReactiveFruitResource {
  11. @Inject ReactiveFruitService fruitService;
  12. @GET
  13. public CompletionStage<List<Fruit>> list() {
  14. return fruitService.list();
  15. }
  16. @POST
  17. public CompletionStage<List<Fruit>> add(Fruit fruit) {
  18. fruitService.add(fruit);
  19. return list();
  20. }
  21. }

Simplifying MongoDB Client usage using BSON codec

By using a Bson Codec, the MongoDB Client will take care of the transformation of your domain object to/from a MongoDB Document automatically.

First you need to create a Bson Codec that will tell Bson how to transform your entity to/from a MongoDB Document.Here we use a CollectibleCodec as our object is retrievable from the database (it has a MongoDB identifier), if not we would have used a Codec instead.More information in the codec documentation: https://mongodb.github.io/mongo-java-driver/3.10/bson/codecs.

  1. package org.acme.rest.json.codec;
  2. import com.mongodb.MongoClient;
  3. import org.acme.rest.json.Fruit;
  4. import org.bson.*;
  5. import org.bson.codecs.Codec;
  6. import org.bson.codecs.CollectibleCodec;
  7. import org.bson.codecs.DecoderContext;
  8. import org.bson.codecs.EncoderContext;
  9. import java.util.UUID;
  10. public class FruitCodec implements CollectibleCodec<Fruit> {
  11. private final Codec<Document> documentCodec;
  12. public FruitCodec() {
  13. this.documentCodec = MongoClient.getDefaultCodecRegistry().get(Document.class);
  14. }
  15. @Override
  16. public void encode(BsonWriter writer, Fruit fruit, EncoderContext encoderContext) {
  17. Document doc = new Document();
  18. doc.put("name", fruit.getName());
  19. doc.put("description", fruit.getDescription());
  20. documentCodec.encode(writer, doc, encoderContext);
  21. }
  22. @Override
  23. public Class<Fruit> getEncoderClass() {
  24. return Fruit.class;
  25. }
  26. @Override
  27. public Fruit generateIdIfAbsentFromDocument(Fruit document) {
  28. if (!documentHasId(document)) {
  29. document.setId(UUID.randomUUID().toString());
  30. }
  31. return document;
  32. }
  33. @Override
  34. public boolean documentHasId(Fruit document) {
  35. return document.getId() != null;
  36. }
  37. @Override
  38. public BsonValue getDocumentId(Fruit document) {
  39. return new BsonString(document.getId());
  40. }
  41. @Override
  42. public Fruit decode(BsonReader reader, DecoderContext decoderContext) {
  43. Document document = documentCodec.decode(reader, decoderContext);
  44. Fruit fruit = new Fruit();
  45. if (document.getString("id") != null) {
  46. fruit.setId(document.getString("id"));
  47. }
  48. fruit.setName(document.getString("name"));
  49. fruit.setDescription(document.getString("description"));
  50. return fruit;
  51. }
  52. }

Then you need to create a CodecProvider to link this Codec to the Fruit class.

  1. package org.acme.rest.json.codec;
  2. import org.acme.rest.json.Fruit;
  3. import org.bson.codecs.Codec;
  4. import org.bson.codecs.configuration.CodecProvider;
  5. import org.bson.codecs.configuration.CodecRegistry;
  6. public class FruitCodecProvider implements CodecProvider {
  7. @Override
  8. public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
  9. if (clazz == Fruit.class) {
  10. return (Codec<T>) new FruitCodec();
  11. }
  12. return null;
  13. }
  14. }

Quarkus takes care of registering the CodecProvider for you.

Finally, when getting the MongoCollection from the database you can use directly the Fruit class instead of the Document one, the codec will automatically map the Document to/from your Fruit class.

Here is an example of using a MongoCollection with the FruitCodec.

  1. package org.acme.rest.json;
  2. import com.mongodb.client.MongoClient;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.MongoCursor;
  5. import javax.enterprise.context.ApplicationScoped;
  6. import javax.inject.Inject;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @ApplicationScoped
  10. public class CodecFruitService {
  11. @Inject MongoClient mongoClient;
  12. public List<Fruit> list(){
  13. List<Fruit> list = new ArrayList<>();
  14. MongoCursor<Fruit> cursor = getCollection().find().iterator();
  15. try {
  16. while (cursor.hasNext()) {
  17. list.add(cursor.next());
  18. }
  19. } finally {
  20. cursor.close();
  21. }
  22. return list;
  23. }
  24. public void add(Fruit fruit){
  25. getCollection().insertOne(fruit);
  26. }
  27. private MongoCollection<Fruit> getCollection(){
  28. return mongoClient.getDatabase("fruit").getCollection("fruit", Fruit.class);
  29. }
  30. }

Simplifying MongoDB with Panache

The MongoDB with Panache extension facilitates the usage of MongoDB by providing active record style entities (and repositories) like you have in Hibernate ORM with Panache and focuses on making your entities trivial and fun to write in Quarkus.

Building a native executable

You can use the MongoDB client in a native executable.

If you want to use SSL/TLS encryption, you need to add these properties in your application.properties:

  1. quarkus.mongodb.tls=true
  2. quarkus.mongodb.tls-insecure=true # only if TLS certificate cannot be validated

You can then build a native executable with the usual command ./mvnw package -Pnative.

Running it is as simple as executing ./target/mongodb-quickstart-1.0-SNAPSHOT-runner.

You can then point your browser to http://localhost:8080/fruits.html and use your application.

Currently, Quarkus doesn’t support the mongodb+srv protocol in native mode.

Conclusion

Accessing a MongoDB database from a MongoDB Client is easy with Quarkus as it provides configuration and native support for it.

Configuration Reference

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.mongodb.connection-stringConfigures the connection string. The format is: mongodb://[username:password@]host1[:port1][,host2[:port2],…​[,hostN[:portN]]][/[database.collection][?options]] mongodb:// is a required prefix to identify that this is a string in the standard connection format. username:password@ are optional. If given, the driver will attempt to login to a database after connecting to a database server. For some authentication mechanisms, only the username is specified and the password is not, in which case the ":" after the username is left off as well. host1 is the only required part of the connection string. It identifies a server address to connect to. :portX is optional and defaults to :27017 if not provided. /database is the name of the database to login to and thus is only relevant if the username:password@ syntax is used. If not specified the admin database will be used by default. ?options are connection options. Note that if database is absent there is still a / required between the last host and the ? introducing the options. Options are name=value pairs and the pairs are separated by "&". An alternative format, using the mongodb+srv protocol, is: mongodb+srv://[username:password@]host[/[database][?options]] - mongodb+srv:// is a required prefix for this format. - username:password@ are optional. If given, the driver will attempt to login to a database after connecting to a database server. For some authentication mechanisms, only the username is specified and the password is not, in which case the ":" after the username is left off as well - host is the only required part of the URI. It identifies a single host name for which SRV records are looked up from a Domain Name Server after prefixing the host name with "_mongodb._tcp". The host/port for each SRV record becomes the seed list used to connect, as if each one were provided as host/port pair in a URI using the normal mongodb protocol. - /database is the name of the database to login to and thus is only relevant if the username:password@ syntax is used. If not specified the "admin" database will be used by default. - ?options are connection options. Note that if database is absent there is still a / required between the last host and the ? introducing the options. Options are name=value pairs and the pairs are separated by "&". Additionally with the mongodb+srv protocol, TXT records are looked up from a Domain Name Server for the given host, and the text value of each one is prepended to any options on the URI itself. Because the last specified value for any option wins, that means that options provided on the URI will override any that are provided via TXT records.string
quarkus.mongodb.hostsConfigures the Mongo server addressed (one if single mode). The addressed are passed as host:port.list of stringrequired
quarkus.mongodb.databaseConfigure the database name.string
quarkus.mongodb.application-nameConfigures the application name.string
quarkus.mongodb.max-pool-sizeConfigures the maximum number of connections in the connection pool.int
quarkus.mongodb.min-pool-sizeConfigures the minimum number of connections in the connection pool.int
quarkus.mongodb.max-connection-idle-timeMaximum idle time of a pooled connection. A connection that exceeds this limit will be closed.Duration
quarkus.mongodb.max-connection-life-timeMaximum life time of a pooled connection. A connection that exceeds this limit will be closed.Duration
quarkus.mongodb.wait-queue-timeoutThe maximum wait time that a thread may wait for a connection to become available.Duration
quarkus.mongodb.maintenance-frequencyConfigures the time period between runs of the maintenance job.Duration
quarkus.mongodb.maintenance-initial-delayConfigures period of time to wait before running the first maintenance job on the connection pool.Duration
quarkus.mongodb.wait-queue-multipleThis multiplier, multiplied with the maxPoolSize setting, gives the maximum number of threads that may be waiting for a connection to become available from the pool. All further threads will get an exception right away.int
quarkus.mongodb.connect-timeoutHow long a connection can take to be opened before timing out.Duration
quarkus.mongodb.socket-timeoutHow long a send or receive on a socket can take before timing out.Duration
quarkus.mongodb.tls-insecureIf connecting with TLS, this option enables insecure TLS connections.booleanfalse
quarkus.mongodb.tlsWhether to connect using TLS.booleanfalse
quarkus.mongodb.replica-set-nameImplies that the hosts given are a seed list, and the driver will attempt to find all members of the set.string
quarkus.mongodb.server-selection-timeoutHow long the driver will wait for server selection to succeed before throwing an exception.Duration
quarkus.mongodb.local-thresholdWhen choosing among multiple MongoDB servers to send a request, the driver will only send that request to a server whose ping time is less than or equal to the server with the fastest ping time plus the local threshold.Duration
quarkus.mongodb.heartbeat-frequencyThe frequency that the driver will attempt to determine the current state of each server in the cluster.Duration
quarkus.mongodb.read-preferenceConfigures the read preferences. Supported values are: primary|primaryPreferred|secondary|secondaryPreferred|neareststring
quarkus.mongodb.max-wait-queue-sizeConfigures the maximum number of concurrent operations allowed to wait for a server to become available. All further operations will get an exception immediately.int
Write concernTypeDefault
quarkus.mongodb.write-concern.safeConfigures the safety. If set to true: the driver ensures that all writes are acknowledged by the MongoDB server, or else throws an exception. (see also w and wtimeoutMS). If set fo - false: the driver does not ensure that all writes are acknowledged by the MongoDB server.booleantrue
quarkus.mongodb.write-concern.journalConfigures the journal writing aspect. If set to true: the driver waits for the server to group commit to the journal file on disk. If set to false: the driver does not wait for the server to group commit to the journal file on disk.booleantrue
quarkus.mongodb.write-concern.wWhen set, the driver adds w: wValue to all write commands. It requires safe to be true. The value is typically a number, but can also be the majority string.string
quarkus.mongodb.write-concern.retry-writesIf set to true, the driver will retry supported write operations if they fail due to a network error.booleanfalse
quarkus.mongodb.write-concern.w-timeoutWhen set, the driver adds wtimeout : ms to all write commands. It requires safe to be true.Duration
Credentials and authentication mechanismTypeDefault
quarkus.mongodb.credentials.usernameConfigures the username.string
quarkus.mongodb.credentials.passwordConfigures the password.string
quarkus.mongodb.credentials.auth-mechanismConfigures the authentication mechanism to use if a credential was supplied. The default is unspecified, in which case the client will pick the most secure mechanism available based on the sever version. For the GSSAPI and MONGODB-X509 mechanisms, no password is accepted, only the username. Supported values: MONGO-CR|GSSAPI|PLAIN|MONGODB-X509string
quarkus.mongodb.credentials.auth-sourceConfigures the source of the authentication credentials. This is typically the database that the credentials have been created. The value defaults to the database specified in the path portion of the connection string or in the 'database' configuration property.. If the database is specified in neither place, the default value is admin. This option is only respected when using the MONGO-CR mechanism (the default).string
quarkus.mongodb.credentials.auth-mechanism-propertiesAllows passing authentication mechanism properties.Map<String,String>required
About the Duration formatThe format for durations uses the standard java.time.Duration format.You can learn more about it in the Duration#parse() javadoc.You can also provide duration values starting with a number.In this case, if the value consists only of a number, the converter treats the value as seconds.Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.