12.2.3 Configuring MongoDB

Setting up the Native MongoDB Driver

Using the CLI

If you are creating your project using the Micronaut CLI, supply the mongo-reactive feature to configure the native MongoDB driver in your project:

  1. $ mn create-app my-app features mongo-reactive

Micronaut includes a configuration to automatically configure the native MongoDB Java driver. To use this configuration, add the following dependency to your application:

  1. implementation("io.micronaut.mongodb:micronaut-mongo-reactive")
  1. <dependency>
  2. <groupId>io.micronaut.mongodb</groupId>
  3. <artifactId>micronaut-mongo-reactive</artifactId>
  4. </dependency>

Then configure the URI of the MongoDB server in application.yml:

Configuring a MongoDB server

  1. mongodb:
  2. uri: mongodb://username:password@localhost:27017/databaseName
The mongodb.uri follows the MongoDB Connection String format.

A non-blocking Reactive Streams MongoClient is then available for dependency injection.

To use the blocking driver, add a dependency to your application to the mongo-java-driver.

  1. compile "org.mongodb:mongo-java-driver"

Then the blocking MongoClient will be available for injection.

See the Micronaut MongoDB documentation for further information on configuring and using MongoDB within Micronaut.