mongodump

On this page 7

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.

OptionPurposePractical note
–uriConnection string with hosts and credentialsPrefer a config file to keep passwords out of shell history
–db / –collectionLimit the dump to one database or collectionEnables targeted backups and partial restores
–queryExport only documents matching a filterFilter in extended JSON format
–oplogCapture writes made during the dumpReplica sets only; applies to full-instance dumps
–gzipCompress each output fileTypically shrinks output several times for text-heavy data
–archiveWrite a single archive file or streamAllows piping directly to storage or another server
–readPreferenceChoose which member to read fromSecondary reads reduce load on the primary
–numParallelCollectionsNumber of collections dumped concurrentlyDefault is 4; higher values add server load
Frequently used mongodump options

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.

MethodBackup typePoint-in-time capabilityBest fit
mongodumpLogical, document levelOnly to dump end with –oplogSmall datasets, migrations, partial copies
Filesystem snapshotPhysical, block levelSnapshot moment with journalingLarge single-server or replica set deployments
Managed cloud backupPhysical snapshots plus oplogContinuous point-in-time recoveryProduction clusters needing low RPO
Hidden or delayed secondaryLive replica copyFixed delay windowProtection against recent operator error
mongoexportLogical, JSON or CSVNoneData exchange, not full-fidelity backup
mongodump compared with alternative approaches

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.

SituationRecommended approachReason
Database under 100 GB on a replica setmongodump with –oplog and –gzipSimple, portable and consistent at dump end
Copying one collection to stagingmongodump with –collection and –queryExports only what is needed
Multi-terabyte production clusterSnapshot or managed backupRestore from dump would take too long
Upgrading across major versionsmongodump and mongorestoreLogical format avoids data file incompatibility
Sharded cluster needing consistencyCoordinated cluster backupmongodump cannot guarantee cross-shard consistency
Choosing when to use mongodump

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.