Registry / database / mongodb-queue

mongodb-queue

JSON →
library4.0.0jsnpmunverified

A lightweight, reliable message queue library built on top of MongoDB. The current stable version is 4.0.0, compatible with MongoDB v3 driver. It provides a simple API for adding, getting, acking, and pinging messages, with options for visibility timeout and delay. It is feature-complete and stable, with minimal ongoing development. Key differentiators include zero external dependencies beyond MongoDB, persistent storage, and built-in message visibility management. Ideal for Node.js applications already using MongoDB that need a simple, durable queue without additional infrastructure. Note: For MongoDB v2 driver, use mongodb-queue@3.

npm install mongodb-queue
INSTALL
IMPORT
SIG · MONGODB-QUEUE
M
mongodb-queue
databasejavascriptv4.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.

default
import mongoDbQueue from 'mongodb-queue'
const mongoDbQueue = require('mongodb-queue').default
ESM default import preferred. CommonJS require works as 'const mongoDbQueue = require('mongodb-queue')'.
commonjs require
const mongoDbQueue = require('mongodb-queue')
const { mongoDbQueue } = require('mongodb-queue')
The module exports a single function. Must not use destructuring.
mongoDbQueue with callback
import mongoDbQueue from 'mongodb-queue'; const queue = mongoDbQueue(db, 'my-queue', { visibility: 30 });
const queue = new mongoDbQueue(db, 'my-queue')
mongoDbQueue is a function, not a constructor. Do not use 'new'.

Shows adding, getting, and acknowledging a message using mongodb-queue with async/await and proper error handling.

import { MongoClient } from 'mongodb'; import mongoDbQueue from 'mongodb-queue'; const url = process.env.MONGO_URL ?? 'mongodb://localhost:27017/'; const client = new MongoClient(url); async function run() { try { await client.connect(); const db = client.db('test'); const queue = mongoDbQueue(db, 'my-queue'); // Add a message const id = await new Promise((resolve, reject) => { queue.add('Hello, World!', (err, id) => { if (err) reject(err); else resolve(id); }); }); console.log('Added message with id:', id); // Get a message const msg = await new Promise((resolve, reject) => { queue.get((err, msg) => { if (err) reject(err); else resolve(msg); }); }); console.log('Got message:', msg.payload); // Ack the message await new Promise((resolve, reject) => { queue.ack(msg.ack, (err) => { if (err) reject(err); else resolve(); }); }); console.log('Acked message'); await client.close(); } catch (err) { console.error(err); } } run();
Debug
Known issues
breakingVersion 4.x uses MongoDB v3 driver and requires compatible driver. Do not use with MongoClient from mongodb v2.
fix
Upgrade to mongodb driver v3 or later. If using v2, use mongodb-queue@3.
affects: >=4.0.0
deprecatedCallbacks are deprecated; prefer async/await or Promises.
fix
Wrap callbacks in Promises or use utility like 'util.promisify'.
affects: >=4.0.0
gotchaDo not use the same queue name with different options across instances; undefined behavior.
fix
Create only one queue instance per collection and reuse it.
affects: *
gotchaMessages are not automatically removed after ack by default; they persist for analysis. Use queue.clean() to delete processed messages.
fix
Call queue.clean() periodically or after processing to remove acked messages if desired.
affects: *
gotchaThe 'add' callback returns the message id as the second argument, not the first.
fix
Ensure you access the id via (err, id) not (id).
affects: *
Errors
Common errors & fixes
TypeError: mongoDbQueue is not a constructor
Using 'new' keyword on the imported function.
fix
Remove 'new': const queue = mongoDbQueue(db, 'name');
MongoError: topology was destroyed
MongoClient is closed while queue operations are still in progress.
fix
Ensure all queue operations complete before closing the client.
Cannot read property 'id' of null
queue.get() returned null when queue is empty, but code tries to access msg.id without null check.
fix
Always check if msg is not null before accessing its properties.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
mongodb-queue — npm install mongodb-queue · libregistry