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:
dbname
MongoDB db name 'dev' by defaulthost
MongoDB server hostname '127.0.0.1' by default (pass an array for replica set)port
MongoDB server port 27017 by default (pass an array for replica set)username
MongoDB server usernamepassword
MongoDB server password
Or just the url:
url
MongoDB 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:
handle
Existing connection/database reference (instance of mongodb.Db)
Or with a server configuration:
serverConfig
Existing server configuration (may be an instance of either mongodb.Server, mongodb.ServerPair, mongodb.ServerCluster, mongodb.ReplSetServers) - review node-mongodb-native docs.
Other options:
collection
MongoDB collection to host sessions. 'sessions' by defaultreapInterval
ms 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]
})
}