mongodb driver

mongodb driver

MongoDB driver for Jooby.

exports

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-mongodb</artifactId>
  <version>1.5.1</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
  });
}

mongodb session store

dependency

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

usage

{
  use(new Mongodb());

  session(MongoSessionStore.class);

  get("/", req -> {
   req.session().set("name", "jooby");
  });
}

The name attribute and value will be stored in a MongoDB.

options

timeout

By default, a mongodb session will expire after 30 minutes. Changing the default timeout is as simple as:


# 8 hours 
session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

# no timeout 
session.timeout = -1

It uses MongoDB’s TTL collection feature (2.2+) to have mongod automatically remove expired sessions.

session collection

Default MongoDB collection is sessions.

It’s possible to change the default key setting the mongodb.sesssion.collection properties.

mongodb.conf

These are the default properties for mongodb:

################################################################################################### 
# mongodb 
################################################################################################### 
mongodb.connectionsPerHost = 100

mongodb.threadsAllowedToBlockForConnectionMultiplier = 5

mongodb.maxWaitTime = 120s

mongodb.connectTimeout = 10s

mongodb.socketTimeout = 0

mongodb.socketKeepAlive = false

mongodb.cursorFinalizerEnabled = true

mongodb.alwaysUseMBeans = false

mongodb.heartbeatFrequency = 5000

mongodb.minHeartbeatFrequency = 500

mongodb.heartbeatConnectTimeout = 20s

mongodb.heartbeatSocketTimeout = 20s

################################################################################################### 
# session datastore 
# collection: sessions 
# timeout: 30m 
################################################################################################### 
mongodb.session.collection = sessions