$linuxjunkies
>

mongorestore(1)

Restore a MongoDB database from a dump created by mongodump.

UbuntuDebianFedoraArch

Synopsis

mongorestore [OPTION]... [DIRECTORY]

Description

Reads data from a backup directory created by mongodump and restores it into a running MongoDB instance. By default, mongorestore reads from the 'dump' directory in the current working directory.

It can restore an entire database, specific collections, or merge data into existing collections. Connection details, authentication, and restore behavior are controlled via command-line flags.

Common options

FlagWhat it does
--uri=<mongodb-uri>MongoDB connection URI; alternative to --host, --port, --username, --password
--host=<hostname[:port]>Hostname and optional port of MongoDB server (default: localhost:27017)
--port=<port>Port number for MongoDB server (default: 27017)
--username=<username>Username for authentication; requires --authenticationDatabase
--password=<password>Password for authentication (use --password with no argument to prompt)
--authenticationDatabase=<database>Database to authenticate against (typically 'admin')
--db=<database>Restore to specified database instead of the original database name
--collection=<collection>Restore only the specified collection
--dropDrop collections before restoring; removes existing data
--dryRunValidate backup files without actually restoring data
--stopOnErrorStop restore on first error instead of continuing
--nsInclude=<namespace>Include only specified namespaces; supports wildcards (e.g., 'mydb.*')

Examples

Restore all databases from the 'dump' directory to localhost:27017

mongorestore dump/

Restore to a remote MongoDB server

mongorestore --host mongodb.example.com --port 27017 dump/

Restore with authentication; prompts for password interactively

mongorestore --username admin --password --authenticationDatabase admin dump/

Restore the 'myapp' database from backup as 'production' database

mongorestore --db production dump/myapp/

Restore only the 'users' collection, dropping existing data first

mongorestore --collection users --drop dump/mydb/users.bson

Restore only collections from 'mydb', dropping existing ones

mongorestore --nsInclude 'mydb.*' --drop dump/

Validate the backup structure without performing the restore

mongorestore --dryRun dump/

Restore to MongoDB Atlas using a connection URI

mongorestore --uri 'mongodb+srv://user:[email protected]/admin' dump/

Related commands