Kyaw Min Thu L Kyaw Min Thu L - 6 months ago 34
Bash Question

How to get exact list of collections form mongo database in shell script

I would like to get the collection names list from mongo database.
So, I use the following command in shell script :


mongo $dbName --eval "db.getCollectionNames()"

The output result of this command is

"MongoDB shell version: 2.2.0 connecting to: cm_v2 col1,col2,col3,col4"

I would like to get only the collections name such as : col,col2,col3,col4.
So, how should I delete the output like version from result.


use --quiet flag

collections=mongo $dbName --quiet --eval "db.getCollectionNames()"