Last active
February 14, 2021 04:25
-
-
Save jackrobertscott/77df160adcb7b2f75f5b3648d5e6b8ee to your computer and use it in GitHub Desktop.
Export & import all MongoDB collections using JSON files.
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 | |
| # Usage: ./mongodb-json-export.sh dbName | |
| DB=$1 | |
| COLLECTIONS=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | tr -d '\[\]\"[:space:]' | tr ',' ' ') | |
| mkdir json | |
| for collection in $COLLECTIONS; do | |
| echo "Exporting $DB/$collection ..." | |
| mongoexport -d $DB -c $collection -o json/$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
| #!/bin/bash | |
| # Usage: ./mongodb-json-import.sh dbName jsonFilesFolderLocation | |
| DB=$1 | |
| FOLDER=$2 | |
| WORKDIR=$(pwd) | |
| cd $FOLDER | |
| COLLECTIONS=$(ls -1 *.json | sed 's/.json$//') | |
| for collection in $COLLECTIONS; do | |
| echo "Importing $DB/$collection ..." | |
| mongoimport -d $DB -c $collection --file $collection.json | |
| done | |
| cd $WORKDIR |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original version: https://gist.github.com/thbkrkr/7617edf1f540a04f482a