Registry / database / connect-mongodb-session

connect-mongodb-session

JSON →
library5.0.0jsnpmunverified

MongoDB-backed session store for Connect and Express. Version 5.0.0, maintained by MongoDB. Supports Express 4 with express-session. Unlike connect-mongo, it uses native MongoDB driver with explicit connection string, database name, and collection. Provides event-based error handling and optional constructor callback. Actively maintained with frequent releases.

npm install connect-mongodb-session
INSTALL
IMPORT
SIG · CONNECT-MONGODB-SE
C
connect-mongodb-session
databasejavascriptv5.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MongoDBStore
import pkg from 'connect-mongodb-session'; const MongoDBStore = pkg(session);
import { MongoDBStore } from 'connect-mongodb-session';
Default export is a function that takes express-session and returns the store class.
MongoDBStore (require)
const MongoDBStore = require('connect-mongodb-session')(session);
const { MongoDBStore } = require('connect-mongodb-session');
CommonJS: require returns the function directly, must call with express-session.
session (express-session)
import session from 'express-session';
const session = require('express-session');
CommonJS require is acceptable but ESM import is preferred for modern projects.
Express session setup
app.use(session({...options}));
app.use(require('express-session')({...}));
Pass the middleware directly; do not call express-session inline incorrectly.

Sets up Express app with MongoDB session storage using connect-mongodb-session. Includes error handling and session view counter.

import express from 'express'; import session from 'express-session'; import connectMongo from 'connect-mongodb-session'; const MongoDBStore = connectMongo(session); const app = express(); const store = new MongoDBStore({ uri: process.env.MONGO_URI ?? 'mongodb://localhost:27017/myapp', databaseName: 'myapp', collection: 'sessions' }); store.on('error', (error) => console.error('Session store error:', error)); app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: true, store: store })); app.get('/', (req, res) => { req.session.views = (req.session.views ?? 0) + 1; res.send(`Views: ${req.session.views}`); }); app.listen(3000, () => console.log('Server started on port 3000'));
Debug
Known issues
gotchaMongoDBStore constructor throws if no callback is provided and connection fails. Without a callback, uncaught exceptions crash the app.
fix
Always pass a callback to new MongoDBStore(options, callback) to catch connection errors.
affects: >=3.0.0
deprecatedOptions 'url' is deprecated; use 'uri' instead.
fix
Replace 'url' with 'uri' in store options.
affects: >=4.0.0
gotchaIf 'databaseName' is not provided, the store may use a different database than intended.
fix
Always specify 'databaseName' in options to avoid connection to default database.
affects: >=1.0.0
breakingVersion 5.0 requires Node.js >=12. Older Node versions are not supported.
fix
Upgrade Node.js to version 12 or later.
affects: =5.0.0
gotchaThe module does not automatically clean up expired sessions; you must set TTL index manually via MongoDB or use MongoDBStore's default collection options.
fix
Create a TTL index on the 'expires' field of the session collection, or let the store create it by setting 'expires: true' in options.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: MongoDBStore is not a constructor
Calling new MongoDBStore() directly on the default import without invoking the factory function with express-session.
fix
const MongoDBStore = require('connect-mongodb-session')(session);
Error: Cannot find module 'connect-mongodb-session'
Package not installed or missing from node_modules.
fix
npm install connect-mongodb-session
MongoNetworkError: failed to connect to server on first connect
MongoDB connection string or server is unreachable.
fix
Check MongoDB URI and ensure the server is running.
The uri option is required
Missing 'uri' property in options object passed to MongoDBStore.
fix
Add uri: 'mongodb://localhost:27017/dbname' to the options.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies
express-sessionrequiredPeer dependency required to instantiate store
mongodbrequiredMongoDB driver for database connection
Agent activity
28 hits · last 30 days
node
26
Resources
connect-mongodb-session — npm install connect-mongodb-session · libregistry