Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

SessionStorage for connect's session middleware

branch: master
Readme.md

connect-mongodb

connect-mongodb is a mongoDB session store backed by node-mongodb-native.

Originally written by dvv

Installation

via npm:

$ npm install connect-mongodb

Updating

If you update this module, please clean your sessions database as some changes may affect the way the sessions are stored.

Options

You can build your MongoDB connection url passing an object with the following parameters:

  • dbname MongoDB db name 'dev' by default
  • host 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 username
  • password 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 default
  • reapInterval ms to check expired sessions to remove on db

Example

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'
    })
  })
);

Host - Port Examples for Replica Sets

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]
  })
}
Something went wrong with that request. Please try again.