public class Mongodb extends Object implements Jooby.Module
MongoClient
and a DB
.
application.conf:
db = "mongodb://localhost/mydb"
{ use(new Mongodb()); get("/", req -> { MongoClient client = req.require(MongoClient.class); // work with client DB = req.require(DB.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 -> { DB mydb = req.require(DB.class); // work with mydb }); }
Options can be set via .conf
file:
mongodb.connectionsPerHost = 100
or programmatically:
{ use(new Mongodb() .options((options, config) -> { options.connectionsPerHost(100); }) ); }
Default connection URI is defined by the db
property. Mongodb URI looks like:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]For more detailed information please check:
MongoClientURI
.
db1 = "mongodb://localhost/mydb1" db2 = "mongodb://localhost/mydb2"
{ use(new Mongodb("db1")); use(new Mongodb("db2")); get("/", req -> { MongoClient client1 = req.require("mydb1", MongoClient.class); // work with mydb1 MongoClient client2 = req.require("mydb2", MongoClient.class); // work with mydb1 }); }
Constructor and Description |
---|
Mongodb()
Creates a new Mongodb using the default property: db .
|
Mongodb(String db)
Creates a new Mongodb module.
|
Modifier and Type | Method and Description |
---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
Configure and produces bindings for the underlying application.
|
Mongodb |
options(BiConsumer<com.mongodb.MongoClientOptions.Builder,com.typesafe.config.Config> options)
Set an options callback.
|
public Mongodb(String db)
Mongodb
module.
db
- Name of the property with the connection URI.
public Mongodb()
Mongodb
using the default property: db
.
public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
Jooby.Module
application env
and/or the current application properties available from Config
.
configure
in interface Jooby.Module
env
- The current application's env. Not null.
config
- The current config object. Not null.
binder
- A guice binder. Not null.
public Mongodb options(BiConsumer<com.mongodb.MongoClientOptions.Builder,com.typesafe.config.Config> options)
{ use(new Mongodb() .options((options, config) -> { options.connectionsPerHost(100); }) ); }
options
- Configure callback.
public com.typesafe.config.Config config()
config
in interface Jooby.Module
Copyright © 2019. All rights reserved.