Skip to content
Latchkey

mongodump: Usage, Options & Common CI Errors

mongodump writes a binary BSON backup of MongoDB data.

mongodump produces the BSON dumps mongorestore reloads in CI. Like mongosh, its failures cluster around the connection URI and authSource, plus the archive-vs-directory output choice.

What it does

mongodump connects to a MongoDB server and writes BSON files (plus metadata) for a database or collection into a dump directory, or into a single --archive file. It is the standard logical backup tool, paired with mongorestore.

Common usage

Terminal
mongodump --uri="mongodb://localhost:27017/app" --out=dump
mongodump --uri="mongodb://user:pass@localhost:27017/app?authSource=admin" --gzip --out=dump
mongodump --host 127.0.0.1 --port 27017 --db app --collection users --out=dump
mongodump --uri="mongodb://localhost:27017/app" --archive=app.archive --gzip

Options

FlagWhat it does
--uri="..."Connection string
--db / --collectionLimit to one database / collection
--out <dir>Write BSON files into a directory
--archive[=file]Write a single archive (file or stdout)
--gzipCompress the output
--query '{...}'Dump only matching documents

Common errors in CI

Failed: ... Authentication failed. - supply the right credentials and authSource in the URI (admin users need ?authSource=admin). "could not connect to server ... connection() : dial tcp 127.0.0.1:27017: connect: connection refused" means the server is not up; wait on mongosh ping first. If you write --archive (one file), mongorestore must read with --archive too; a directory dump restores from the directory.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →