mongodump is the MongoDB command-line utility that reads documents through a normal client connection and writes them to BSON files, together with JSON metadata describing collection options and indexes. In backup & recovery for MongoDB databases, it produces a logical backup that its companion tool, mongorestore, can load into the same or a different deployment.
Because it works at the level of documents rather than data files, mongodump is portable across storage engines, server versions and platforms within supported ranges. That flexibility makes it well suited to small and medium datasets, migrations and selective restores, while large production clusters usually rely on physical or snapshot-based methods for their primary backups.
How mongodump reads and writes data
mongodump connects as an ordinary client, enumerates databases and collections, and issues find queries that stream every document into output files. Each collection becomes a .bson file holding raw documents and a .metadata.json file holding options, index definitions and the collection UUID. The tool is distributed in the MongoDB Database Tools package, versioned separately from the server since version 4.4.
By default the dump is not a point-in-time image. Collections are read one after another while writes continue, so related data can reflect different moments. On a replica set, the --oplog option addresses this by also capturing oplog entries written during the dump, which mongorestore replays with --oplogReplay to reach a consistent state as of the moment the dump finished.
Indexes are stored only as definitions, not as data. During restore, mongorestore rebuilds every index from scratch, which often makes index builds the slowest phase of recovering a large collection.
Key options and output formats
A small set of options controls scope, consistency and output form. The table below lists those used most often in practice.
| Option | Purpose | Practical note |
|---|---|---|
| –uri | Connection string with hosts and credentials | Prefer a config file to keep passwords out of shell history |
| –db / –collection | Limit the dump to one database or collection | Enables targeted backups and partial restores |
| –query | Export only documents matching a filter | Filter in extended JSON format |
| –oplog | Capture writes made during the dump | Replica sets only; applies to full-instance dumps |
| –gzip | Compress each output file | Typically shrinks output several times for text-heavy data |
| –archive | Write a single archive file or stream | Allows piping directly to storage or another server |
| –readPreference | Choose which member to read from | Secondary reads reduce load on the primary |
| –numParallelCollections | Number of collections dumped concurrently | Default is 4; higher values add server load |
The archive format combined with gzip is popular for automation because a single compressed stream can be written to object storage or piped straight into mongorestore on another host, avoiding intermediate disk space.
How mongodump compares with other backup methods
mongodump is one of several MongoDB backup approaches, and the right choice depends mainly on data size and recovery time objectives. It plays a role similar to pg_dump and mysqldump in relational systems.
| Method | Backup type | Point-in-time capability | Best fit |
|---|---|---|---|
| mongodump | Logical, document level | Only to dump end with –oplog | Small datasets, migrations, partial copies |
| Filesystem snapshot | Physical, block level | Snapshot moment with journaling | Large single-server or replica set deployments |
| Managed cloud backup | Physical snapshots plus oplog | Continuous point-in-time recovery | Production clusters needing low RPO |
| Hidden or delayed secondary | Live replica copy | Fixed delay window | Protection against recent operator error |
| mongoexport | Logical, JSON or CSV | None | Data exchange, not full-fidelity backup |
Unlike mongoexport, mongodump preserves full BSON type fidelity, including dates, decimals and binary data. Unlike physical backups, its restore time grows with data volume and index count, which is the main reason it rarely serves as the sole protection for multi-terabyte systems.
Using mongodump safely in production
A dump reads every document, so it pulls the working set through the cache and can evict hot data. Running it against a secondary member with an appropriate read preference limits the impact on application traffic, and scheduling it outside peak hours further reduces risk.
Sharded clusters need particular care. mongodump can read through mongos, but the result is not consistent across shards, and chunk migrations during the dump can cause missing or duplicate documents. Recommended practice is to stop the balancer for the duration, or to use snapshot-based cluster backups that coordinate all shards.
The following decision table matches common situations to a sensible approach.
| Situation | Recommended approach | Reason |
|---|---|---|
| Database under 100 GB on a replica set | mongodump with –oplog and –gzip | Simple, portable and consistent at dump end |
| Copying one collection to staging | mongodump with –collection and –query | Exports only what is needed |
| Multi-terabyte production cluster | Snapshot or managed backup | Restore from dump would take too long |
| Upgrading across major versions | mongodump and mongorestore | Logical format avoids data file incompatibility |
| Sharded cluster needing consistency | Coordinated cluster backup | mongodump cannot guarantee cross-shard consistency |
Limitations and common mistakes
The most damaging mistake is never testing a restore. A dump that has not been restored and verified is an assumption, not a backup, and problems such as missing permissions, truncated archives or incompatible tool versions surface only at recovery time.
- Running without –oplog on a busy replica set and assuming the dump is consistent.
- Dumping with a user that lacks the backup role, which silently skips some system data.
- Storing dumps on the same host as the database, so one failure destroys both.
- Forgetting that users and roles live in the admin database and need to be included for a full restore.
- Using mongodump on very large datasets without measuring restore duration against the recovery time objective.
Encryption is another gap. mongodump writes plain BSON unless the output is encrypted separately, for example by the storage layer or an encrypting pipe. Dumps of production data therefore require the same access controls as the database itself.
Understanding it simply
mongodump is like photocopying every page of a filing cabinet into a neat folder you can take anywhere. The copy does not depend on the brand of cabinet, so it can be placed into a different one later.
The catch is time. Copying page by page is fine for a small office, but for a huge archive it is slow, and putting everything back with mongorestore takes even longer because the index cards must be rewritten. That is why large systems combine it with faster whole-disk copies.
Applied in practice
A small software company runs a three-member MongoDB replica set holding about 40 GB of customer project data. Its operations engineer schedules a nightly job that runs mongodump against a secondary with --oplog, --gzip and --archive, streaming the result to encrypted object storage in another region. The compressed archive is about 9 GB and the dump finishes in roughly 25 minutes.
Once a month, an automated job restores the latest archive into an isolated test server with mongorestore and --oplogReplay, then compares document counts per collection. The first test reveals that rebuilding indexes takes longer than the dump itself, bringing total restore time to about 70 minutes.
When a faulty migration script later deletes a field across 120,000 documents, the team restores the affected collection from the previous night into a separate database and copies the field back. Recovery takes under an hour with no downtime for the rest of the application.