mongorestore: Usage, Options & Common CI Errors
mongorestore reloads a mongodump BSON backup into a MongoDB server.
mongorestore is the counterpart to mongodump for seeding CI databases. The recurring failures are duplicate-key errors on a non-empty target and archive/gzip flags that must mirror the dump.
What it does
mongorestore reads a BSON dump (directory or --archive) and inserts the documents and indexes back into a MongoDB deployment. With --drop it clears each collection before loading so a re-run starts clean.
Common usage
mongorestore --uri="mongodb://localhost:27017/app" dump/app
mongorestore --uri="mongodb://localhost:27017" --drop dump
mongorestore --uri="mongodb://localhost:27017/app" --archive=app.archive --gzip
mongorestore --uri="mongodb://localhost:27017" --nsInclude='app.*' dumpOptions
| Flag | What it does |
|---|---|
| --uri="..." | Connection string |
| --drop | Drop each collection before restoring it |
| --archive[=file] | Read from an archive file (or stdin) |
| --gzip | Input is gzip-compressed |
| --nsInclude / --nsExclude | Restore only matching namespaces |
| --numParallelCollections | Restore N collections at once |
Common errors in CI
E11000 duplicate key error collection - you restored into a non-empty collection; pass --drop for an idempotent re-run. "Failed: archive parser ... unexpected EOF" means the archive is truncated or you forgot --gzip on a gzipped archive (the flags must match the dump). Mismatched --archive vs directory input is the other common error: a directory dump is restored by pointing at the directory, an --archive dump needs --archive.