MongoDB initializer for app-context
This initializer can be auto-installed by using it in your context file.
This initializer will attach the configured connections to APP.mongodb
.
module.exports = function() {
this.runlevel('connected')
// attach a connection to APP.mongodb - use the value at APP.config.mongodb as the connection string
.use('mongodb', '$mongodb')
// attach a connection to APP.mongodb
.use('mongodb', 'mongodb://localhost/foobar')
// create 2 connections and attach them as an object to APP.mongodb
// this will create APP.mongodb.users and APP.mongodb.data
.use('mongodb', {
users: '$mongodb.users',
data: '$mongodb.data'
})
// you can also pass options to each connection
// (from http://mongodb.github.io/node-mongodb-native/2.0/api/MongoClient.html#.connect)
.use('mongodb', {
users: {url: '$mongodb.users', db: {
wtimeout: 2000,
authSource: 'other-db'
}},
data: {url: 'mongodb://localhost/data', server: {
reconnectTries: 2,
reconnectInterval: 500
}}
})
};
Connection string formats and options are available in the mongodb driver docs.
Each connection can be configured with either a connection string (like mongodb://localhost/foobar
) or
with an object. The object will be passed through to MongoClient.connect
and can consist of any options
from the mongodb driver. There is a special url
option that this initializer requires.