Registry / database / mongodb-legacy

mongodb-legacy

JSON →
library6.1.3jsnpmunverified

A wrapper around the official MongoDB Node.js driver that adds back optional callback support, intended to help projects migrate incrementally from callbacks to promises/async-await. The main mongodb driver dropped optional callbacks in v5. This package (v6.1.3) wraps mongodb v6, is actively maintained, ships TypeScript types, and requires Node >=16.20.1. It uses a caret semver range on mongodb to automatically pull in minor/patch updates. Performance penalty is minimal but present. Not recommended for new projects; use the native driver directly.

npm install mongodb-legacy
INSTALL
IMPORT
SIG · MONGODB-LEGACY
M
mongodb-legacy
databasejavascriptv6.1.3
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.

MongoClient
import { MongoClient } from 'mongodb-legacy'
const { MongoClient } = require('mongodb-legacy')
Package ships TypeScript types; ESM import recommended.
Db
import { Db } from 'mongodb-legacy'
import Db from 'mongodb-legacy'
Named export, not default.
Collection
import { Collection } from 'mongodb-legacy'
import { Collection } from 'mongodb'
Use mongodb-legacy to get callback methods; mongodb package no longer supports callbacks.

Demonstrates connecting to MongoDB using mongodb-legacy, performing a findOne operation with both callback and promise styles.

import { MongoClient } from 'mongodb-legacy'; const uri = process.env.MONGO_URI ?? 'mongodb://localhost:27017'; const client = new MongoClient(uri); async function run() { try { await client.connect(); const db = client.db('test'); const collection = db.collection('users'); // Callback style collection.findOne({ name: 'Alice' }, (err, doc) => { if (err) console.error(err); else console.log(doc); }); // Promise style const doc = await collection.findOne({ name: 'Bob' }); console.log(doc); } finally { await client.close(); } } run().catch(console.dir);
Debug
Known issues
gotchaNever use the same callback method with both a callback and await; behavior is undefined.
fix
Use either callback or promise, not both.
affects: >=4.0.0
deprecatedNew MongoDB driver APIs will not have optional callback support in this package.
fix
Use .then()/.catch() or util.callbackify for new APIs.
affects: >=6.0.0
gotchaReplacing mongodb with mongodb-legacy may bypass lockfile control; mongodb version is determined by the caret range.
fix
Manually edit lockfile if specific mongodb version is needed.
affects: all
Errors
Common errors & fixes
import { MongoClient } from 'mongodb' but get 'MongoClient is not a constructor'
Importing from 'mongodb' instead of 'mongodb-legacy' after switching packages.
fix
Change import to 'mongodb-legacy'.
TypeError: collection.findOne is not a function
Using mongodb-legacy but not awaiting or providing a callback; method returns a promise or accepts callback.
fix
Either await the call or pass a callback function.
Cannot find module 'mongodb-legacy'
Package not installed.
fix
Run 'npm install mongodb-legacy'.
Upgrade
Version history
6.1.3latest on npm
Audit
Dependencies
mongodbrequiredwrapper around the official MongoDB driver
Agent activity
6 hits · last 30 days
node
6
Resources
mongodb-legacy — npm install mongodb-legacy · libregistry