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.
Use named() when you need two or more mongodb connections:
db1 = "mongodb://localhost/mydb1" db2 = "mongodb://localhost/mydb2"
{
use(new Mongodb("db1").named());
use(new Mongodb("db2").named());
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) |
protected void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder,
BiConsumer<com.mongodb.MongoClientURI,javax.inject.Provider<com.mongodb.MongoClient>> callback) |
protected <T> com.google.inject.Key<T> |
key(Class<T> type,
String db) |
Mongodb |
named()
Exposes services with
Named annotation. |
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)
configure in interface Jooby.Moduleprotected void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder, BiConsumer<com.mongodb.MongoClientURI,javax.inject.Provider<com.mongodb.MongoClient>> callback)
public Mongodb named()
Named annotation.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.ModuleCopyright © 2015. All rights reserved.