Guest User

Untitled

a guest
Jul 14th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. OIFS=$IFS;
  2. IFS=",";
  3.  
  4. # fill in your details here
  5. dbname=DBNAME
  6. user=USERNAME
  7. pass=PASSWORD
  8. host=HOSTNAME:PORT
  9.  
  10. # first get all collections in the database
  11. collections=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();db.getCollectionNames();"`;
  12. collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();"`;
  13. collectionArray=($collections);
  14.  
  15. # for each collection
  16. for ((i=0; i<${#collectionArray[@]}; ++i));
  17. do
  18. echo 'exporting collection' ${collectionArray[$i]}
  19. # get comma separated list of keys. do this by peeking into the first document in the collection and get his set of keys
  20. keys=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys;" --quiet`;
  21. # now use mongoexport with the set of keys to export the collection to csv
  22. mongoexport --host $host -u $user -p $pass -d $dbname -c ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv;
  23. done
  24.  
  25. IFS=$OIFS;
Add Comment
Please, Sign In to add comment