Export and Import MongoDB Database and collections

Using Mongo restore and dump:

Dump MongoDB Database collections:

mongodump --host <host> --port <port> --username <user> --password "<pass>" --out <path>/mongodump-<date>

Restore MongoDB Database collections:

mongorestore --host <host> --port <port> --username <user> --password "<pass>" <path>/mongodump-<date>

Using Mongo export and import:

Export MongoDB Database collections:

mongoexport --db <db-name> --collection <collection-name> --out <name>.json

Import MongoDB Database collections:

mongoimport --db <db-name> --collection <collection-name> --file <name>.json

Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.