{"id":43722,"library":"qp-mongodb","title":"MongoDB Node.js Driver","description":"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.","status":"active","version":"0.0.0-dev.27","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/chequer-io/node-mongodb-native","tags":["javascript","mongodb","driver","official","typescript"],"install":[{"cmd":"npm install qp-mongodb","lang":"bash","label":"npm"},{"cmd":"yarn add qp-mongodb","lang":"bash","label":"yarn"},{"cmd":"pnpm add qp-mongodb","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for TypeScript users to access Node.js type definitions for driver types","package":"@types/node","optional":true}],"imports":[{"note":"ESM import is preferred; CommonJS require works but may not be tree-shakeable. The package exports ESM and CJS.","wrong":"const MongoClient = require('mongodb').MongoClient","symbol":"MongoClient","correct":"import { MongoClient } from 'mongodb'"},{"note":"Type only; not needed at runtime but commonly imported for typing.","wrong":"const Db = require('mongodb').Db","symbol":"Db","correct":"import { Db } from 'mongodb'"},{"note":"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.","wrong":"import { ObjectID } from 'mongodb'","symbol":"ObjectId","correct":"import { ObjectId } from 'mongodb'"},{"note":"The type is Collection, not MongoCollection.","wrong":"import { MongoCollection } from 'mongodb'","symbol":"Collection","correct":"import { Collection } from 'mongodb'"}],"quickstart":{"code":"import { MongoClient } from 'mongodb';\n\nconst uri = process.env.MONGODB_URI ?? 'mongodb://localhost:27017';\nconst client = new MongoClient(uri);\n\nasync function run() {\n  try {\n    await client.connect();\n    const database = client.db('test');\n    const collection = database.collection('users');\n    const doc = { name: 'Alice', age: 30 };\n    const result = await collection.insertOne(doc);\n    console.log('Inserted with _id:', result.insertedId);\n  } finally {\n    await client.close();\n  }\n}\n\nrun().catch(console.dir);","lang":"typescript","description":"Connects to MongoDB, inserts a document into a collection, and prints the inserted id."},"warnings":[{"fix":"Use await client.connect() or client.connect().then(...).","message":"In v4, MongoClient.connect() no longer accepts a callback; must use promise or async/await.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always use full URI: 'mongodb://localhost:27017' or 'mongodb+srv://user:password@cluster.mongodb.net'","message":"The MongoClient constructor now requires 'mongodb://' or 'mongodb+srv://' URI; short strings like 'localhost' no longer work.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Replace ObjectID with ObjectId in imports and usage.","message":"ObjectID is removed; use ObjectId instead.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use cursor.toArray() or cursor.forEach() with async/await.","message":"Collection.find() no longer accepts a callback; returns a FindCursor.","severity":"deprecated","affected_versions":">=3.6.0"},{"fix":"Check if client is connected before closing, e.g., if (client.topology?.isConnected()) await client.close();","message":"Calling client.close() after connection failure may throw if client was never successfully connected.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"search_vec":"'12.9.0':31 '4':18 '4.4':29 'activ':25 'aggreg':50 'altern':55 'api':43 'base':40 'better':80 'callback':42 'chang':46 'compar':53 'complex':83 'connect':44 'control':75 'current':14 'definit':35 'develop':26 'driver':3,7,87 'featur':71 'full':74 'inc':13 'includ':66 'javascript':85 'level':62 'like':56 'lower':61 'lower-level':60 'maintain':10 'metadata':23 'mongodb':1,6,12,28,77,86 'mongoos':57 'node.js':2,9,30 'offer':73 'offici':5,88 'oper':78 'orm':70 'perform':81 'pipelin':51 'pool':45 'promis':39 'promise-bas':38 'provid':36 'queri':84 'schema':67 'ship':33 'stabl':15 'stream':47 'support':27,52 'transact':48 'typescript':34,89 'valid':68 'version':16 'x':19","created_at":"2026-06-05T17:01:10.438483+00:00","updated_at":"2026-06-05T17:01:10.438483+00:00","problems":[{"fix":"Ensure URI starts with 'mongodb://' or 'mongodb+srv://' and is properly encoded.","cause":"Connection URI missing scheme (mongodb://) or contains typos.","error":"MongoParseError: Invalid connection string"},{"fix":"Check username/password and authSource in URI. For SCRAM-SHA-256, ensure driver version >=4.0.","cause":"Incorrect credentials or auth mechanism mismatch.","error":"MongoError: Authentication failed."},{"fix":"Ensure client.connect() succeeded and 'db' is a Db instance: const db = client.db('test');","cause":"Using `db.collection` on a non-MongoDB object or after connection failure.","error":"TypeError: db.collection is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/chequer-io/node-mongodb-native","github":"ssh://git@github.com/chequer-io/node-mongodb-native","docs":null,"changelog":null,"pypi":null,"npm":"qp-mongodb","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}