Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

mongodb driver

{{mongodb}} driver for Jooby.

exports

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-mongodb</artifactId>
  <version>{{version}}</version>
</dependency>

usage

application.conf:

db = "mongodb://localhost/mydb"
{
  use(new Mongodb());

  get("/", req -> {
    MongoClient client = require(MongoClient.class);
    // work with client
    MongoDatabase = require(MongoDatabase.class);
    // work with mydb
  });
}

Default URI connection property is db but of course you can use any other name:

application.conf:

mydb = "mongodb://localhost/mydb"
{
  use(new Mongodb("mydb"));

  get("/", req -> {
    MongoDatabase mydb = require(MongoDatabase.class);
    // work with mydb
  });
}

options

Options can be set via .conf file:

mongodb.connectionsPerHost  = 100

or programmatically:

{
  use(new Mongodb()
    .options((options, config) -> {
      options.connectionsPerHost(100);
    })
  );
}

connection URI

Default connection URI is defined by the db property. Mongodb URI looks like:

db = mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

For more detailed information please check: MongoClientURI.

two or more connections

Use named when you need two or more mongodb connections:

{
  use(new Mongodb("db1"));
  use(new Mongodb("db2"));

  get("/", req -> {
    MongoClient client1 = require("db1", MongoClient.class);
    // work with db1
    MongoClient client2 = require("db2", MongoClient.class);
    // work with db2
  });
}

{{doc/mongodb/mongodb-session.md}}

{{appendix}}