Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhnt/64380ecd69efd129b9d239fd54523c16 to your computer and use it in GitHub Desktop.
Save anhnt/64380ecd69efd129b9d239fd54523c16 to your computer and use it in GitHub Desktop.
Mongodb restore and dump commands via json, bson and csv

NOTE -

  • Remove -h option if you are doing operation on same machine
  • Remove -u , -p option if your database don't have username and password

Binary

Import database

mongorestore -h IP:port -d DB_Name -u user_name -p password <input db directory>

Export database

mongodump -h IP:port -d DB_Name -u user_name -p password -o <output directory>

Import collection

mongorestore -h IP:port -d DB_Name -u user_name -p password <input .bson file>

Export collection

mongodump -h IP:port -d DB_Name -c collection_name -u user_name -p password -o <output directory>

JSON

Import collection

mongoimport -h IP:port -d DB_Name -c collection_name -u user_name -p password --file <input file>

Export collection

mongoexport -h IP:port -d DB_Name -c collection_name -u user_name -p password -o <output file>

CSV

Import collection

mongoimport -h IP:port -d DB_Name -c collection_name -u user_name -p password --file <input .csv file> --type csv --headerline

Export collection

mongoexport -h IP:port -d DB_Name -c collection_name -u user_name -p password -o <output .csv file> --csv -f <comma-separated list of field names>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment