Skip to content

Commit b0c58a8

Browse files
author
adrianbartyczak
committed
Add action "drop-collection" to script mongodbutils
1 parent 868a98b commit b0c58a8

File tree

1 file changed

+57
-44
lines changed
  • functions_scripts/application_management/database

1 file changed

+57
-44
lines changed

functions_scripts/application_management/database/mongodbutils

+57-44
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ fi
2222

2323
while getopts u:p: OPT; do
2424
case "${OPT}" in
25-
u) AUTH_OPTS="-u ${OPTARG}"
25+
u) MONGO_AUTH_OPTS="-u ${OPTARG}"
2626
;;
27-
p) AUTH_OPTS+=" -p ${OPTARG}"
27+
p) MONGO_AUTH_OPTS+=" -p ${OPTARG}"
2828
;;
2929
esac
3030
done
@@ -36,17 +36,17 @@ shift 2
3636

3737
case "${ACTION}" in
3838
# Action:
39-
# import
39+
# import-collection
4040
# Description:
4141
# Import data to a collection from a JSON file.
4242
# Usage:
43-
# ... import <json_file_name> [collection_name]
44-
# collection name: collection name in the database; default is
45-
# the JSON file name without the extension
43+
# ... import-collection <json_file_name> [collection_name]
44+
# [collection_name]: name of collection to import; default is
45+
# JSON file name
4646
# Note:
4747
# If needing to import multiple MongoDB documents in a single JSON array,
4848
# append option "--jsonArray" to the end of the mongoimport command.
49-
'import')
49+
'import-collection')
5050
[ "$#" -eq 0 ] && \
5151
echo "mongodbutils: no directory specified" 1>&2 && \
5252
exit 1
@@ -57,19 +57,19 @@ case "${ACTION}" in
5757
if [ "$#" -ge 2 ]; then
5858
opts+="--collection ${2}"
5959
fi
60-
mongoimport --db "${DB_NAME}" ${AUTH_OPTS} --file "${1}" ${opts}
60+
mongoimport --db "${DB_NAME}" ${MONGO_AUTH_OPTS} --file "${1}" ${opts}
6161
;;
6262

6363
# Action:
64-
# export
64+
# export-collection
6565
# Description:
6666
# Export data from a collection to a JSON file.
6767
# Usage:
68-
# ... export <collection_name> [directory]
69-
# collection name: collection name in the database
70-
# directory: directory to output JSON file to; default is
71-
# current
72-
'export')
68+
# ... export-collection <collection_name> [directory]
69+
# <collection_name>: name of the collection to export
70+
# [directory]: directory to output JSON file to; default
71+
# is current
72+
'export-collection')
7373
if [ "$#" -ge 1 ]; then
7474
collName="${1}"
7575
opts+="--collection ${collName}"
@@ -80,26 +80,40 @@ case "${ACTION}" in
8080
opts+=" --out ${collName}.json"
8181
fi
8282
fi
83-
mongoexport --db "${DB_NAME}" ${AUTH_OPTS} ${opts} --jsonArray
83+
mongoexport --db "${DB_NAME}" ${MONGO_AUTH_OPTS} ${opts} --jsonArray
8484
;;
8585

8686
# Action:
87-
# drop
87+
# drop-collection
88+
# Description:
89+
# Drop a collection
90+
# Usage:
91+
# ... drop-collection <collection_name>
92+
# <collection_name>: the name of the collection to drop
93+
'drop-collection')
94+
[ "$#" -eq 0 ] && \
95+
echo "mongodbutils: no collection specified" 1>&2 && \
96+
exit 1
97+
mongo "${DB_NAME}" --eval "db.${1}.drop()"
98+
;;
99+
100+
# Action:
101+
# drop-database
88102
# Description:
89103
# Drop the database.
90104
# Usage:
91-
# ... drop
92-
'drop')
105+
# ... drop-database
106+
'drop-database')
93107
mongo "${DB_NAME}" --eval 'db.dropDatabase()'
94108
;;
95109

96110
# Action:
97-
# drop_all
111+
# drop-all-databases
98112
# Description:
99113
# Drop all databases.
100114
# Usage:
101-
# ... drop_all
102-
'drop_all')
115+
# ... drop-all-databases
116+
'drop-all-databases')
103117
/usr/bin/mongo <<-EOF
104118
db.getMongo().getDBNames().forEach(function(database) {
105119
if (database != 'local' && database != 'admin') {
@@ -108,23 +122,7 @@ case "${ACTION}" in
108122
print('dropped database ' + db.getName());
109123
}
110124
})
111-
EOF
112-
;;
113-
114-
# Action:
115-
# restore
116-
# Description:
117-
# Restore data from one or more BSON files.
118-
# Usage:
119-
# ... restore <bson_file_or_directory_containing_bson_files>
120-
'restore')
121-
[ -z "${1}" ] && \
122-
echo "mongodbutils: no directory specified" 1>&2 && \
123-
exit 1
124-
[ ! -d "${1}" ] && \
125-
echo "mongodbutils: directory \"${1}\" does not exist" 1>&2 && \
126-
exit 1
127-
mongorestore ${AUTH_OPTS} "${1}"
125+
EOF
128126
;;
129127
130128
# Action:
@@ -133,19 +131,34 @@ case "${ACTION}" in
133131
# Dump the database to BSON files.
134132
# Usage:
135133
# ... dump [collection_name] [directory]
136-
# collection name: collection name in the database; default is
137-
# all
138-
# directory: directory to output BSON files to; default is
139-
# current
134+
# [collection_name]: name of collection to dump; if no name is
135+
# specified, all collections will be dumped
136+
# [directory]: directory to output BSON files to; default
137+
# is current
140138
'dump')
141139
if [ "$#" -ge 1 ]; then
142140
opts+="--collection ${1}"
143-
144141
if [ "$#" -ge 2 ]; then
145142
opts=" --out ${2}"
146143
fi
147144
fi
148-
mongodump --db "${DB_NAME}" ${AUTH_OPTS} ${opts}
145+
mongodump --db "${DB_NAME}" ${MONGO_AUTH_OPTS} ${opts}
146+
;;
147+
148+
# Action:
149+
# restore
150+
# Description:
151+
# Restore data from one or more BSON files.
152+
# Usage:
153+
# ... restore <bson_file_or_directory_containing_bson_files>
154+
'restore')
155+
[ -z "${1}" ] && \
156+
echo "mongodbutils: no directory specified" 1>&2 && \
157+
exit 1
158+
[ ! -d "${1}" ] && \
159+
echo "mongodbutils: directory \"${1}\" does not exist" 1>&2 && \
160+
exit 1
161+
mongorestore ${MONGO_AUTH_OPTS} "${1}"
149162
;;
150163
151164
*)

0 commit comments

Comments
 (0)