Registry / database / rate-limit-mongo

rate-limit-mongo

JSON →
library2.3.2jsnpmunverified

rate-limit-mongo is a specialized MongoDB store designed for the popular `express-rate-limit` middleware, currently at version 2.3.2. This package provides a persistent, database-backed storage mechanism for rate limiting records, moving beyond in-memory or Redis solutions. It leverages MongoDB's TTL (Time-To-Live) indexes to automatically expire rate limiting entries, ensuring efficient cleanup and preventing stale data. While not on a strict release cadence, updates typically align with bug fixes or `express-rate-limit`/MongoDB driver compatibility improvements. Its primary differentiation lies in offering a robust, low-configuration MongoDB-specific solution for managing API rate limits, particularly beneficial for applications already using MongoDB and requiring shared, persistent rate limit counters across multiple instances.

npm install rate-limit-mongo
INSTALL
IMPORT
SIG · RATE-LIMIT-MONGO
R
rate-limit-mongo
databasejavascriptv2.3.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

MongoStore
import MongoStore from 'rate-limit-mongo';
import { MongoStore } from 'rate-limit-mongo';
The package exports the `MongoStore` class as a default export for ESM environments.
MongoStore
const MongoStore = require('rate-limit-mongo');
const { MongoStore } = require('rate-limit-mongo');
The package exports the `MongoStore` class directly as its `module.exports` for CommonJS environments.

This code demonstrates the basic setup of `rate-limit-mongo` as a store for `express-rate-limit`, configuring connection details and matching `expireTimeMs` with `windowMs`.

const RateLimit = require('express-rate-limit'); const MongoStore = require('rate-limit-mongo'); const limiter = new RateLimit({ store: new MongoStore({ uri: process.env.MONGO_URI ?? 'mongodb://127.0.0.1:27017/test_db', user: process.env.MONGO_USER ?? '', password: process.env.MONGO_PASSWORD ?? '', expireTimeMs: 15 * 60 * 1000, // Should match windowMs errorHandler: console.error.bind(null, 'rate-limit-mongo store error') }), max: 100, windowMs: 15 * 60 * 1000 // Should match expireTimeMs }); // Example of how to apply it in an Express app (assuming 'app' is an Express instance) // app.use(limiter);
Debug
Known issues
gotchaThe `expireTimeMs` option in `rate-limit-mongo` and the `windowMs` option in `express-rate-limit` should be set to identical values. Mismatching these values will result in incorrect `Retry-After` headers being sent to clients.
fix
Ensure `expireTimeMs` in `MongoStore` options and `windowMs` in `RateLimit` options are numerically equivalent (e.g., both 15 * 60 * 1000 for 15 minutes).
affects: >=1.0.0
gotchaMongoDB TTL indexes operate on a background task that runs every 60 seconds. Consequently, expired documents may persist in the collection for a period between their expiration and the task's execution.
fix
Account for this delay in application logic if strict immediate deletion is required, or consider shorter `expireTimeMs` for more aggressive cleanup (though this increases database write load).
affects: >=1.0.0
gotchaBy default, `rate-limit-mongo` attempts to create a TTL index on the collection. If the MongoDB user lacks permissions for index creation, this operation will fail. The `createTtlIndex: false` option can suppress this behavior.
fix
If index creation fails or is not desired from the application, set `createTtlIndex: false` in the `MongoStore` options and ensure the TTL index is manually created on the collection (`db.collection.createIndex({ expirationDate: 1 }, { expireAfterSeconds: 0 })`).
affects: >=1.0.0
gotchaThe default MongoDB connection options `useUnifiedTopology: true` and `useNewUrlParser: true` are implicitly applied. These options may become deprecated or change behavior in future versions of the MongoDB Node.js driver.
fix
Monitor `mongodb` driver release notes for changes to connection options. Explicitly define all desired connection options via the `connectionOptions` property to maintain control over connection behavior.
affects: >=1.0.0
Errors
Common errors & fixes
MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect
The MongoDB server is not running or the connection URI is incorrect.
fix
Ensure the MongoDB server is running and accessible from the application host. Verify the `uri` provided in the `MongoStore` configuration is correct and points to an active MongoDB instance.
MongoError: Authentication failed.
Incorrect username or password provided for MongoDB authentication.
fix
Verify that the `user` and `password` fields in the `MongoStore` configuration match valid MongoDB credentials with access to the specified database or `authSource`.
Error: TTL index 'expirationDate_1' already exists with different options.
An existing index with the same name on the `expirationDate` field has different `expireAfterSeconds` settings than what `rate-limit-mongo` expects (which is 0).
fix
Drop the existing index (`db.collection.dropIndex('expirationDate_1')`) or ensure the `createTtlIndex` option is set to `false` if you manage the index manually with the correct `expireAfterSeconds: 0` setting.
Upgrade
Version history
2.3.2latest on npm
Audit
Dependencies
mongodbrequiredRequired to connect to and store rate limit records in a MongoDB database.
Agent activity
6 hits · last 30 days
node
6
Resources
rate-limit-mongo — npm install rate-limit-mongo · libregistry