skip to content

What rule should a team set for moving MongoDB data between systems: BSON files, Extended JSON or an application format, and how do you decide per consumer?

level: principalimportance: should knowfreq 15%

answer

  1. who reads it next?
  2. bytes, wrappers or a contract
  3. what the specification says it is for
  4. types a consumer must not guess
  5. enforce with a check, not a wiki page

basics

~20 s

Choose by consumer: BSON files between MongoDB deployments, Extended JSON for people and test fixtures, and a versioned application format for anything outside the team. Then enforce it: named flags, one UUID representation, and a type census on every pipeline.

solid answer

~50 s

I would set the rule by who reads the data next, because no single format serves all of them. Between MongoDB deployments, BSON files through `mongodump` and `mongorestore` involve no text step, so types and order stay as stored. For people and test fixtures, Extended JSON fits: canonical where types are asserted, relaxed for reading. Its specification says it is designed mainly for testing and inspection, so I would not make it a contract. For consumers outside the team, I would publish an application format with stated types: decimals as strings, dates as RFC 3339, UUIDs as canonical text, and no `$` wrappers. That costs mapping code and versioning, but it means storage can change without breaking them. Then I would enforce it with default flags, one `uuidRepresentation`, and a type census in each pipeline. A small team with one consumer can stop at fewer rules.

go deeper

for a junior

Recall that BSON files, Extended JSON and an application's own format are three different ways to move data, and that they trade exactness against readability.

for a middle

Explain what each route preserves: stored bytes, type wrappers, or only what a contract states. Know which tool produces which output, and what relaxed mode narrows.

for a senior

Choose for a specific pipeline and defend the choice, including how you would detect a silent type change. Spot the fields, such as dollar-prefixed names, that rule a route out.

for a principal

Set defaults per consumer, name the owner of each contract, and pay for enforcement. Decide when a contract's mapping cost is worth it, and what would make you revisit the rule.

## The decision Every team running MongoDB ends up moving data: into test environments, to analytics, to partners, between services. Without a rule, each job picks its own route, and the differences surface later as a price that became a double or a UUID that decodes differently. A lead's job is to name a default per kind of consumer, and to accept that there is no single right format. The useful question is **who reads this next, and what must they not have to guess?** ## The candidates | Route | Fidelity | Readable | Consumer can be non-MongoDB | Main cost | |---|---|---|---|---| | BSON files (`mongodump`, `mongorestore`) | Stored bytes, no text step | No | Only with a BSON library | Opaque in review and diff; tied to the tools | | Canonical Extended JSON | High, with stated limits | Somewhat | Parses, but wrappers leak | Verbose; `$` keys in every consumer's code | | Relaxed Extended JSON | Numbers lose width | Yes | Yes, for simple documents | Silent type narrowing; some wrappers remain | | Application format | What the contract states | Depends | Yes, by design | Mapping code, versioning, an owner | ## How each one earns its place **BSON files** are the tightest route between MongoDB deployments, because nothing is converted. They are poor as a shared artefact: nobody reviews them, and `bsondump`, the tool for reading them, is documented as a diagnostic tool and not for ingestion. **Extended JSON** is excellent where a person or a test needs to see types. The specification says it is "designed primarily for testing and human inspection" and "not designed to reliably round-trip BSON documents". That wording should shape the rule. Two limits are structural and no flag removes them: - JSON objects are unordered, while BSON documents are ordered. - There is no escape for a leading `$`, so a real field can be indistinguishable from a type wrapper. The specification says Extended JSON should not be used where `$`-prefixed keys could exist, and the tools manual warns about `$` and `.` in field names too. **Relaxed mode** suits a JSON-like consumer, a use the specification itself names for documents built from ordinary JSON types. It is a poor choice as soon as a consumer depends on integer width, or on large integers: a JSON library that holds numbers as IEEE 754 doubles rounds values above 2^53. **An application format** is the only one of these that belongs to you. Decimals as strings with a stated scale, dates as RFC 3339, UUIDs as canonical text, IDs as strings, and a version field. A consumer then has no `$numberDecimal` in its code, and the collection can be remodelled without breaking anyone. The price is real: mapping code, schema evolution, an owner, and one more thing to keep correct. ## A rule you could actually adopt 1. **MongoDB to MongoDB:** BSON files. A text format needs a written reason. 2. **Fixtures and tests:** canonical Extended JSON, checked in, so a fixture states its types. 3. **People debugging:** relaxed or `bsondump`, and never as an input to another job. 4. **Other internal services:** an application format, or relaxed Extended JSON if the documents are simple and the owning team accepts that risk. 5. **Anything leaving the team:** an application format with a version, always. 6. **CSV:** only with `--columnsHaveTypes`, because the auto parser guesses types. ## Rules that travel with the format choice - **One `uuidRepresentation`**, `standard`, set in shared connection configuration. A format rule fails if services disagree on the bytes. - **No `$`-prefixed or dotted field names** in any collection that goes through Extended JSON tools. - **Named flags in jobs:** `--jsonFormat=canonical` written out, not left to the default, so a reader sees the intent. - **A type census** on both sides of every recurring pipeline, failing the job on a difference. Comparing values is not enough, because numeric equality hides narrowing. ## Where the answer changes - **A small team with one consumer** can stop at two rules, since a contract with no second reader is overhead. - **Regulated or financial data** moves the bar up: exact decimals and a type check become mandatory, and text formats need justification. - **Many outside consumers** make the application format worth its cost early, and argue for a schema-managed format on top of it. - **Huge volumes** make text routes costly for size and time alone, whatever their fidelity. The rule also needs a review trigger: revisit it when a new kind of consumer appears, because that is what breaks it.

  • Why not make canonical Extended JSON the one interchange format for everything?
    It keeps types well, but every consumer then carries `$numberLong` and `$binary` handling, which couples them to BSON's type table. The specification also says it is not designed to round-trip reliably, and it cannot escape a real `$`-prefixed field. It suits fixtures and tooling better than a contract.
  • How do you stop the rule from decaying into a wiki page?
    Make the safe path the default and the check automatic. Provide a shared job template with the flags written out, set `uuidRepresentation` in shared configuration, and fail pipelines on a type census mismatch. A review item for new export jobs catches the rest. A rule that nothing enforces gets bypassed by the first urgent job.
  • When is relaxed Extended JSON a reasonable thing to hand to another team?
    When documents use mostly ordinary JSON types, integer width does not matter, and the receiving team accepts the leftover wrappers on ObjectId, dates and decimals. It is a cheap start for one internal consumer. It becomes the wrong answer once several teams depend on it, because the collection's shape is then their contract.

  • Canonical Extended JSON should be the universal contract because it keeps every type.
  • Exposing mongoexport output to partners is fine because it is valid JSON.
  • bsondump output is a good input format for ingestion jobs.
  • One format can serve every consumer, so the team should standardise on it alone.
  • A rule on formats is enough without agreeing a UUID representation.
  • Value comparison after a transfer proves a pipeline is type-safe.