In MongoDB, what does mongodump write out, and why is mongoexport not a substitute for it as a backup?
answer
- binary copy against text copy
- BSON files beside metadata files
- definitions, not index data
- one database a full run skips
- one collection at a time
basics
~20 smongodump writes a binary copy: BSON documents plus metadata holding collection options and index definitions. mongoexport writes one collection as JSON or CSV with no indexes or options, and its docs say plainly that it is not a backup tool.
solid answer
~40 s`mongodump` connects to a running `mongod` or `mongos` and writes each collection as a `.bson` file beside a `.metadata.json` file that holds its options, UUID and index definitions. A full run also carries users and roles, and it skips the `local` database. The indexes themselves aren't copied, only their definitions, so `mongorestore` rebuilds them after loading the data. `mongoexport` takes one collection per run and writes JSON or CSV. Its default relaxed Extended JSON can lose type detail, it records no indexes or collection options, and the tools docs say it isn't meant for backing up, or for converting data to JSON and back. So I use `mongodump` to back up a small deployment and `mongoexport` when a person or another system needs to read the data.
code
bash · 3 linesmongodump --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=rs0" --out=/opt/backup/nightly
mongoexport --uri="mongodb://mongodb0.example.com:27017/reporting" --collection=events --type=csv --fields=_id,status --out=events.csvgo deeper
Recall the two outputs: BSON plus metadata for mongodump, JSON or CSV for mongoexport. Say out loud that index definitions travel in a dump and index data does not.
Explain the restore consequence: documents are inserted and indexes rebuilt. Name what a full run skips, starting with the local database, and what view metadata means for a view's documents.
Show you check a dump against what the deployment really holds. Encrypted collections, views and system collections all behave differently, and a dump that looks complete can restore into something that isn't.
Frame the two tools as different contracts: one promises a way back into MongoDB, the other promises readable output. Keep exports out of recovery plans so nobody mistakes a data feed for a backup.
## What `mongodump` writes `mongodump` is a command-line tool from the MongoDB Database Tools. It connects to a running `mongod` or `mongos`, reads the data through the server and writes a **binary export**. It doesn't copy the storage engine's files. A directory dump contains: - A root directory, `dump/` unless `--out` names another. - One subdirectory per database. - One `.bson` file per collection, holding its documents. - One `.metadata.json` file per collection, holding its options, UUID and index definitions. - A metadata file for each view, with no `.bson` file beside it. - An `oplog.bson` file at the top, only when the run used `--oplog`. `--archive` puts all of that into one file or stream, and `--gzip` compresses the output. When the source uses WiredTiger, `mongodump` writes the data uncompressed, so a dump isn't the size of the compressed files on disk. Running it again over the same folder overwrites what is there. A full run also includes user and role definitions. `mongorestore` loads the result by inserting the documents and then **rebuilds the indexes** from the recorded definitions. That is what makes a dump **logical**: it holds the data and the instructions for rebuilding everything else, not the physical structures. ## What a dump leaves behind The list is worth knowing, because each item is something a restored deployment won't have: - **Index data.** Only definitions travel, so every index is built again on restore. - **The `local` database.** A full run skips it, and you get it only by naming it with `--db=local`. - **Most of `config`.** A full run keeps only `chunks`, `collections`, `databases`, `settings`, `shards`, `tags` and `version`. - **`admin.system.keys`,** which the tool always skips. - **`system.*` collections in ordinary databases,** except `system.js`. - **View contents.** A view keeps only its metadata, unless `--viewsAsCollections` dumps its documents as a collection instead. - **Encrypted collections.** The docs say you can't use `mongodump` on a collection that uses Queryable Encryption or client-side field level encryption. - **Anything written later.** A dump describes the past, and without `--oplog` it may not describe one single moment. ## What `mongoexport` writes `mongoexport` produces text for people and other systems: - It needs a collection, and it fails with `must specify a collection` otherwise. - `--type` defaults to `json`. `csv` also needs `--fields` or `--fieldFile`. - `--jsonFormat` defaults to `relaxed`. `canonical` keeps BSON types that relaxed mode can blur. - `mongoimport` is the matching way back in. The tools docs are direct about it: `mongoimport` and `mongoexport` aren't intended for backing up and restoring data, or for converting data to another format and back to a cluster. The reason follows from the output. Each file holds one collection's documents, with no index definitions, collection options, users or roles, so rebuilding a deployment from a set of exports means rebuilding everything else by hand. ## Side by side | | `mongodump` | `mongoexport` | |---|---|---| | Output | BSON files, or one archive | JSON or CSV | | Scope | Server, database, collection or query | One collection per run | | Index definitions and options | Recorded in metadata | Not recorded | | Users and roles | Included in a full run | Not included | | Way back in | `mongorestore` | `mongoimport` | ## Which one to reach for 1. To be able to bring a small deployment back, use `mongodump`. The manual describes the pair as useful for backing up small deployments, and points larger ones at snapshots. 2. To give an analyst a CSV, or feed one collection to another system, use `mongoexport`. 3. To move data between MongoDB deployments, use `mongodump` and `mongorestore`, since a trip through JSON is the case the docs warn against. ## Before you rely on a dump - **Version match.** Restore into the same major version or feature compatibility version as the source, and use the same tools version for `mongodump` and `mongorestore`. The Database Tools version separately from the server. - **File names.** On a case-insensitive filesystem, collections whose names differ only by case overwrite each other in a directory dump, so the docs say to use `--archive` there. - **A restore test.** The guidance is to verify a backup by restoring it to a test deployment, because a dump that was written isn't yet a dump that was read back.
- Why can a mongodump be smaller than a copy of the data files and still be slower to restore?The manual notes that copied files are larger because they include indexes plus storage padding and fragmentation. WiredTiger's block compression can narrow that gap, because `mongodump` writes uncompressed data unless you add `--gzip`. Restore is slower because the server has to insert every document and then build every index, where restored files only need to be put in place and started.
- If mongoexport's --jsonFormat=canonical keeps BSON types, does that make it a backup?No. Canonical mode fixes one problem, the type detail that default relaxed mode can blur. The output still covers one collection per run and carries no index definitions, collection options, users or roles, and the tools docs still say `mongoexport` isn't intended for backing up and restoring data.
- A mongodump file contains the index data, so a restore needs no index builds.
- mongoexport to JSON makes a fine nightly backup of a whole deployment.
- A full mongodump includes the local database, so it carries the whole oplog.
- mongodump copies the WiredTiger data files as they sit on disk.
- Users and roles never travel in a mongodump, so they must be recreated by hand.