Should a team set a fleet-wide rule shortening BSON field names to save space, and how would you decide?
answer
- cost per document times documents
- share of document size
- what block compression reclaims
- uncompressed in the cache
- who pays for cryptic names
basics
~20 sUsually not as a blanket rule. Names sit in every document, but block compression can reclaim much on disk and cryptic names cost every reader. Measure their share of document size, and shorten only where small documents meet huge volume.
solid answer
~50 sI would not make it fleet-wide. BSON stores every name in every document, exactly as JSON does, so the cost scales with documents times name bytes, and its share is highest on small documents. The manual's `{last_name, best_score}` example saves 9 of 46 bytes when renamed, which is about 20% but only 9 GB per billion documents before compression. In MongoDB 9.0 snappy block compression can reclaim much of that on disk, though collection data sits uncompressed in the WiredTiger cache, and indexes do not store field names at all. Against that stand readability, ad hoc queries, and a rewrite of every document for any later rename. So my rule is readable names by default, with exceptions per collection on measured evidence, and a look at the document model first, since that often saves more.
code
plaintext · 14 lines{"last_name":"Smith","best_score":3.9} as BSON
size prefix 4
type 02 + "last_name" + NUL = 1 + 10 11
string: int32 + "Smith" + NUL = 4 + 6 10
type 01 + "best_score" + NUL = 1 + 11 12
double 8
end of document 1
total 46 (name characters: 9 + 10 = 19)
Renamed to {"lname":"Smith","score":3.9}:
4 + (1 + 6) + 10 + (1 + 6) + 8 + 1 = 37 (saves 9)
Compact JSON: 38 bytes before, 29 after (also saves 9)go deeper
Recall that BSON stores each field name in full in every document, so longer names mean larger documents, most noticeably when the documents themselves are small.
Work the arithmetic per document and per billion documents, and explain why the same rename saves the same bytes in BSON and in compact JSON.
Separate where the cost lands: compressed on disk, uncompressed in the cache, unaffected in indexes. Measure the names' share of average document size before proposing any rename.
Own the standing rule: readable names by default, a measured threshold for exceptions, a named approver, and a check at design time, since a rename on a large collection costs far more later.
## What a field name costs, worked **BSON** writes a key as a `cstring` in every element of every document, with no dictionary. So does **JSON** text, so this is a cost of self-describing documents and not of the binary encoding. Moving between the two formats does not change it. The MongoDB manual's own example: | Document | Compact JSON | BSON | |---|---|---| | `{"last_name":"Smith","best_score":3.9}` | 38 | 46 | | `{"lname":"Smith","score":3.9}` | 29 | 37 | The rename saves **9 bytes** per document in both formats. In BSON, the original names hold 19 of 46 bytes, about 41%. Now scale it: - This small document: 9 bytes, so **9 GB per billion documents** before compression. - A wider one, with 20 fields whose names shrink by 8 bytes each: 160 bytes per document, so **160 GB per billion**. - A 2,000-byte document saving those same 160 bytes: 8%, on a store where the long values dominate. Name cost is a share question first and an absolute question second. ## Where the cost lands in MongoDB 9.0 | Place | Effect of long names | |---|---| | Disk | Collections use snappy block compression by default, and repeated names tend to compress well | | Filesystem cache | Same format as on disk, including compression | | WiredTiger internal cache | Collection data is uncompressed, so names take full space | | Network | Full size unless the driver enables compression with `compressors` | | Indexes | No effect: the manual says indexes do not incorporate field names | | `$bsonSize` | Names count in full, including towards the document size limit | The internal cache row is often the real argument. When the working set has to fit in memory, a saving that disk statistics hide can still be worth having. ## What shortening costs - **Readability, permanently.** Everyone who writes a query, reads a log or debugs a document pays this. The manual itself warns that shortening names might reduce expressiveness. - **Rename cost.** Renaming means rewriting every existing document and moving every reader, and during that window the code has to accept both names. - **A mapping layer.** Long names in code and short names in storage bring readability back to the application, but not to ad hoc queries, dashboards or exports. - **Mistakes.** `st` may mean status, state or start, and nothing in a self-describing document can correct a misreading. ## Options that may beat a rename The manual's note says it is often more effective to change the overall document model. Consider these first: - Replace repeated embedded objects with a more compact shape, since an array of objects repeats its inner names per element. - For measurements, use a time series collection, which keeps data in a columnar format. - Drop fields that nothing reads, which saves both name and value. Moving to a schema-driven encoding removes names from the data altogether, but that is a different decision, involving every consumer and every schema change. ## How I would decide 1. **Measure the share.** On a sample, add up name bytes and framing, then divide by average `$bsonSize`. 2. **Price it where it hurts.** Test on a copy to see what compression already gives you, and estimate the cache effect separately, using document counts in the working set. 3. **Compare with the model change** that the same effort would buy. 4. **Set the default and the exception.** My default is readable, consistent names, with short names only by reviewed exception on named collections: typically telemetry-like data with very small documents and very high volume. 5. **Decide early.** Shortening is cheap for a new collection and expensive for a large one, so the time to check is during design. ## There is no single right answer A team storing 500 billion small sensor readings and a team storing a few million order documents should reach opposite conclusions under the same rule. Be wary of a blanket "always short" rule, because it taxes everyone to help a few collections, and equally of "never", because it can leave real memory cost on the table. What a lead owns is the measurement, the threshold and who can grant the exception.
- Does shortening field names shrink indexes?No. The MongoDB manual says shortening names does not affect the size of indexes, since indexes have a predefined structure that does not incorporate field names. So an index-heavy memory problem is not helped by a rename. Anyone proposing short names to reduce index size is targeting the wrong structure, and should look at which indexes exist and how selective they are.
- If disk compression handles repeated names, why might a team still care?Because disk is not the only place the bytes live. In MongoDB 9.0, collection data in the WiredTiger internal cache is uncompressed, so long names reduce how many documents fit in memory. Network traffic also carries them at full size unless the driver enables compression. For a workload whose working set barely fits, that can matter even when disk figures look fine.
- Short field names are always worth it, since names are stored in every document.
- Shortening field names will shrink the collection's indexes as well.
- Field names are a BSON-only cost that moving to JSON text would remove.
- Block compression makes field-name length irrelevant to memory use in every workload.
- Renaming a field later is cheap, so naming can be fixed whenever size becomes a problem.
Assumes MongoDB 9.0 for snappy default block compression, the uncompressed WiredTiger cache and the manual's guidance on field names; the byte counts come from BSON 1.1.