Skip to main content

Useful MongoDB commands

To export JSON from MongoDB Collection
 mongoexport -h replicaSet/primaryHost,secondaryHost -u username -p pwd --db dbName -c collectionName --quiet > collectionExport.json

To export BSON from MongoDB Collection
mongodump -h replicaSet/primaryHost,secondaryHost -u username -p pwd --db dbName -c collectionName -o - > collectionDump.bson

To export some fields in csv from MongoDB Collection
mongoexport --host primaryHost --db dbName --collection collectionName --fields 'field1,field2,field3' --out collection.csv --csv -u userName -p

To run javascript on MongoDB
mongo replicaSet/primaryHost,secondaryHost/dbName -u userName mongoScript.js -p

To restore bson in MongoDB
mongorestore --host primaryHost --db dbName --collection collectionName -u userName -p --drop  collectionName.bson

Comments

Post a Comment