API Reference

BSON

The BSON serialization library for MongoDB, encoding documents to a compact binary format.

Official Documentation ↗
CompatibilityThis documentation covers version v7.3.3. Check the official docs for the latest API surface.

Installation

npmnpm install bson

Methods

js-bson encodes and decodes BSON documents and object ids, the wire format used by MongoDB.

Key Features

  • Binary encoding of the rich BSON type set
  • ObjectId creation and validation
  • Order-preserving document fields

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/mongodb/js-bson@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:35.163Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

What types does BSON support beyond JSON?
BSON adds Date, binary, ObjectId, long, decimal, and regex, which is why it is used by MongoDB internally.
Is BSON order-preserving?
Yes. Documents are an ordered sequence of fields, so field order round-trips exactly, unlike JSON parsed into a plain object.
Can ObjectId bytes come from a client?
Yes. new ObjectId(string) validates length and hex characters, so malformed identifiers throw instead of corrupting queries.