MongoDB session store for Express and Connect
6.xand all io.js versions
For older Node.js versions
0.12 and io.js, please read the Node.js compatibility section
5.0 and Connect
const session = ;const MongoStore = session;app;
3.x and Connect
const MongoStore = express;app;
2.x, just replace
In many circumstances,
connect-mongo will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.
Alternatively, you can configure
connect-mongo to establish a new connection.
const mongoose = ;// Basic usagemongoose;app;// Advanced usageconst connection = mongoose;app;
In this case, you just have to give your
Db instance to
If the connection is not opened,
connect-mongo will do it for you.
/*** There are many ways to create dbInstance.** You should refer to the driver documentation.*/app;
Or just give a promise...
// Basic usageapp;// Advanced usageapp;
0.12 and io.js, you must use the ES5 fallback:
var MongoStore = session;
When the session cookie has an expiration date,
connect-mongo will use it.
Otherwise, it will create a new one, using
Note: Each time an user interacts with the server, its session expiration date is refreshed.
connect-mongo uses MongoDB's TTL collection feature (2.2+) to have mongod automatically remove expired sessions. But you can change this behavior.
connect-mongo will create a TTL index for you at startup. You MUST have MongoDB 2.2+ and administration permissions.
Note: If you use
connect-mongo in a very concurrent environment, you should avoid this mode and prefer setting the index yourself, once!
You have an older MongoDB version (compatible with connect-mongo) or you can't or don't want to create a TTL index.
connect-mongo will take care of removing expired sessions, using defined interval.
You are in production environnement and/or you manage the TTL index elsewhere.
If you are using express-session >= 1.10.0 and don't want to resave all the session on database every single time that the user refresh the page, you can lazy update the session, by limiting a period of time.
by doing this, setting
touchAfter: 24 * 3600 you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)
MemoryStore. Useful if you want to use MemoryStore in some case, like in development environment.
stringifyIf true, connect-mongo will serialize sessions using
JSON.stringifybefore setting them, and deserialize them with
JSON.parsewhen getting them. (optional, default: true). This is useful if you are using types that MongoDB doesn't support.
serializeCustom hook for serializing sessions to MongoDB. This is helpful if you need to modify the session before writing it out.
unserializeCustom hook for unserializing sessions from MongoDB. This can be used in scenarios where you need to support different types of serializations (e.g., objects and JSON strings) or need to modify the session before using it in your app.
transformId(optional) Transform original sessionId in whatever you want to use as storage key.
The tests use a database called
The MIT License