Skip to content

Instantly share code, notes, and snippets.

@nas
Last active January 2, 2016 19:09
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 nas/8348774 to your computer and use it in GitHub Desktop.
Save nas/8348774 to your computer and use it in GitHub Desktop.
importing and exporting mongo db/collection in various formats
Binary
======
* Import database
mongorestore -h host:port -d experimental2 -u <user> -p <password> <input db directory>
* Export database
mongodump -h host:port -d experimental2 -u <user> -p <password> -o <output directory>
* Import collection
mongorestore -h host:port -d experimental2 -u <user> -p <password> <input .bson file>
* Export collection
mongodump -h host:port -d experimental2 -c <collection> -u <user> -p <password> -q <query else will export full collection> -o <output directory>
JSON
====
* Import collection
mongoimport -h host:port -d experimental2 -c <collection> -u <user> -p <password> --file <input file>
* Export collection
mongoexport -h host:port -d experimental2 -c <collection> -u <user> -p <password> -q <query else will export full collection> -o <output file>
CSV
===
* Import collection
mongoimport -h host:port -d experimental2 -c <collection> -u <user> -p <password> --file <input .csv file> --type csv --headerline
* Export collection
mongoexport -h host:port -d experimental2 -c <collection> -u <user> -p <password> -q <query else will export full collection> -o <output .csv file> --csv -f <comma-separated list of field names>
Date query e.g.
* var end = new Date("01/23/2014 00:00:00");end.getTime() #=>1390435200000
* -q {created_at: {\$lt: new Date(1390435200000) }}"
* db.coll.count({created_at: {$lt: new Date(1390435200000) }});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment