skip to content

A MongoDB filter by UUID finds nothing after one service moved to uuidRepresentation=standard while another still writes the old way: what does the setting decide, and how do you fix it?

level: seniorimportance: should knowfreq 22%

answer

  1. who chose the bytes: driver, not server
  2. subtype 3 does not record byte order
  3. same UUID, three legacy orders
  4. subtype takes part in equality
  5. standardise, then rewrite old values

basics

~20 s

Drivers encode native UUIDs by uuidRepresentation: standard is subtype 4, while the Java, C# and Python legacy forms are subtype 3 with their own byte orders. Mismatches misread values or miss filters. Inspect stored bytes, set standard, and rewrite legacy values.

solid answer

~40 s

The server stores opaque binary, so the driver decides the bytes. `uuidRepresentation=standard` writes subtype 4 in RFC 4122 order. `javaLegacy`, `csharpLegacy` and `pythonLegacy` write subtype 3, with Java and C# reordering bytes and Python keeping standard order. Subtype 3 does not record which writer made it, so a `javaLegacy` client reading C# bytes decodes the wrong UUID without any error. Across the boundary the symptom differs: a `standard` client reading subtype 3 gets a binary value, not a UUID, and a filter misses because the subtype is part of equality. To diagnose, look at stored `subType` and base64, and compare with a UUID you know from an application log. To fix, set `standard` everywhere, then rewrite each legacy value, reading it with the representation that wrote it.

code

plaintext · 6 lines
plaintext
UUID 00112233-4455-6677-8899-aabbccddeeff

standard      subtype 4  00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
pythonLegacy  subtype 3  00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
javaLegacy    subtype 3  77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88
csharpLegacy  subtype 3  33 22 11 00 55 44 77 66 88 99 AA BB CC DD EE FF

go deeper

for a junior

Recall that a UUID in BSON is binary with a subtype, that subtype 4 is the standard one, and that a driver setting called uuidRepresentation decides how native UUIDs are encoded.

for a middle

Explain the table: standard is subtype 4, the legacy values are subtype 3, and Java and C# reorder bytes while Python does not. Predict what a given client decodes from a given subtype.

for a senior

Diagnose from stored bytes against a UUID known from outside the database. Separate silent misreads between legacy settings from binary values and failed filters at the standard boundary, and sequence the rewrite safely.

for a principal

Own one representation for the organisation and enforce it in shared connection configuration. Weigh the cost of rewriting legacy data, including _id and references, against carrying the ambiguity indefinitely.

