A nightly mongoexport is re-imported into a test MongoDB, and a Decimal128 price arrives as a double and a date as a string: how do you find the cause?
answer
- read the export file before blaming relaxed mode
- which wrappers survive relaxed mode
- CSV cells have no types
- what does auto-detection guess for a cell
- census the types on both sides
basics
~20 sRelaxed mongoexport is not the cause: it writes $numberDecimal and $date, and mongoimport restores both. Read the file. Bare values mean a CSV export or a flattening script stripped the wrappers, and untyped import then guessed double and string.
solid answer
~40 sI would not blame relaxed mode first, because `$numberDecimal` and `$date` both survive it and `mongoimport` turns them back into Decimal128 and Date. So I read the file. If it shows the wrappers, the export is fine and the damage is in whatever consumes it, usually a script that parses with a plain JSON library and flattens the wrappers. If it shows `19.99` and a bare timestamp, something removed them. The classic cause is `--type=csv`: `mongoimport` without `--columnsHaveTypes` auto-parses each cell, so `19.99` becomes a double and the timestamp stays a string. To fix it, keep the JSON path, or declare `price.decimal()` and a date type on the columns. What relaxed does cost is narrower: an Int64 of 42 returns as Int32. I would add a `$type` census on both sides as a check.
code
bash · 7 lines# JSON (default relaxed): price stays Decimal128, at stays Date
mongoexport --uri="$SRC" --collection=orders --out=orders.json
mongoimport --uri="$TEST" --collection=orders --file=orders.json
# CSV, auto-parsed: price becomes Double, at becomes String
mongoexport --uri="$SRC" --collection=orders --type=csv --fields=price,at --out=orders.csv
mongoimport --uri="$TEST" --collection=orders --type=csv --headerline --file=orders.csvgo deeper
Recall that JSON and CSV have fewer types than BSON, so an export can change a value's type. Know that mongoexport defaults to relaxed Extended JSON and can write CSV.
Explain which wrappers relaxed mode keeps, and what mongoimport does with a bare CSV cell: try an integer, then a float, otherwise keep a string.
Show the method: census the source, read one line of the file, find the hop that removed the wrappers, then census the target. Refuse to name a cause before reading the file.
Treat a silent type change as a pipeline defect that needs a guard. Put a type census in the job, and decide which hand-offs may use untyped formats at all.
## Start by doubting the obvious suspect The tempting diagnosis is "`mongoexport` defaults to relaxed Extended JSON, and relaxed loses types". The first half is true and the second half does not explain these two symptoms. In the conversion table, **Decimal128 is written the same in both modes**, as `{"$numberDecimal": "19.99"}`, and a date from 1970 to 9999 stays inside a `$date` wrapper as an ISO string. `mongoimport` parses Extended JSON and turns both back into their BSON types. So a plain JSON export followed by a plain JSON import does **not** produce a double price and a string date. Something else in the pipeline does. ## The diagnostic sequence 1. **Confirm the source.** Run a `$type` census on the source collection. If some prices were already doubles, an old writer did it and the export is innocent. 2. **Read the file.** Open a line of the export. This one look splits the problem in two. 3. **Trace the hop that stripped the types.** The file's form tells you which hop to read. 4. **Census the target** with the same query and compare the two results. ## If the file has wrappers `{"price":{"$numberDecimal":"19.99"},"at":{"$date":"2026-11-03T00:00:00Z"}}` means the export is correct. Look at what reads it: - A script that uses a **language-native JSON parser** sees `price` as an object with one string field. Code that "cleans" it with something like `float(v["$numberDecimal"])` makes the double on purpose, and code that takes `v["$date"]` makes the string. - A script that inserts the parsed object unchanged usually produces a different symptom: `price` lands as an embedded document with a `$numberDecimal` field instead of a number at all. - A real Extended JSON parser, such as `mongoimport` in its JSON mode or a driver's Extended JSON helper, restores the types. ## If the file has bare values Then the wrappers were removed before the import, and the type decision moved to whatever parsed the text. A middle step that rewrites the JSON without wrappers has this effect: under Extended JSON's number rules a bare `19.99` is a Double, and a bare timestamp is simply a String. The most common route is CSV: - `mongoexport --type=csv` writes a decimal as `19.99` and a date as `2026-11-03T00:00:00.000Z`. A CSV cell has no type. - `mongoimport --type=csv` without `--columnsHaveTypes` uses its **auto parser** on each cell: try an integer, then a float, else keep the string. | Cell | Auto parser gives | Note | |---|---|---| | `19.99` | Double | Decimal128 is never guessed | | `20` | Int32 | the same price column, now a mix of types | | `19.90` | Double 19.9 | the written scale is gone | | `2026-11-03T00:00:00.000Z` | String | dates are never guessed | | `007` | Int32 7 | an ID string loses its zeros | That table reproduces the reported symptoms exactly, and predicts a third one worth checking: whole-number prices become integers, so the column ends up with mixed types. ## The fix - **Stay on JSON** when the two ends are MongoDB tools. It keeps Decimal128 and Date with no extra flags. - **Use `--jsonFormat=canonical`** if integer width matters as well. - **Declare column types** if CSV must stay: `--columnsHaveTypes` with a field list such as `price.decimal()` and `at.date_ms(<format>)`. - **Parse with an Extended JSON library** in any script in the middle, and do not flatten wrappers by hand. - **Keep the census** as a job step that fails when the source and target type counts differ. ## What relaxed mode really costs on this path Relaxed mode is not free, only innocent of these two symptoms. An Int64 that fits in 32 bits comes back as **Int32**. Numeric equality still matches, so most tests pass, but a `$type: "long"` filter misses it and a schema validator requiring `bsonType: "long"` can reject it. Doubles usually survive, because the tools write `42.0` and it parses back as a double, but a middle step that rewrites `42.0` as `42` turns it into an Int32. Canonical mode removes both effects. Field names matter here too. The manual warns that `mongoimport` and `mongoexport` do not work with `$`-prefixed or dotted field names, so a collection with such names needs another route altogether.
- If a CSV hand-off cannot be removed, how do you keep the types?Declare them. `mongoimport --type=csv --columnsHaveTypes` with `--fields` or a header naming `price.decimal()`, `at.date_ms(<format>)` and `zip.string()` replaces auto-detection with a parser per column. The date format has to match what the export wrote, so a test import of a few rows with a type census is worth keeping.
- Why might a relaxed round trip pass every test and still have changed the data?Because numeric equality ignores width. An Int64 of 42 that returns as Int32 still matches `{qty: 42}`, so value-level assertions pass. Only something that inspects the type sees it: a `$type` filter, a schema validator, or a byte-level comparison. That is why the check should compare types and not only values.
- Which part of this diagnosis would change if the file came from bsondump?`bsondump` writes canonical Extended JSON, so every number keeps its wrapper and the file shows the source types directly. That makes it a good way to inspect types, but its documentation calls it a diagnostic tool and not one for ingestion, so it should not become a step in the nightly job.
- Relaxed mode turns a Decimal128 price into a double on export.
- Relaxed mode writes dates as bare strings, so mongoimport cannot restore them.
- Comparing values after import proves the types survived the round trip.
- mongoimport auto-detects decimals and dates in CSV cells.
- Switching to canonical fixes a pipeline whose CSV step strips the types.
- A script using any JSON library can safely read mongoexport output.