Registry / database / qp-mongodb

qp-mongodb

JSON →
library0.0.0-dev.27jsnpmunverified

The official MongoDB driver for Node.js, maintained by MongoDB Inc. Current stable version is 4.x (as of this metadata) with active development. Supports MongoDB 4.4+, Node.js 12.9.0+, and ships TypeScript definitions. Provides both promise-based and callback APIs, connection pooling, change streams, transactions, and aggregation pipeline support. Compared to alternatives like Mongoose, it is lower-level and does not include schema validation or ORM features, but offers full control over MongoDB operations and better performance for complex queries.

npm install qp-mongodb
INSTALL
IMPORT
SIG · QP-MONGODB
Q
qp-mongodb
databasejavascriptv0.0.0-dev.27
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 is preferred; CommonJS require works but may not be tree-shakeable. The package exports ESM and CJS.
Db
import { Db } from 'mongodb'
const Db = require('mongodb').Db
Type only; not needed at runtime but commonly imported for typing.
ObjectId
import { ObjectId } from 'mongodb'
import { ObjectID } from 'mongodb'
The correct class name is ObjectId (capital I, lower-case d). Using ObjectID (all caps) is a common mistake and will lead to runtime errors.
Collection
import { Collection } from 'mongodb'
import { MongoCollection } from 'mongodb'
The type is Collection, not MongoCollection.

Connects to MongoDB, inserts a document into a collection, and prints the inserted id.

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('users'); const doc = { name: 'Alice', age: 30 }; const result = await collection.insertOne(doc); console.log('Inserted with _id:', result.insertedId); } finally { await client.close(); } } run().catch(console.dir);
Debug
Known issues
breakingIn v4, MongoClient.connect() no longer accepts a callback; must use promise or async/await.
fix
Use await client.connect() or client.connect().then(...).
affects: >=4.0.0
breakingThe MongoClient constructor now requires 'mongodb://' or 'mongodb+srv://' URI; short strings like 'localhost' no longer work.
fix
Always use full URI: 'mongodb://localhost:27017' or 'mongodb+srv://user:password@cluster.mongodb.net'
affects: >=4.0.0
breakingObjectID is removed; use ObjectId instead.
fix
Replace ObjectID with ObjectId in imports and usage.
affects: >=4.0.0
deprecatedCollection.find() no longer accepts a callback; returns a FindCursor.
fix
Use cursor.toArray() or cursor.forEach() with async/await.
affects: >=3.6.0
gotchaCalling client.close() after connection failure may throw if client was never successfully connected.
fix
Check if client is connected before closing, e.g., if (client.topology?.isConnected()) await client.close();
affects: >=4.0.0
Errors
Common errors & fixes
MongoParseError: Invalid connection string
Connection URI missing scheme (mongodb://) or contains typos.
fix
Ensure URI starts with 'mongodb://' or 'mongodb+srv://' and is properly encoded.
MongoError: Authentication failed.
Incorrect credentials or auth mechanism mismatch.
fix
Check username/password and authSource in URI. For SCRAM-SHA-256, ensure driver version >=4.0.
TypeError: db.collection is not a function
Using `db.collection` on a non-MongoDB object or after connection failure.
fix
Ensure client.connect() succeeded and 'db' is a Db instance: const db = client.db('test');
Upgrade
Version history
0.0.0-dev.27latest on npm
Audit
Dependencies
@types/nodeoptionalRequired for TypeScript users to access Node.js type definitions for driver types
Agent activity
9 hits · last 30 days
node
6
Meta
1
Resources
qp-mongodb — npm install qp-mongodb · libregistry