## Whose decision this is A UUID has one canonical text form, but BSON stores 16 bytes and a subtype. **The server stores and compares those bytes without interpreting them**, so the **driver** decides what goes on the wire. Historically the Java, C# and Python drivers each chose subtype 3 and each chose its own byte order. Subtype 4 was added with the byte order fixed by RFC 4122, and the UUID specification now asks drivers to make the application state its choice through `uuidRepresentation`. ## One UUID, four encodings The specification's own test vector is `00112233-4455-6677-8899-aabbccddeeff`: | `uuidRepresentation` | Subtype | Stored bytes (hex) | Reordering | |---|---|---|---| | `standard` | 4 | `00112233445566778899AABBCCDDEEFF` | none | | `pythonLegacy` | 3 | `00112233445566778899AABBCCDDEEFF` | none | | `javaLegacy` | 3 | `7766554433221100FFEEDDCCBBAA9988` | bytes 0-7 reversed, bytes 8-15 reversed | | `csharpLegacy` | 3 | `33221100554477668899AABBCCDDEEFF` | bytes 0-3, 4-5 and 6-7 each reversed | Note the second row: `standard` and `pythonLegacy` differ **only in the subtype**. It follows that a value can look right in hex and still not match. ## How the mismatch shows up The decode table in the specification explains each symptom: | Client setting | Reads subtype 4 as | Reads subtype 3 as | Encodes a native UUID as | |---|---|---|---| | `unspecified` | binary | binary | raises an error | | `standard` | native UUID | binary | subtype 4 | | any legacy value | binary | native UUID, in its own order | subtype 3 in its own order | - **Silently wrong value.** Two legacy settings disagree: a `javaLegacy` client reads bytes a `csharpLegacy` client wrote and gets a valid-looking, different UUID. Nothing fails, because subtype 3 does not record who wrote it. - **A binary value where a UUID was expected.** A `standard` client reads subtype 3, or a legacy client reads subtype 4. With a strongly typed field the specification says the driver raises an exception instead. - **A filter that finds nothing.** The stored value is subtype 3 and the filter encodes subtype 4. Binary comparison looks at length, then subtype, then bytes, so even identical bytes do not match. - **An encoding error on insert.** The client is on `unspecified`. PyMongo has defaulted to this since 4.0 and raises `ValueError` when asked to encode a native UUID. ## Diagnosing it 1. **Read the stored value without a UUID codec in the way.** `bsondump` on a dumped file shows `$binary` with `base64` and `subType`. A client on `unspecified` works too, because it decodes both subtypes to binary. 2. **Take a UUID you can trust from outside the database**, such as an application log, and compare its bytes with the table above. That tells you the writer. 3. **If you have no known value, use the version nibble as a heuristic.** Random UUIDs have 4 as the high nibble of byte 6 in RFC order. Under Java's reordering that nibble lands in byte 1, and under C#'s in byte 7. It is evidence, not proof. 4. **Read each service's setting** in its connection string or codec options. C# is the exception, because its serializer registry is global and the specification allows a global setting there. ## Fixing it - **Set `uuidRepresentation=standard`** in every service that writes or reads the field, in one coordinated rollout. Older data needs to keep working during that rollout, so plan the read path first. - **Rewrite old values** per writer. There is no blanket conversion, because you must know which order wrote each value. Read as `Binary`, convert with the explicit helper for that representation (`Binary.as_uuid(JAVA_LEGACY)` in PyMongo), and write back as subtype 4. - **Treat `_id` specially.** It cannot be updated in place, so those documents need an insert and a delete, plus the same change to anything that references them. - **Expect sort order to change**, because the byte order is different. Range scans on the old order stop meaning what they did. - **Sweep with `validate` and `checkBSONConformance: true`** afterwards. In the MongoDB 9.0 source it flags subtype 3 as a deprecated binary subtype, so it can confirm that the rewrite finished. Extended JSON has its own shortcut: the specification lets parsers accept `{"$uuid": "<text>"}`, and they must read it as **subtype 4**. It is convenient for fixtures, but it will not match subtype 3 data.

  • Why not just set every service to one legacy value, instead of migrating?
    It stops new disagreement but keeps every mixed-writer value wrong under that setting, because subtype 3 does not say who wrote it. It also keeps a format the server's conformance check reports as deprecated. A legacy setting can be a safe interim step when all data has one writer, and the rewrite then has a single known source.
  • What does the MongoDB server do about UUID byte order?
    Nothing: it stores and compares the binary. Byte order is a driver concern set by the UUID specification. The server's stricter BSON conformance check looks only at the subtype: subtype 4 must be 16 bytes and subtype 3 is reported as deprecated. Neither check can tell Java bytes from C# bytes.
  • Does {"$uuid": "..."} in Extended JSON help with legacy data?
    Not directly. The specification says parsers must read `$uuid` as binary subtype 4. It gives readable fixtures for standard data, but a filter built from it will not match a subtype 3 value, even when the bytes are identical, as they are with `pythonLegacy`.

A date written 03/04/2026 has all its digits, but nothing on the page says day-first or month-first. Subtype 3 is like that: the bytes are present and the order is unstated. Subtype 4 is an ISO date, where the order is part of the format.

  • The server normalises UUID byte order, so all drivers read the same value.
  • Subtype 3 means Java's byte order.
  • pythonLegacy and standard are the same encoding, so they match in a filter.
  • Changing uuidRepresentation in the connection string converts existing data.
  • A wrong UUID always raises a decoding error in the driver.
  • A single migration script can convert all subtype 3 values without knowing the writer.

Assumes MongoDB 9.0 for the conformance check on stored subtypes; uuidRepresentation itself is a driver setting.