Understanding BSON binary types - cover art

MongoDB and UUID 15 min read

Understanding BSON binary types

November 20, 2026 · 15 min read

MongoDB documents are BSON, not JSON text on disk. The Binary type stores opaque bytes with a one-byte subtype that tells drivers how to interpret them - generic blob, UUID, MD5 hash, or user-defined. UUID confusion in production is almost always a subtype/endianness mismatch, not “random hex.”

BSON Binary type structure

Binary values include length, subtype byte, and data. Drivers map these to language types (Binary, byte[], Buffer). Extended JSON represents them as { "$binary": { "base64": "...", "subType": "04" } } in canonical exports.

Subtype 0x00 (generic)

Subtype 0 is a generic byte array - images, ciphertext, protobuf payloads. No implied endianness rules. Do not store RFC UUID strings here without a consistent 16-byte convention.

Subtype 0x04 (UUID standard)

Subtype 0x04 is the standard UUID encoding (16 bytes). Java and other drivers must align UuidRepresentation with how data was written. This is the recommended storage for new collections.

{
  "_id": { "$binary": { "base64": "VQeEAOKbQdSnFkRmVUQAAA==", "subType": "04" } }
}

Legacy subtype 0x03

Older drivers used subtype 0x03 with a different byte order (C# legacy). Mixed subtypes in one deployment cause “UUID looks swapped” bugs. Migration scripts normalize to 0x04.

Conversion workflow

When shell hex differs from application strings, use the MongoDB UUID converter with known subtype. Cross-check string form in the UUID validator. Document subtype in schema design reviews.

FAQ

What is BSON subtype 5?
MD5 hash binary - different from UUID; do not confuse in migrations.
Should new projects use subtype 4?
Yes - standard UUID with Java 4.x driver UuidRepresentation.STANDARD.
Can I store UUID as string instead?
Yes - 36-char strings work; binary saves space and enforces type.

Related: MongoDB binary UUID · UUID subtype 03 vs 04 · MongoDB Extended JSON · How MongoDB stores UUIDs

Browse all tools