Registry / database / qh-mongodb

qh-mongodb

JSON →
library4.10.100jsnpmunverified

The official MongoDB driver for Node.js, maintained by MongoDB Inc. This package (version 4.10.100) provides async/await support, transaction support, and a modern API for connecting to MongoDB databases. It ships TypeScript types and requires Node.js >= 12.9.0. Key differentiators include full support for MongoDB features like change streams, aggregation, and gridfs, as well as automatic connection pooling and retryable reads/writes. The driver follows MongoDB's release cadence and is actively developed.

npm install qh-mongodb
INSTALL
IMPORT
SIG · QH-MONGODB
Q
qh-mongodb
databasejavascriptv4.10.100
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').MongoClient
ESM import works with Node >= 12. For CommonJS, use `const { MongoClient } = require('mongodb')`.
ObjectId
✓ import { ObjectId } from 'mongodb'
✗ const ObjectId = require('mongodb').ObjectID
Note: ObjectID (capital D) deprecated in v4; use ObjectId (lowercase d).
Db
✓ import { Db } from 'mongodb'
TypeScript: Db type is exported for type annotations.

Connects to MongoDB, inserts a document, and retrieves it using async/await.

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 db = client.db('mydb'); const collection = db.collection('mycollection'); const insertResult = await collection.insertOne({ hello: 'world' }); console.log('Inserted document:', insertResult.insertedId); const findResult = await collection.findOne({ hello: 'world' }); console.log('Found document:', findResult); } finally { await client.close(); } } run().catch(console.dir);
Debug
Known issues
breakingIn v4, MongoClient.connect() no longer returns a promise that resolves when connected; call connect() explicitly before operations.
fix
Add await client.connect() before any db operations.
affects: >=4.0.0
deprecatedThe `ObjectID` (capital D) constructor is deprecated in favor of `ObjectId`.
fix
Use ObjectId from 'mongodb' instead of ObjectID.
affects: >=4.0.0
gotchaMongoClient does not automatically close connections; forgetting to close can cause resource leaks.
fix
Always call client.close() in a finally block or use the with statement pattern.
affects: >=3.0.0
gotchaUsing `mongodb` package without `mongodb-client-encryption` installed will cause an error when trying to use FLE.
fix
Install optional dependency: npm install mongodb-client-encryption.
affects: >=4.0.0
breakingIn v4, the callback-based API is no longer supported; all methods return promises.
fix
Use async/await or .then() instead of callbacks.
affects: >=4.0.0
deprecatedThe `collection.insert()`, `collection.update()`, and `collection.remove()` methods are deprecated in v4.
fix
Use insertOne/insertMany, updateOne/updateMany, deleteOne/deleteMany instead.
affects: >=4.0.0
Errors
Common errors & fixes
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
MongoDB server is not running or is not accessible at the given host/port.
fix
Start MongoDB service: `mongod` or `net start MongoDB` on Windows.
MongoParseError: option useNewUrlParser is not supported
The `useNewUrlParser` option has been removed in v4.
fix
Remove `useNewUrlParser: true` from the MongoClient options.
TypeError: client.db is not a function
MongoClient.connect() not called before using client.db().
fix
Add `await client.connect();` before using `client.db()`.
MongoError: Authentication failed
Incorrect credentials or missing authentication mechanism.
fix
Check username/password in the connection string and ensure the database user exists.
Upgrade
Version history
4.10.100latest on npm
Audit
Dependencies
mongodb-client-encryptionoptionalOptional dependency for client-side field level encryption (FLE)
saslprepoptionalOptional dependency for SASL authentication (SCRAM-SHA-256/PLAIN)
Agent activity
10 hits · last 30 days
node
6
Meta
1
Resources
qh-mongodb — npm install qh-mongodb · libregistry