More than JSON.
Keep ObjectId, dates, binary values and exact numeric types in their native forms.
ObjectId and dates
const id = new BsonLite.ObjectId();
await BsonLite.collection('events').insertOne({
_id: id,
title: 'First launch',
occurredAt: new Date()
});
const event = await BsonLite.collection('events')
.findOne({ _id: new BsonLite.ObjectId(id.toHexString()) });An ObjectId is a BSON value, not a plain string. Convert and validate identifiers at the boundary of your application. Date values should be actual JavaScript Date objects before insertion.
Exact numeric types
const { Decimal128, Long, Int32 } = BsonLite.BSON;
await BsonLite.collection('balances').insertOne({
amount: Decimal128.fromString('1250.50'),
sequence: Long.fromString('9007199254740993'),
count: new Int32(7)
});BsonLite.BSON exposes the driver BSON namespace without a separate npm package. Construct a Decimal128 from a string to preserve decimal intent. Long represents 64-bit integers beyond JavaScript safe integer precision.
Serialize deliberately
const { EJSON } = BsonLite.BSON;
const text = EJSON.stringify(event, { relaxed: false });
const restored = EJSON.parse(text, { relaxed: false });Ordinary JSON may discard BSON type distinctions. Use Extended JSON when a transport needs a textual representation that preserves BSON types. The normal SDK queries and encrypted backups do not require you to serialize documents manually.
Documentation for BsonLite 0.6.0-rc.1. View SDK source · Download the validated SDK