Skip to content

Instantly share code, notes, and snippets.

@phanletrunghieu
Last active July 28, 2018 11:17
Show Gist options
  • Save phanletrunghieu/5c68c6798df8bb9784f7a59dc1941a65 to your computer and use it in GitHub Desktop.
Save phanletrunghieu/5c68c6798df8bb9784f7a59dc1941a65 to your computer and use it in GitHub Desktop.
import/export all collection in mongodb
#!/bin/bash
DB=$1
COLLECTIONS=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | sed -e 's/[,\["]/ /g;s/ */ /g')
COLLECTIONS=${COLLECTIONS#\[}
COLLECTIONS=${COLLECTIONS%\]}
for collection in $COLLECTIONS; do
echo "Exporting $DB/$collection ..."
mongoexport -d $DB -c $collection -o $collection.json
done
DB=$1
COLLECTIONS=$(ls | grep json)
for collection in $COLLECTIONS; do
echo "Exporting $DB/$collection ..."
mongoimport -d $DB $collection
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment