User Rating 0.0 โ˜…โ˜…โ˜…โ˜…โ˜…
Total Usage 0 times
5
6
2
Output
Click "Generate" to create random BSON documents.
Is this tool helpful?

Your feedback helps us improve.

โ˜… โ˜… โ˜… โ˜… โ˜…

About

BSON (Binary JSON) is MongoDB's wire format. Every document stored in a MongoDB collection is serialized as BSON before being written to disk. A valid BSON ObjectId is not a random hex string. It encodes a 4-byte Unix timestamp, a 5-byte random value unique to the machine and process, and a 3-byte incrementing counter. Generating test data with structurally incorrect ObjectIds causes silent failures in aggregation pipelines, shard key routing, and TTL index expiration because MongoDB extracts the embedded timestamp via ObjectId.getTimestamp(). This tool produces documents whose ObjectIds follow the real 12-byte specification defined in the BSON spec (bsonspec.org, revision 1.1). It does not paste random hex characters into a 24-character string.

Beyond ObjectIds, BSON defines 18 distinct element types. Naively generating test data with only strings and integers misses type-specific edge cases: IEEE 754 Double precision rounding, signed 32-bit vs. 64-bit integer overflow boundaries, UTC datetime millisecond precision, Decimal128 for financial values, and regex pattern validation. This generator produces structurally valid documents with configurable depth, field count, and type distribution. It approximates a real-world collection's schema variance, though it cannot replicate application-specific business logic constraints.

bson generator objectid generator mongodb test data random bson bson document nosql generator mongodb mock data

Formulas

A BSON ObjectId is a 12-byte value constructed from four components concatenated in order:

ObjectId = T4 + R5 + C3

Where T4 = 4-byte big-endian Unix timestamp in seconds (gives ObjectIds a natural chronological sort order), R5 = 5 random bytes generated once per process (encodes machine identity and process ID), and C3 = 3-byte big-endian counter initialized to a random value, incremented per ObjectId generated within the same second.

The hex-encoded output has exactly 24 characters:

len(hex) = 12 bytes ร— 2 hex chars/byte = 24

Extracting the embedded timestamp from an ObjectId:

timestamp = parseInt(objectId.substring(0, 8), 16)

For random value generation, the tool uses the Web Crypto API's crypto.getRandomValues() which provides cryptographically strong pseudo-random bytes sourced from the operating system's entropy pool. Integer ranges are computed via modular arithmetic to avoid bias:

value = min + (randomUint32 mod (max โˆ’ min + 1))

Reference Data

BSON TypeType IDSizeJS RepresentationRange / Notes
Double0x018 bytesNumber (float)IEEE 754 64-bit. ยฑ1.7976931348623157ร—10308
String0x02VariableString (UTF-8)Length-prefixed. Max 16 MB per document
Document0x03VariableObjectNested embedded document
Array0x04VariableArrayIndexed keys as strings ("0", "1", ...)
Binary0x05VariableBase64 stringSubtype 0x00 (generic)
ObjectId0x0712 bytesHex string (24 chars)Timestamp + Random + Counter
Boolean0x081 bytetrue / false0x00 or 0x01
UTC Datetime0x098 bytesISO 8601 stringMilliseconds since Unix epoch
Null0x0A0 bytesnullNo value stored
Regex0x0BVariable{$regex, $options}Pattern + flags (i, m, s, x)
Int320x104 bytesNumber (integer)โˆ’231 to 231โˆ’1
Timestamp0x118 bytes{t, i}Internal MongoDB oplog type
Int640x128 bytes{$numberLong: "..."}โˆ’263 to 263โˆ’1
Decimal1280x1316 bytes{$numberDecimal: "..."}34 significant digits. Financial precision
MinKey0xFF0 bytes{$minKey: 1}Compares lower than all other BSON types
MaxKey0x7F0 bytes{$maxKey: 1}Compares higher than all other BSON types

Frequently Asked Questions

Yes. Each ObjectId follows the BSON spec 1.1 structure: 4-byte Unix timestamp (current time at generation), 5 random bytes from crypto.getRandomValues(), and a 3-byte incrementing counter. MongoDB will accept these as valid _id values. The embedded timestamp is extractable via ObjectId.getTimestamp() and will return the correct generation time.
JavaScript's Number.MAX_SAFE_INTEGER is 2^53 โˆ’ 1, but BSON Int64 supports up to 2^63 โˆ’ 1. The generator represents Int64 values using MongoDB's Extended JSON v2 format: {"$numberLong": "value"} where the value is a string. This avoids floating-point precision loss. When importing into MongoDB via mongoimport, use the --jsonArray flag with Extended JSON mode to preserve full 64-bit precision.
MongoDB enforces a maximum nesting depth of 100 levels. This generator caps recursive document/array nesting at a user-configurable maximum (default: 3 levels). In practice, nesting beyond 5-6 levels causes significant performance degradation in queries using dot notation paths and $elemMatch. The 16 MB document size limit is also a practical constraint that limits deep nesting with large arrays.
The generated Decimal128 values use MongoDB's Extended JSON format {"$numberDecimal": "value"} with up to 34 significant digits. The values are generated as random decimal strings within reasonable financial ranges. True IEEE 754-2008 Decimal128 has a coefficient of up to 34 digits and an exponent range of โˆ’6176 to 6111. The generator produces values within common-use ranges rather than extreme exponent boundaries.
Yes. The output uses MongoDB Extended JSON v2 (canonical mode) for special types like ObjectId, Int64, Decimal128, Date, and Timestamp. Run: mongoimport --db testdb --collection testcol --jsonArray --file output.json. For the strict JSON mode used by older drivers, ObjectIds are wrapped as {"$oid": "..."} and dates as {"$date": "ISO-8601"}.
Binary data is output in Extended JSON format: {"$binary": {"base64": "...", "subType": "00"}}. The generator creates random byte sequences of configurable length (default 8-32 bytes), encodes them as base64, and uses subtype 0x00 (generic binary). MongoDB supports subtypes 0x00 through 0x80, but generic binary is the most common for test data.