How can I list all collections in the MongoDB shell?

789

In the MongoDB shell, how do I list all collections for the current database that I'm using?

This question is tagged with mongodb mongo-shell

~ Asked on 2012-01-14 22:52:03

The Best Answer is


1213

You can do...

JavaScript (shell):

db.getCollectionNames()

Node.js:

db.listCollections()

Non-JavaScript (shell only):

show collections

The reason I call that non-JavaScript is because:

$ 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"
]

If you really want that sweet, sweet show collections output, you can:

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info

~ Answered on 2012-01-14 22:57:47


427

> show collections

will list all the collections in the currently selected DB, as stated in the command line help (help).

~ Answered on 2012-01-14 22:56:24


Most Viewed Questions: