Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


A guice module for connecting a Dropwizard app to a Mongo DB cluster

This JAR is loosely based off of, but re-written to support guice integration and to use Mongo URI connection strings.


This module assumes you are including it in a Dropwizard 0.8.1+ application with dropwizard-guice wiring.

Include as a maven dependency:


NOTE: this JAR assumes the composing application will provide a compatible version of com.hubspot.dropwizard.dropwizard-guice and a compatible version of org.mongodb.mongo-java-driver.

In your application's main initialize() method, add a new MongoBundle to your guice bundle, for example:

import com.hubspot.dropwizard.guice.GuiceBundle;
import com.washingtonpost.mongo.dropwizard.MongoModule;

public class MyApplication extends Application<MyConfiguration> {

    public void initialize(Bootstrap<MyConfiguration> bootstrap) {
        guiceBundle = GuiceBundle.<MyConfiguration>newBuilder()
                .addModule(new MongoModule())

Add configuration to your application's configuration class, like:

import com.washingtonpost.mongo.dropwizard.MongoFactory;

public class MyConfiguration extends Configuration {

    private MongoFactory mongoFactory = new MongoFactory();

    public MongoFactory getMongoFactory() {
        return this.mongoFactory;

    public void setMongoFactory(MongoFactory mongoFactory) {
        this.mongoFactory = mongoFactory;

Add reasonable configuration options to your application YML file:

    user: ${MONGO_USER}
    pass: ${MONGO_PASS}
    hosts: ${MONGO_HOSTS}
    dbName: ${MONGO_DBNAME}
    options: ${MONGO_OPTIONS}
    prefix: ${MONGO_PREFIX}

And finally make sure your Guice wiring "provides" the MongoFactory as a bean (it's needed by name "mongoFactory" in the MongoModule that you wired into your Guice bundle):

import com.washingtonpost.mongo.dropwizard.MongoFactory;
import javax.inject.Named;

public class MyModule extends AbstractModule {

    protected void configure() {

    public MongoFactory provideMongoFactory(MyConfiguration configuration) {
        return configuration.getMongoFactory();

Runtime DB Connections

If you don't know ahead of time what the name of your DB is going to be, you can create a DB object at runtime with the alternative method signature on the MongoFactory:

    MongoFactory factory = new MongoFactory();
    DB db = factory.buildDB("bar");


The Mongo configuration options map directly to the different parts of an allowed com.mongo.MongoClientURI, per the scheme defined here []

e.g. mongodb://[user:pass@][mongo_hosts,like:123,this:456][/[dbName][?options]]

    user: // the username to connect to any & all hosts defined in hosts, optional, but required if pass is provided
    pass: // the password associated with the username.  Optional, but required if user is provided
    hosts: // at least one, but possibly many comma-separated, host:port pairs hosting Mongo DBs.
    dbName: // optional name of the DB Collection to initially connect to when requesting MongoModule.provideDB
    options: // optional connection configuration parameters for the Mongo driver
    disabled: // optional boolean that will cause the Module to provide non-null, but non-functional DB/MongoClient objects
You can’t perform that action at this time.