How can I list all collections in the MongoDB shell?

How can I list all collections in the MongoDB shell?

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
> show collections

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

How can I list all collections in the MongoDB shell?

How do I list all collections for the current database that Im using?

Three methods

  • show collections
  • show tables
  • db.getCollectionNames()

To list all databases:

show dbs

To enter or use a given database:

use databasename

To list all collections:

show collections

Output:

collection1
collection2
system.indexes

(or)

show tables

Output:

collection1
collection2
system.indexes

(or)

db.getCollectionNames()

Output:

[ collection1, collection2, system.indexes ]

To enter or use given collection

use collectionname

Leave a Reply

Your email address will not be published.