Created
February 27, 2014 01:12
-
-
Save brain-zhang/9242308 to your computer and use it in GitHub Desktop.
cascade remove mongodb's collections
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 | |
| # remove mongodb's collections with command "mongn rm app*" | |
| # Usage `mongorm <-c collections*> <-d dababase>` | |
| E_BADARGS=85 | |
| E_NOFILE=86 | |
| DATABASE='' | |
| COLLECTIONS='' | |
| while getopts "c:d:h" arg | |
| do | |
| case $arg in | |
| c) | |
| echo "-c $OPTARG" | |
| COLLECTIONS=$OPTARG | |
| ;; | |
| d) | |
| echo "-d $OPTARG" | |
| DATABASE=$OPTARG | |
| ;; | |
| h) | |
| echo "mongorm <-d database> <-c collections>" | |
| echo "for exp:" | |
| echo " mongorm -d bpc -c app*" | |
| ;; | |
| ?) | |
| echo "unkonw argument" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| if [[ -z "$DATABASE" ]] | |
| # Correct number of arguments passed to script? | |
| then | |
| echo 'Usage: -d database' | |
| echo 'please do mongorm.sh -h to get more info' | |
| exit $E_BADARGS | |
| fi | |
| if [[ -z "$COLLECTIONS" ]] | |
| # Correct number of arguments passed to script? | |
| then | |
| echo 'Usage: -c collections' | |
| echo 'please do mongorm.sh -h to get more info' | |
| exit $E_BADARGS | |
| fi | |
| mongojs_rm_function=`cat << EOF | |
| db.getCollectionNames().forEach(function(c) { | |
| if(c.match("$COLLECTIONS")) { | |
| db.getCollection(c).drop(); | |
| } | |
| }); | |
| EOF | |
| ` | |
| echo $mongojs_rm_function>remove.js | |
| mongo 127.0.0.1:27017/$DATABASE remove.js | |
| rm -rf remove.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment