Registry / database / mongodb

mongodb

JSON →
library7.2.0jsnpmunverified

Official MongoDB driver for Node.js, maintained by MongoDB Inc. Current stable version is 7.2.0 (released 2025-08-13). Requires Node.js >=20.19.0 and ESM-only since v7. Regularly released every few months, with LTS versions supported for 3 years. Key differentiators: directly integrates with MongoDB protocol, supports connection pooling, transactions, change streams, and modern authentication (AWS IAM, OIDC). Users migrating from v6 must update imports and Node.js version.

npm install mongodb
INSTALL
IMPORT
SIG · MONGODB
M
mongodb
databasejavascriptv7.2.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.

MongoClient
import { MongoClient } from 'mongodb'
const MongoClient = require('mongodb')
ESM-only since v7. CommonJS require throws.
ObjectId
import { ObjectId } from 'mongodb'
import ObjectId from 'mongodb'
ObjectId is a named export, not default.
MongoClient (type import)
import type { MongoClient } from 'mongodb'
import { MongoClient } from 'mongodb' (if using runtime)
Use import type for type-only imports to avoid bundling.
Db, Collection
import { Db, Collection } from 'mongodb'
import { Db } from 'mongodb' and assume it's a class
Db and Collection are interfaces, not classes.

Connects to MongoDB, inserts a document and finds it. Uses async/await and proper cleanup.

import { MongoClient } from 'mongodb'; const uri = process.env.MONGODB_URI ?? 'mongodb://localhost:27017'; const client = new MongoClient(uri); async function run() { try { await client.connect(); const database = client.db('test'); const collection = database.collection('movies'); const doc = { title: 'The Matrix', year: 1999 }; const result = await collection.insertOne(doc); console.log(`New document inserted with _id: ${result.insertedId}`); const query = { title: 'The Matrix' }; const movie = await collection.findOne(query); console.log(movie); } finally { await client.close(); } } run().catch(console.dir);
Debug
Known issues
breakingIn v7, the driver is ESM-only. CommonJS require() will throw.
fix
Migrate to ESM (type:'module' in package.json) or use dynamic import.
affects: >=7.0.0
breakingNode.js >=20.19.0 is required. Older versions are not supported.
fix
Update your Node.js to 20.19.0 or later.
affects: >=7.0.0
deprecatedTopology and topologyDescription events are deprecated in favor of serverHeartbeat events.
fix
Use 'serverHeartbeatSucceeded', 'serverHeartbeatFailed', 'serverHeartbeatStarted' events.
affects: >=6.0.0
breakingClient-side encryption requires mongodb-client-encryption >=7.0.0 <7.1.0 in v7.
fix
Install a compatible version: npm install mongodb-client-encryption@~7.0.0
affects: >=7.0.0
gotchaMongoClient must be connected before use. Calling methods on a disconnected client will throw.
fix
Always await client.connect() before database operations.
affects: *
deprecatedThe `useNewUrlParser` and `useUnifiedTopology` options are no longer needed and are ignored.
fix
Remove these options from connection URI.
affects: >=5.0.0
breakingIn v7, `ObjectId` is no longer a constructor that can be called without `new`.
fix
Use `new ObjectId()` instead of `ObjectId()` (though it may work due to legacy, best to use `new`).
affects: >=7.0.0
Errors
Common errors & fixes
MongoNotConnectedError: MongoClient must be connected to perform this operation
Calling methods like find() or insertOne() on a client that hasn't been connected.
fix
Ensure you await client.connect() before any database operations.
TypeError: Class constructor MongoClient cannot be invoked without 'new'
Using require() on an ESM-only module or calling MongoClient() without new.
fix
Use ESM import and always use new MongoClient(uri).
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
MongoDB server is not running on the expected host and port.
fix
Start mongod or update the connection URI to point to a running instance.
MongooseError: The `useNewUrlParser` and `useUnifiedTopology` options are not needed
Mongoose still emits these warnings; driver ignores them.
fix
This is a Mongoose warning, not driver. Update Mongoose or ignore.
Upgrade
Version history
7.2.0latest on npm
Audit
Dependencies
socksoptionalSOCKS5 proxy support
snappyoptionalSnappy compression
kerberosoptionalKerberos authentication
gcp-metadataoptionalGCP IAM authentication
@mongodb-js/zstdoptionalZstd compression
mongodb-client-encryptionoptionalClient-side field level encryption
@aws-sdk/credential-providersoptionalAWS IAM authentication
Agent activity
5 hits · last 30 days
node
4
Resources
mongodb — npm install mongodb · libregistry