Last active
July 28, 2018 11:17
-
-
Save phanletrunghieu/5c68c6798df8bb9784f7a59dc1941a65 to your computer and use it in GitHub Desktop.
import/export all collection in mongodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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