A team moves JSON documents to BSON expecting to save space: what do they measure on short numeric-heavy and long string-heavy documents, and why?
answer
- count framing, then each value
- digits against four fixed bytes
- array indexes are stored as keys
- strings: one extra byte each
- escapes and dates reverse it
basics
~20 sExpect little saving. Documents of small numbers grow a few bytes, arrays of small numbers can grow several times over, and long plain strings stay within about a percent. BSON wins on long numbers, dates, binary and escape-heavy text.
solid answer
~40 sFor a flat, non-empty document, BSON minus compact JSON is `4 + Σ(b − t − 2)`, where `b` is a value's binary size and `t` its JSON text size. A string adds 1. An `int32` adds `2 − d` for `d` characters, so one digit adds 1 and three digits save 1. So `{"temp":21,"hum":60,"rpm":900}` is 30 bytes of JSON against 33 of BSON, while `{"n":1234567890}` is 16 against 12. A 1,000-character plain string is 1,011 against 1,016. Arrays are the worst case, since every element stores its index as a key: `{"a":[1,2,3]}` is 13 against 34. Field names are not the culprit, because both formats carry them. Datetimes, binary values and heavily escaped strings go BSON's way. Measure real documents with `$bsonSize`, and compare on-disk sizes only after compressing both sides.
code
plaintext · 14 lines{"a":[1,2,3]} as BSON, 34 bytes in total (compact JSON: 13)
offset bytes bytes meaning
0-3 22 00 00 00 4 outer total size, 0x22 = 34
4 04 1 element type: array
5-6 61 00 2 e_name "a" + NUL
7-10 1a 00 00 00 4 array document size, 0x1a = 26
11-17 10 30 00 01 00 00 00 7 int32, key "0" + NUL, value 1
18-24 10 31 00 02 00 00 00 7 int32, key "1" + NUL, value 2
25-31 10 32 00 03 00 00 00 7 int32, key "2" + NUL, value 3
32 00 1 end of the array document
33 00 1 end of the outer document
Array document: 4 + 7 + 7 + 7 + 1 = 26. Outer: 4 + 1 + 2 + 26 + 1 = 34.go deeper
Remember that BSON is not automatically smaller: small numbers and arrays usually grow, while dates and very long numbers usually shrink.
Explain the mechanics by counting: framing of five bytes against two, four fixed bytes against a digit, a length on every string, and index keys on every array element.
Measure before promising savings: sample real documents, compare encoded size separately from compressed disk size, and allow for what the store itself adds to each document.
Treat a storage forecast as a shape question, and ask which document classes dominate the bill, because arrays of small numbers and long text move the total in very different ways.
## The assumption being tested "Binary is smaller" is false as a flat claim, and so is "BSON is bigger". The honest answer depends on the document's shape, and you can compute it. Every count here is **compact JSON** with no spaces, against BSON as `spec.html` (version 1.1) lays it out. ## A formula you can recount For a flat document with at least one field: - JSON = `1 + Σ(k + 4 + t)`: the braces, the two quotes, colon and comma around each name of `k` bytes, plus the value's text `t`, with one comma fewer than fields. - BSON = `5 + Σ(k + 2 + b)`: the `int32` size and trailing NUL, the type byte and key NUL around each name, plus the value's binary size `b`. - So **BSON minus JSON = `4 + Σ(b − t − 2)`**. | Value | `b` | `t` | Adds to the difference | |---|---|---|---| | String of `v` bytes, nothing to escape | `v + 5` | `v + 2` | `+1` | | `int32` written in `d` characters | 4 | `d` | `2 − d` | | `double` written in `t` characters | 8 | `t` | `6 − t` | | `true` | 1 | 4 | `−5` | | `null` | 0 | 4 | `−6` | Notice what is missing: the **field name**. Its characters appear on both sides, and JSON spends four bytes around it against BSON's two. Repeated names are a real cost against a schema-driven format, not against JSON. ## Short numeric documents | Document | JSON | BSON | |---|---|---| | `{"n":1}` | 7 | 12 | | `{"a":1,"b":2,"c":3}` | 19 | 26 | | `{"temp":21,"hum":60,"rpm":900}` | 30 | 33 | | `{"p":21.5}` | 10 | 16 | | `{"n":1234567890}` | 16 | 12 | | `{"pi":3.141592653589793}` | 24 | 17 | The result depends on digits. Small integers and short decimals grow, and long numbers shrink. The stored type matters too: `1` kept as `int64` or `double` makes `{"n":1}` 16 bytes rather than 12. ## Arrays lose the most A BSON array is a document whose keys are the decimal indexes, so each element pays for a type byte and a key. In `{"a":[1,2,3]}` each element is 7 bytes against JSON's digit and comma, giving 34 bytes against 13. At 1,000 elements of `int32`: - Index keys with their NULs: `10×2 + 90×3 + 900×4` = 3,890 bytes - Type bytes 1,000, values 4,000, array framing 5: array total 8,895 - With 8 more bytes for the outer document, **8,903 bytes** The JSON for 1,000 single-digit values is **2,007 bytes**, so BSON is about 4.4 times larger. With seven-digit values JSON is 8,007, about 10% smaller. With 17-character doubles JSON is 18,007 and BSON 12,903, so BSON wins. ## Long strings and typed values - A plain 1,000-character field: 1,011 bytes of JSON, 1,016 of BSON, about 0.5% more. - Five 200-character fields: 1,041 against 1,050, one extra byte per string plus framing. - The same 1,000 characters with 100 newlines: 1,111 against 1,016, because JSON writes each newline as two bytes. String-heavy documents therefore stay about level unless they hold a lot of escaping. An encoder that writes non-ASCII characters as `\uXXXX` also loses to BSON's raw UTF-8. Values that JSON has to spell out as text go BSON's way by a wider margin: | Value | JSON | BSON | |---|---|---| | `{"d":"2026-11-03T12:00:00.000Z"}` as a datetime | 32 | 16 | | `{"_id":"56e1fc72e0c917e9c4714161"}` as an ObjectId | 34 | 22 | | A 16-byte UUID under key `x`, as 36 characters against `binData` | 44 | 29 | ## Measuring it properly in MongoDB 9.0 1. Sample real documents, since the shape decides everything. 2. Compare compact JSON bytes with `$bsonSize`, which returns a document's encoded size. Allow for `_id`: an ObjectId under that name adds 17 bytes. 3. Compare `collStats.size`, the uncompressed total, with `storageSize`, the allocated storage. WiredTiger block-compresses most collections with snappy by default, so disk size is not BSON size. 4. Compress the JSON the same way before comparing disk figures, or the test compares a compressor with a format. The team's honest report is therefore per shape: which classes of document grew, which shrank, and by how many bytes each, instead of a single percentage for the whole move.
- Why are field names not the reason BSON comes out larger than JSON?Both formats write every name in every document. Around a name, compact JSON spends two quotes, a colon and a comma, while BSON spends a type byte and a NUL. Renaming `last_name` to `lname` and `best_score` to `score` saves 9 bytes in either format. BSON's overhead comes from framing, string lengths, small fixed-width numbers and array index keys.
- Why does a BSON array of numbers grow so much more than a document of named numbers?In JSON, an array element is its text and a comma, with no key at all. In BSON, an array is a document, so each element adds a type byte and a decimal index key with its NUL. For index 0 to 9 that is three bytes before the value, and those keys lengthen as the index grows. A named field pays for its name in both formats, so only the array pays extra.
- Does the on-disk comparison in MongoDB match the BSON byte counts?No. In MongoDB 9.0, WiredTiger block-compresses most collections with snappy by default, so `storageSize` reflects compressed data and can be below the uncompressed `collStats.size`. Compare `$bsonSize` for format overhead, and compare compressed JSON with `storageSize` for disk. Mixing the two compares a compressor with a format and answers neither question.
- Moving JSON documents to BSON always shrinks them, because binary is smaller than text.
- BSON is larger than JSON mainly because it repeats field names and JSON does not.
- An array in BSON stores only its values, so it costs about the same as in JSON.
- The integer 1 costs the same in both formats because it is one digit.
- Long strings shrink a lot in BSON because the text is stored in binary.
- storageSize tells you how much space the BSON encoding takes per document.
Assumes MongoDB 9.0 for $bsonSize, collStats and default snappy block compression; the byte counts come from BSON 1.1 and carry no version.