API Reference

MongoDB Node Driver

Official Node.js driver for MongoDB with pooling, cursors, transactions and typed collections.

Official Documentation ↗
CompatibilityThis documentation covers version mongodb v6.x / MongoDB 5.0+ / Node 18 LTS. Check the official docs for the latest API surface.

Installation

npmnpm install mongodb
pnpmpnpm add mongodb
yarnyarn add mongodb

Methods

Overview

The official Node driver talks to MongoDB with a pooled client, document-oriented collections and cursor-based reads. It supports typed collections through generics and full transaction control via sessions.

Core Methods

Connect once with a MongoClient, select a database and collection, then call insertOne, findOne, updateOne, deleteOne and find. Cursors from find can be iterated lazily or materialized with toArray.

Usage Notes

Create and reuse a single client, index the fields you filter and sort on, and cap result sets with limit and projection. Use sessions for multi-document transactions on replica sets.

Common Pitfalls

Unbounded find().toArray() can exhaust memory on large collections, so prefer cursor iteration or limits. Missing indexes turn point queries into collection scans. Client-per-request destroys pooling benefits.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://www.mongodb.com/docs/drivers/node/current/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:59.503Z.

FAQ

Should one client be shared application-wide?
Yes. MongoClient manages an internal connection pool, so create one client per process and reuse it. Creating a client per request exhausts connections.
How do I run a transaction?
Start a session with client.startSession, then session.withTransaction. Transactions require a replica set; standalone servers do not support them.
Why are my writes not visible to other readers?
Default read concern and write concern balance durability and latency. Use majority write concern with the appropriate read concern when you need reads to see committed data across replica-set nodes.