npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-mongodb

v1.0.0

Published

MongoDB session store for Express

Readme

npm version CI License: MIT

express-mongodb

MongoDB-backed session stores for express-session.

This package is a modern rewrite of the original Express 2/Mongoose store. It now targets Node.js >=22, Express 5, express-session, and current MongoDB tooling.

Installation

npm install express-mongodb express-session mongodb

For the Mongoose adapter, install Mongoose as well:

npm install mongoose

Native MongoDB Usage

import express from "express";
import session from "express-session";
import { MongoClient } from "mongodb";
import { MongoSessionStore } from "express-mongodb";

const app = express();
const client = new MongoClient("mongodb://localhost:27017/test");
await client.connect();

app.use(session({
    secret: "replace-this-secret",
    resave: false,
    saveUninitialized: false,
    store: new MongoSessionStore({
        client,
        dbName: "test",
        collectionName: "session"
    })
}));

app.get("/", (req, res) => {
    req.session.views = (req.session.views || 0) + 1;
    res.json({ views: req.session.views });
});

app.listen(3000);

You can also pass a Db instance:

new MongoSessionStore({
    db: client.db("test"),
    collectionName: "session"
});

Mongoose Usage

import express from "express";
import session from "express-session";
import mongoose from "mongoose";
import { MongooseSessionStore } from "express-mongodb";

const app = express();
const connection = await mongoose.createConnection("mongodb://localhost:27017/test").asPromise();

app.use(session({
    secret: "replace-this-secret",
    resave: false,
    saveUninitialized: false,
    store: new MongooseSessionStore({
        connection,
        collectionName: "session"
    })
}));

The Mongoose adapter uses connection.collection(collectionName) directly. It does not create a Mongoose model and does not close the supplied connection.

CommonJS Compatibility

Named exports are available through require:

const {
    MongoSessionStore,
    MongooseSessionStore,
    createMongoSessionStore,
    createMongooseSessionStore
} = require("express-mongodb");

The old callable factory is kept only as a deprecated migration aid:

const legacyFactory = require("express-mongodb");
const MongooseStore = legacyFactory(express);

The express argument is ignored. New code should use MongoSessionStore or MongooseSessionStore directly with express-session.

Options

| Option | Default | Description | | --- | --- | --- | | collectionName | session | MongoDB collection used for session documents. | | collection | undefined | Deprecated alias for collectionName. | | ttlSeconds | 1209600 | Fallback expiration when session.cookie.expires is absent. | | autoCreateTtlIndex | true | Creates { expiresAt: 1 } with { expireAfterSeconds: 0 }. | | clearIntervalSeconds | 0 | Deprecated compatibility cleanup interval for expired documents. | | clear_interval | undefined | Deprecated alias for clearIntervalSeconds. |

The default collection name is session.

Store methods use Node-style callbacks. Operation errors are reported through those callbacks, so provide callbacks when you need to observe failures.

Expiration Behavior

Session documents are stored as:

{
    _id: sid,
    session,
    expiresAt,
    updatedAt
}

expiresAt is calculated from session.cookie.expires when it is a valid date. Otherwise, the store uses ttlSeconds. get, all, and length ignore expired sessions; get also deletes an expired document opportunistically.

MongoDB TTL cleanup is the primary expiration mechanism. clearIntervalSeconds and clear_interval exist for compatibility and should not be needed in new applications.

Manual Smoke Test

Start MongoDB locally at mongodb://localhost:27017/test, build the package, and run the smoke app:

npm run build
node demo/store.js

Open http://localhost:3000. The app writes sessions to the mysession collection and returns the current session counter plus stored session count.

License

MIT