skip to content

A BSON reader meets deprecated type codes 6, 12, 14 or 15 in old data: what are they, and how should it handle them?

level: seniorimportance: nice to knowfreq 15%

answer

  1. still in the grammar
  2. Undefined, DBPointer, Symbol, code with scope
  3. reading is not the same as writing
  4. what comes back after a round trip?
  5. a conversion can grow the document

basics

~20 s

Codes 6, 12, 14 and 15 are Undefined, DBPointer, Symbol and code with scope. Deprecated means new data should avoid them, not that they are invalid: read them by layout, never emit them, and expect a driver's mapping to change them on rewrite.

solid answer

~40 s

`\x06` is Undefined, with no value bytes. `\x0C` is DBPointer, a string plus 12 bytes. `\x0E` is Symbol, laid out like a string. `\x0F` is code with scope, an `int32` total, a string and a document. Deprecated does not make them invalid: they stay in the grammar, so a reader needs their layouts to get past them, and the corpus includes decode vectors for each. A reader should parse them and new writers should not produce them. The danger is the round trip. Corpus conversions turn Undefined into null, Symbol into string and DBPointer into a `$ref`/`$id` document, and PyMongo does the same, so rewriting a document can change its types without anyone asking for it.

code

javascript · 7 lines
javascript
// Count fields holding each deprecated type before planning a migration.
for (const t of ["undefined", "dbPointer", "symbol", "javascriptWithScope"]) {
  print(t, db.orders.countDocuments({ ref: { $type: t } }));
}

// Collection-wide check; on MongoDB 9.0 it warns and logs each deprecated type.
db.runCommand({ validate: "orders", checkBSONConformance: true });

go deeper

for a junior

Know the four names and that they still exist in old data, and that new code should not write them.

for a middle

Lay each one out: Undefined with no value, DBPointer as string plus 12 bytes, Symbol as a string, code with scope with its all-inclusive int32.

for a senior

Point at the round trip: a driver maps these to native types, so a read-modify-write changes type codes in fields nobody touched. Audit first, then convert deliberately.

for a principal

Decide when old types are worth migrating at all: the cost and risk of a rewrite against the burden of keeping every tool able to read them, and how to stop new ones appearing.

## The four codes The BSON 1.1 grammar marks four element types as **deprecated** and still defines how each is laid out: | Code | Name | Value layout | Corpus vector under key `a` | |---|---|---|---| | `\x06` | Undefined | Nothing | `08 00 00 00 06 61 00 00` | | `\x0C` | DBPointer | `string`, then 12 bytes | 26-byte document | | `\x0E` | Symbol | `string` | 14 bytes when the value is `b` | | `\x0F` | Code with scope | `int32`, `string`, document | 26 bytes for code `abcd` and an empty scope | JavaScript code without a scope, `\x0D`, is **not** deprecated, and the two binary subtypes marked old, 2 and 3, are a separate list. The `int32` in code with scope covers the entire value, itself included, and that differs from the length rule for a binary value. ```plaintext 1A 00 00 00 document length: 26 0F type code: code with scope 61 00 key "a" as a cstring 12 00 00 00 whole value: 18 bytes, counting these four 05 00 00 00 code string length: 5 61 62 63 64 00 code "abcd" and its NUL 05 00 00 00 00 scope: an empty document 00 document terminator ``` The value is 4 + 4 + 5 + 5 = 18, and the document is 4 + 1 + 2 + 18 + 1 = 26. ## What deprecated actually means Deprecated is a statement about writing new data. It does not make old data illegal, and three parties treat it differently: - **The specification** labels the types and keeps their grammar. It sets no explicit rule for how a reader must treat them. - **The corpus** says its files for deprecated types are informational, and implementations may ignore or modify them to match legacy treatment. Its `deprecated` flag names Symbol, Undefined and DBPointer. Its code-with-scope file does not carry that flag, even though the grammar and the server both call the type deprecated. - **The server**, in MongoDB 9.0, parses these types in its normal validation. Running `validate` with `checkBSONConformance: true` returns a warning about non-conformant documents, logs the reason `Use of deprecated BSON type`, and changes nothing. A reader cannot just ignore them. An element has no length of its own, so skipping one still needs its layout. A library that drops these codes from its table has to stop at them, as with any unknown code. ## The real risk: the round trip A driver has to give you something in your language, and most languages have no matching type. The corpus shows the common conversions in its `converted_bson` fields, while stating that conversion is outside its scope: | Stored as | Corpus conversion | PyMongo 4.19 decodes to | Type code after re-encoding | |---|---|---|---| | Undefined | null | `None` | `\x0A` | | Symbol | string | `str` | `\x02` | | DBPointer | `$ref`/`$id` document | `DBRef` | `\x03` | | Code with scope | none given | `Code` with scope | `\x0F`, unchanged | So a service that reads a document, changes one field and writes the whole thing back has changed other fields too. Some consequences are invisible and some are not: - **Symbol to string** keeps the size, but a `$type` check for `symbol` stops matching. - **Undefined to null** keeps the size, but the type changes. - **DBPointer to document** grows the value. The corpus vector goes from 26 to 42 bytes, 16 more, because the namespace and id gain their own keys and type bytes. ## How to handle them 1. **Read everything.** Keep the four layouts in any parser or tool, so old data does not stop it. 2. **Never emit them.** New code should write null, a string, a `$ref`/`$id` document or plain code. 3. **Count before you change anything.** The `$type` query operator has aliases for each: `undefined`, `dbPointer`, `symbol` and `javascriptWithScope`. A collection-wide `validate` with `checkBSONConformance: true` also finds them, at a cost in time, with the details in the server log. 4. **Convert on purpose.** Prefer a dedicated migration to an accidental rewrite, and allow for growth in documents near a size limit. 5. **Test the round trip** with the corpus files, using the `converted_bson` values to check what your own library actually does. In Extended JSON, each has its own wrapper in canonical form: `$undefined`, `$dbPointer`, `$symbol`, and `$code` with `$scope`. That makes an export a convenient way to see them, since the wrappers keep the types visible.

  • Why does a parser need the layout of a type it will never produce?
    Because an element has no general length field. To reach the next element a parser has to know how the current one ends: no bytes for Undefined, a string plus 12 bytes for DBPointer, and so on. Without those rules, one deprecated field makes the rest of the document unreadable.
  • Is plain JavaScript code, type 13, deprecated too?
    No. The BSON grammar marks only code with scope, type 15, as deprecated, while type 13 stays as an ordinary type. The MongoDB 9.0 conformance check agrees: it flags code with scope but not plain code. PyMongo 4.19 writes a `Code` with no scope as type 13 and one with a scope as type 15.

  • Deprecated BSON types are invalid, so a reader should reject the whole document.
  • A reader can skip a deprecated element without knowing its layout.
  • Reading and rewriting a document always preserves every field's type code.
  • The server converts deprecated types automatically when validate finds them.
  • Plain JavaScript code, type 13, is deprecated along with code with scope.
  • Converting a DBPointer to a $ref and $id document leaves its size unchanged.

Assumes MongoDB 9.0 for the conformance check on deprecated types; layouts come from BSON 1.1 and the driver mapping was read in PyMongo 4.19.