In BSON versus JSON, what work is saved when reading or changing one value in a large document, and what stays linear?
answer
- hopping against scanning
- what a skip costs per element
- no directory of fields
- fixed width against growing strings
- the benefit fades when you read everything
basics
~20 sA BSON reader hops over values using type bytes and length prefixes, where a JSON parser scans all the text before the target. BSON still walks elements in order, with no field index, so lookup stays linear in the elements passed.
solid answer
~50 sTo reach a field in JSON you read all the text before it, tracking quotes, escapes and nesting. In BSON you read each element's type byte and key, then jump: a string or embedded document gives its length, and a number has a width set by its type. So the cost follows the number of elements passed rather than the bytes in their values. On 200 fields of 1,000-byte strings, reaching the last touches about 2 KB rather than about 200 KB. It is still a walk: BSON has no directory of fields, and an array has no offset table. Edits differ too: in a buffer an `int32` can be overwritten in place, but a growing string shifts all later bytes and needs its enclosing lengths rewritten. If you decode every field anyway, skipping saves nothing.
code
plaintext · 15 linesFinding "z" in a BSON document: {"a":"<1000 bytes>","b":7,"z":true}
offset read action
0-3 int32 size note the document's end
4 02 string
5-6 "a" NUL name does not match
7-10 e9 03 00 00 (1001) string length: jump 1001 bytes
1012 10 int32, so the value width is 4
1013-14 "b" NUL name does not match, jump 4 bytes
1019 08 boolean
1020-21 "z" NUL name matches
1022 01 value: true
Reader touched 18 bytes of the 1,024-byte document.
A JSON parser would read the 1,000-byte string to reach "z".go deeper
Recall the picture: a BSON reader jumps over values it does not need because their sizes are known, while a JSON parser reads through all the text in front of its target.
Explain what each hop reads, what still scans, and why a lookup is linear in elements passed: type byte, cstring key, then a length or a type-determined width.
Judge when the advantage is real: large values and partial reads gain most, while decoding every field or reading small documents gains little. Measure the access pattern before crediting the format.
Keep format claims and storage claims apart when a team argues over update cost, because what the bytes permit in a buffer and what the storage engine does on disk are separate questions.
## Two cost models **Traversable** is one of BSON's stated design goals, and it is easy to overstate. It means a reader can move forward through a document without interpreting the values it passes. It does not mean random access. - A **JSON parser** finds the end of a value only by reading to it. It has to examine the text before the target and track whether it is inside a string, whether a quote is escaped and how deep the nesting is. Its cost follows the **bytes before the target**. - A **BSON reader** reads a type byte and a key, then works out where the next element starts. Its cost follows the **number of elements before the target, plus their key bytes**. Those are different scaling factors, and neither one is constant. ## A worked comparison Take a document with 200 fields, each with a five-character name and a 1,000-byte string, and look up the last one. | Reader | Touches per field passed | Total for 199 fields | |---|---|---| | JSON | 5 + 2 quotes, colon, 1,000 + 2 quotes, comma = 1,011 bytes | about 201 KB | | BSON | type 1, name 5, NUL 1, length 4 = 11 bytes | about 2.2 KB | The gap comes entirely from long values. Replace the strings with one-digit integers and JSON reads 10 bytes per field against BSON's 7, so the gap all but closes. Traversal pays off on large values, not on every document. ## What stays linear, and what must still be scanned - **No field directory.** The grammar is `int32 e_list unsigned_byte(0)`, a plain sequence of elements. Finding a name means comparing names from the front. - **No array offset table.** An array is a document keyed by decimal indexes, and its elements can have different widths. Reaching element 500 means walking 500 elements, even though the index looks like a direct address. - **Keys are `cstring`s.** A key has no length, so it is scanned to its NUL. The same goes for a regular expression's two `cstring`s, which is why not every value is a constant-time hop. - **Nesting repeats the walk.** Each level of a dotted path repeats the walk inside the embedded document, which at least lets the reader skip siblings at every level. ## When traversal stops helping The benefit is skipping, so it shrinks as you skip less. - A service that decodes every field into objects passes over nothing, and it typically still validates and copies strings. What remains is the cheaper number handling: the FAQ's point that an integer needs no conversion from text. - A small document has little to skip. - A length prefix is a claim made by the sender, so a reader of untrusted input still has to check it against the buffer. So "BSON decodes faster than JSON" is too flat a claim. Measure with your documents and your access pattern. ## Changing one value Width decides the cost of an edit inside an encoded buffer: | Edit | BSON | JSON | |---|---|---| | `9` becomes `1000` in an `int32` | Overwrite four bytes, nothing moves | Text grows by three bytes, later text shifts | | `true` becomes `false` | Overwrite one byte | Text grows by one byte, later text shifts | | `int32` widened to `int64` | Type byte changes, later bytes shift, lengths rewritten | Depends on the digits | | String grows | Later bytes shift; own and enclosing lengths rewritten | Later text shifts, with no lengths to fix | A growing string is therefore *more* work in BSON, since every enclosing document's `int32` size has to change as well. This is a property of the encoded bytes, not a promise about a store. In MongoDB 9.0, WiredTiger is a no-overwrite storage engine: when blocks are rewritten they go to new locations in the file. A fixed-width field does not give you an on-disk overwrite of the document there. ## How to reason about it 1. Ask what fraction of each document a typical read needs. 2. Ask how large the values passed over are, since skip savings scale with them. 3. Treat a claim about update cost as a claim about buffers, and check what the storage layer actually does.
- Does putting frequently read fields first help?In a reader that stops at the first match, yes: it passes fewer elements. Each hop is cheap, though, so the gain is small unless documents have many fields. Order also affects other things, since BSON documents are ordered and whole-document comparisons depend on that order. Measure before adopting a field-order rule, and do not expect it to matter on small documents.
- Does an increment on an int32 overwrite bytes on disk in MongoDB?Not in place on disk. In MongoDB 9.0, WiredTiger describes itself as a no-overwrite storage engine: when blocks are rewritten, they go to new locations in the file. Fixed width means the encoded field need not change size, which is a property of the bytes. It does not make the storage layer overwrite the document where it sits.
A book with no table of contents, where each chapter's first page states how many pages it runs. You can flip ahead chapter by chapter without reading them, but you still visit every chapter before yours. JSON is the same book without those page counts.
- Length prefixes give BSON random access to any field by name.
- A BSON array can jump straight to element N, since the index is the key.
- BSON always decodes faster than JSON, whatever the document looks like or the reader needs.
- A fixed-width int32 means MongoDB rewrites that field in place on disk.
- Growing a string is cheaper in BSON because its length is stored up front.
Assumes MongoDB 9.0 and WiredTiger for the on-disk clause; the traversal and buffer-edit behaviour comes from the BSON 1.1 grammar and carries no version.