Skip to content

Instantly share code, notes, and snippets.

@sillyfellow
Last active July 2, 2018 13:51
Show Gist options
  • Save sillyfellow/caca388380383630be44a4d3f4aaa5b7 to your computer and use it in GitHub Desktop.
Save sillyfellow/caca388380383630be44a4d3f4aaa5b7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# list and export all collections to json using mongoexport, from a specific mongo db
[ $# -ne 1 ] && echo "Usage: $0 <db-name>" && exit 1
DB=${1}
# fetch the list of all collections
COLLECTIONS=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | grep \" | tr -d '\[\]\"[:space:]' | tr ',' ' ')
# create a folder for all the output (we don't want to litter cwd)
mkdir -p ${DB}
# loop through them, create the json output
for collection in ${COLLECTIONS}; do
echo "Exporting ${DB}/${collection} ..."
mongoexport -d ${DB} -c ${collection} -o ${DB}/${collection}.json
done
# Credits to: https://gist.github.com/thbkrkr/7617edf1f540a04f482a
# inspired from there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment