Skip to content

Instantly share code, notes, and snippets.

@juanem1
Last active July 16, 2017 08:02
Show Gist options
  • Save juanem1/9ab5d05776216b522062851074ae9a32 to your computer and use it in GitHub Desktop.
Save juanem1/9ab5d05776216b522062851074ae9a32 to your computer and use it in GitHub Desktop.
mongoimport / mongoexport MongoDB
mongoimport
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
-u [ --username ] arg username
-p [ --password ] arg password
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--upsert insert or update objects that already exist
--jsonArray
// Import all documents
mongoimport -d dbName -c collectionName --file dump.json
// Import all documents and update if exist
mongoimport -d dbName -c collectionName --file dump.json --upsert
// Import to remote
mongoimport -h mypage.com:27000 -d dbName -c collectionName -u username -p password --file dump.json
// Export --------------------------------------------------------------------------------
mongoexport
options:
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
-u [ --username ] arg username
-p [ --password ] arg password
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-q [ --query ] arg query filter, as a JSON string
-o [ --out ] arg output file; if not specified, stdout is used
// Export all documents
mongoexport -d dbName -c collectionName -o dump.json
// Export all documents with fields email and phone only.
mongoexport -d dbName -c collectionName -f "email,phone" -o dump.json
// Export all documents with a search query
mongoexport -d dbName -c collectionName -f "email,phone" -q '{spent:{$gt:1000}}' -o dump.json
// Export remote DB
mongoexport -h mypage.com:27000 -d dbName -c collectionName -u username -p password -o dump.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment