skip to content

A BSON field holds subtype 3 UUIDs from services on different MongoDB drivers, and the decoded values look scrambled: what do the raw bytes show, and what can they not prove?

level: seniorimportance: should knowfreq 25%

answer

  1. same UUID, different stored bytes
  2. the old subtype records no order
  3. reverse which byte ranges?
  4. where did the version nibble land?
  5. a sample is evidence, not proof

basics

~20 s

Legacy subtype 3 records no byte order: the Java and C# legacy encodings reorder the 16 bytes, while subtype 4 always means RFC 4122 order. Across many random UUIDs, where the version nibble lands points to the writer, as evidence and not proof.

solid answer

~50 s

A UUID is stored as 16 bytes of `binData`, and the subtype says which convention applies. Subtype 4 means RFC 4122 order. Subtype 3 is the old form: the Java, C# and Python drivers each wrote it in their own order, and nothing in the value records which. So I would dump the raw bytes and not trust a decoded value, check the subtype, and for random UUIDs look for version nibble `4`: stored offset 6 points to Python legacy, 7 to C# legacy and 1 to Java legacy. The catch is what this cannot prove. One value can match by chance, so it takes many samples. A UUID of another version has no `4` there. One field can hold values from several writers. And subtype 4 only names an order, so a value built by hand can still be wrong.

code

plaintext · 7 lines
plaintext
1D 00 00 00   document length: 29
05            type code: binary
78 00         key "x" as a cstring
10 00 00 00   data length: 16
04            subtype: UUID
00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
00            document terminator

go deeper

for a junior

Know that a UUID is 16 bytes in a BSON binary value, that subtype 4 is the standard form, and that subtype 3 is the older deprecated one.

for a middle

Recite the orders: Java reverses each 8-byte half, C# reverses the first three groups, Python keeps RFC order under the old label. Then predict bytes for a sample UUID.

for a senior

Diagnose from raw bytes rather than decoded values, use the version nibble to identify the writer, and say what a sample does not prove.

for a principal

Decide how much evidence is enough before anyone acts on an inferred writer: how many samples, from which ages and services, and what stays unresolved for UUIDs that are not random.

## Why one UUID has several byte sequences A **UUID** is 16 bytes, and BSON stores it as a binary value: subtype 4 for the standard form, or the deprecated subtype 3 for the older one. BSON's little-endian rule covers fields such as `int32`; it says nothing about the order of data bytes inside a binary value. The Java, C# and Python drivers all used subtype 3 by default, each encoding the bytes in a different order. Subtype 4 was introduced with the RFC 4122 order to end that. For the specification's test UUID `00112233-4455-6677-8899-aabbccddeeff`: | `uuidRepresentation` | Subtype | Stored bytes | Rule | |---|---|---|---| | `standard` | 4 | `00112233445566778899AABBCCDDEEFF` | RFC 4122 order | | `pythonLegacy` | 3 | `00112233445566778899AABBCCDDEEFF` | Same order, older label | | `javaLegacy` | 3 | `7766554433221100FFEEDDCCBBAA9988` | Bytes 0-7 reversed, bytes 8-15 reversed | | `csharpLegacy` | 3 | `33221100554477668899AABBCCDDEEFF` | Bytes 0-3, 4-5 and 6-7 each reversed | Subtype 3 has no field saying which row applies. The writer's choice is not in the data. ## Reading the value Look at the stored bytes, not at what a driver decoded. A decoded value has already been through someone's choice of byte order, and a wrong order still gives a well-formed UUID. 1. **Get the raw value.** `bsondump` prints Canonical Extended JSON, whose `$binary` form shows `base64` and `subType` exactly as stored. 2. **Read the subtype first.** The binary element is a four-byte length, one subtype byte, then the data. The length counts only the data, so a UUID shows `10 00 00 00` followed by `03` or `04`. 3. **If the subtype is 04**, the bytes should be in RFC order, so look first at the reader, then at any code that built the binary value by hand. 4. **If the subtype is 03**, the writer has to be worked out from the data. ## Finding the writer from the version nibble A randomly generated UUID has version nibble `4` as the high nibble of RFC byte 6, and a variant nibble of `8`, `9`, `A` or `B` as the high nibble of RFC byte 8. Reordering moves those bytes, so where they land identifies the order. Counting from 0: | Writer | Version nibble at stored byte | Variant nibble at stored byte | |---|---|---| | `pythonLegacy` | 6 | 8 | | `csharpLegacy` | 7 | 8 | | `javaLegacy` | 1 | 15 | Run it over many documents, and treat the result as a distribution. Any one nibble is `4` about one time in 16 by chance, so a single value is weak evidence, while a nibble that stays `4` across hundreds of values is strong. ## What the bytes cannot prove - **The writer of one value.** The nibble check is statistical, and a sample that agrees says nothing about values outside it. - **The writer of a UUID that is not version 4.** The version nibble then holds another digit, and a mix of versions leaves no constant to look for. Compare against a UUID you can trust from outside the database, such as an application log. - **That one field has one writer.** Several services may have written it, at different times, so sample by age and by service. - **That a subtype 4 value is really in RFC order.** The subtype names an order, and hand-built code can label any 16 bytes with it. A version nibble away from stored byte 6 exposes that. ## Identical bytes that are still different values A hex comparison alone can mislead in the other direction. `standard` and `pythonLegacy` hold the same data under different subtypes, and MongoDB 9.0 compares binary values by length and then by the subtype byte plus data, so a subtype 4 parameter never equals a stored subtype 3 value. Always read the data and the subtype together. To find what remains, MongoDB 9.0's `validate` with `checkBSONConformance: true` treats subtype 3 as non-conformant: it returns a warning, and the log names the deprecated subtype. That finds the old values. It still does not say who wrote them.

  • Why does a subtype 3 value not record which driver wrote it?
    Because subtype 3 only labels the bytes as a UUID, and the format has no field for byte order. Drivers chose their orders independently, so nothing in the value could distinguish them. Subtype 4 solved this by defining one order for the subtype itself, so the label now implies the order.
  • How would you find the writer if the UUIDs are version 7 and not version 4?
    The same idea holds with a different digit. Every UUID carries its version in the high nibble of RFC byte 6, so a field holding only version 7 values shows a constant `7` at the writer's offset. Mixed versions leave no constant, and then the reliable check is comparing the bytes with a UUID known from outside the database.
  • A value has subtype 4 but its version nibble sits at stored byte 1. What does that tell you?
    The label and the bytes disagree. Subtype 4 names RFC order, where a random UUID has that nibble at stored byte 6, and byte 1 is where Java's reordering puts it. So something reordered the bytes and then labelled them 4, most likely code that built the binary value by hand. Check many values before concluding, since one nibble can match by chance.

  • Subtype 3 means Java's byte order, and subtype 4 means everyone else's.
  • UUIDs are little-endian because all BSON values are little-endian.
  • A value that decodes to a well-formed UUID must have been read in the right byte order.
  • Python legacy matches standard byte for byte, so the two values compare as equal.
  • The server reorders UUID bytes to match the client that reads them.
  • A few values with the version nibble at byte 1 prove the whole collection was written by Java.

Assumes MongoDB 9.0 for binary comparison and the conformance warning; byte orders come from the drivers' BSON Binary UUID specification.