connect-mongodb is a mongoDB session store backed by node-mongodb-native.
Originally written by dvv
via npm:
$ npm install connect-mongodb
If you update this module, please clean your sessions database as some changes may affect the way the sessions are stored.
You can build your MongoDB connection url passing an object with the following parameters:
dbnameMongoDB db name 'dev' by defaulthostMongoDB server hostname '127.0.0.1' by default (pass an array for replica set)portMongoDB server port 27017 by default (pass an array for replica set)usernameMongoDB server usernamepasswordMongoDB server password
Or just the url:
urlMongoDB connection url (comma separated list of urls for replica set)
It is also possible to pass instances of select node-mongodb-native classes, thus permitting the usage of existing connections or server configurations.
Using an existing connection:
handleExisting connection/database reference (instance of mongodb.Db)
Or with a server configuration:
serverConfigExisting server configuration (may be an instance of either mongodb.Server, mongodb.ServerPair, mongodb.ServerCluster, mongodb.ReplSetServers) - review node-mongodb-native docs.
Other options:
collectionMongoDB collection to host sessions. 'sessions' by defaultreapIntervalms to check expired sessions to remove on db
You have a complete example on examples/index.js.
var connect = require('connect'),
mongoStore = require('connect-mongodb');
connect.createServer(
connect.bodyParser(),
connect.cookieParser(),
connect.session({
cookie: {maxAge: 60000 * 20}, // 20 minutes
secret: 'foo',
store: new mongoStore({
dbname: 'production',
username: 'foo',
password: 'bar'
})
})
);
Instances running on separate machines:
{
cookie: {maxAge: 60000 * 20}, // 20 minutes
secret: 'foo',
store: new mongoStore({
host: ['xx.xxx.xxx.xx', 'xx.xxx.xx.xxx', 'xx.xxx.xx.xxx'],
port: 27017
})
}
...separate ports:
{
cookie: {maxAge: 60000 * 20}, // 20 minutes
secret: 'foo',
store: new mongoStore({
host: 'localhost',
port: [27017, 27017, 27018]
})
}
Or some combination:
{
cookie: {maxAge: 60000 * 20}, // 20 minutes
secret: 'foo',
store: new mongoStore({
host: ['xx.xxx.xxx.xx', 'xx.xxx.xx.xxx', 'xx.xxx.xx.xxx'],
port: [27017, 27017, 27018]
})
}