Random BSON Generator
Generate random BSON documents with valid ObjectIds, typed fields, nested structures, and all standard BSON types. Copy or download instantly.
Click "Generate" to create random BSON documents.
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.
Formulas
A BSON ObjectId is a 12-byte value constructed from four components concatenated in order:
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:
Extracting the embedded timestamp from an ObjectId:
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:
Reference Data
| BSON Type | Type ID | Size | JS Representation | Range / Notes |
|---|---|---|---|---|
| Double | 0x01 | 8 bytes | Number (float) | IEEE 754 64-bit. ยฑ1.7976931348623157ร10308 |
| String | 0x02 | Variable | String (UTF-8) | Length-prefixed. Max 16 MB per document |
| Document | 0x03 | Variable | Object | Nested embedded document |
| Array | 0x04 | Variable | Array | Indexed keys as strings ("0", "1", ...) |
| Binary | 0x05 | Variable | Base64 string | Subtype 0x00 (generic) |
| ObjectId | 0x07 | 12 bytes | Hex string (24 chars) | Timestamp + Random + Counter |
| Boolean | 0x08 | 1 byte | true / false | 0x00 or 0x01 |
| UTC Datetime | 0x09 | 8 bytes | ISO 8601 string | Milliseconds since Unix epoch |
| Null | 0x0A | 0 bytes | null | No value stored |
| Regex | 0x0B | Variable | {$regex, $options} | Pattern + flags (i, m, s, x) |
| Int32 | 0x10 | 4 bytes | Number (integer) | โ231 to 231โ1 |
| Timestamp | 0x11 | 8 bytes | {t, i} | Internal MongoDB oplog type |
| Int64 | 0x12 | 8 bytes | {$numberLong: "..."} | โ263 to 263โ1 |
| Decimal128 | 0x13 | 16 bytes | {$numberDecimal: "..."} | 34 significant digits. Financial precision |
| MinKey | 0xFF | 0 bytes | {$minKey: 1} | Compares lower than all other BSON types |
| MaxKey | 0x7F | 0 bytes | {$maxKey: 1} | Compares higher than all other BSON types |