How to list all collections in the mongo shell ?
How to list all collections in the mongo shell ?
Just we can try to do it
JS (shell):
db.getCollectionNames()
node.js:
db.listCollections()
non-JS (only for shell):
show collections
The following below code can be a reason for non-JS:
$ mongo prodmongo/app --eval "show collections" MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5 $ mongo prodmongo/app --eval "db.getCollectionNames()" MongoDB shell version: 3.2.10 connecting to: prodmongo/app [ "Profiles", "Unit_Info" ]
show collections result, can be:
$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')" MongoDB shell version: 3.2.10 connecting to: prodmongo/app Profiles Unit_Info
> show collections
Here we will show all the collections in the currently selected DB, as stated in the command line help (help)
how to list all the collection in the database ?
There are three methods
show collections
show collections
db.getCollectionNames()
To show entire database:
show dbs
Use specific database:
use databasename
To show entire collections
show collections
Result:
collection1 collection2 system.indexes
or else
show tables
Result:
collection1 collection2 system.indexes
or else
db.getCollectionNames()
Result:
[ "collection1", "collection2", "system.indexes" ]
Use specific collection
use collectionname