898

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

Braiam
  • 1
  • 11
  • 47
  • 78
coffee-grinder
  • 26,940
  • 19
  • 56
  • 82

23 Answers23

1340

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
  • Nice feature. You can iterate through the array of names to do something else (e.g. remove all items from the collections). – Hilton Perantunes Apr 27 '15 at 15:34
  • 20
    [`db.getCollectionNames()` was removed in favor of `db.listCollections()`](http://mongodb.github.io/node-mongodb-native/2.0/meta/changes-from-1.0/) – Oliver Salzburg May 18 '15 at 15:07
  • 8
    Can we please get `db.listCollections()` as the answer shown here and checked in green? Otherwise people are making the same mistake I did countless times when they come to this answer - and attempt to use `db.getCollectionNames` and the error comes back `db.collectionNames is not a function`. –  Dec 17 '15 at 20:20
  • 28
    @niftylettuce This question is about the MongoDB shell, not the node.js driver. [`db.getCollectionNames()`](https://docs.mongodb.org/manual/reference/method/db.getCollectionNames/#db.getCollectionNames) is still the right answer for the shell. – JohnnyHK Jan 18 '16 at 20:51
  • 1
    @JohnnyHK I do not agree that the `node` answer here is not well placed! Please look at the google result rank and people like me come here with a node env wanting to have a quick solution. Node is widely used with mongo. But nevertheless, the node solution here does not work for me;( – Timo Jun 09 '22 at 06:18
  • db.getCollectionNames() will only show the first 100 collections, if you need more use 'show collections' – moose-o Oct 27 '22 at 14:28
469
> show collections

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

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • 2
    You can't use the show collections output in a script, but one can do x=db.getCollectionNames() to get an array of all the names. – ceteras Apr 05 '13 at 11:43
  • 1
    What do the two numbers listed after every collection mean? Two types of sizes? `content 1145.586MB / 1506.855MB` for example. – Dan Dascalescu Jun 20 '14 at 01:37
  • 1
    @Dan: I haven't used MongoDB in a while, but my best guess is that it's the size of the data stored in the collection vs. the total amount allocated to that collection (to handle minor updates and growth without having to constantly reallocate new space for the entire collection's contents). – Cameron Sep 16 '14 at 17:34
307

How do I list all collections for the current database that I'm 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bharadwaja Bapatla
  • 3,223
  • 1
  • 13
  • 14
56

> show tables

It gives the same result as Cameron's answer.

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
36

Apart from the options suggested by other people:

show collections  // Output every collection
show tables
db.getCollectionNames() // Shows all collections as a list

There is also another way which can be really handy if you want to know how each of the collections was created (for example, it is a capped collection with a particular size):

db.system.namespaces.find()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
27

First you need to use a database to show all collection/tables inside it.

>show dbs
users 0.56787GB
test (empty)
>db.test.help() // this will give you all the function which can be used with this db
>use users
>show tables //will show all the collection in the db
Tarun Gupta
  • 6,305
  • 2
  • 42
  • 39
20

Try:

help // To show all help methods
show dbs  // To show all dbs
use dbname  // To select your db
show collections // To show all collections in selected db
user
  • 86,916
  • 18
  • 197
  • 190
Indrajeet Singh
  • 2,958
  • 25
  • 25
17

You can use show tables or show collections.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lxg
  • 215
  • 3
  • 9
  • 3
    @LalitKumarB: How come is that? Based on other answers, that is suitable answer for this that actually might work. At least it is an attempt to answer. What it is, is an answer to very old question that already has multiple correct answers posted. – Roope Hakulinen Apr 16 '15 at 09:27
15
 1. show collections; // Display all collections
 2. show tables     // Display all collections
 3. db.getCollectionNames();   // Return array of collection. Example :[ "orders", "system.profile" ]

Detailed information for every collection:

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
  • For users with the required access (privileges that grant listCollections action on the database), the method lists the names of all collections for the database.
  • For users without the required access, the method lists only the collections for which the users has privileges. For example, if a user has find on a specific collection in a database, the method would return just that collection.

To list collections list based on a search string.

db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })

Example: Find all collection having "import" in the name

db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
  • Can I get the list of collection which contains some name like filter – Parveen Apr 27 '20 at 12:04
  • @Praveen - I've updated my answer to include answer to you case. Hope that helps – Amitesh Bharti Apr 28 '20 at 09:40
  • Thanks Amitesh. I wrote my script db.getCollectionNames().forEach(function(collName) { if (collName.startsWith("TestCollection_")) { print("droping index for " + collName); db.getCollection(collName).dropIndex("ts_1"); } }); – Parveen Apr 30 '20 at 15:10
13

The following commands on mongoshell are common.

show databases
show collections

Also,

show dbs
use mydb
db.getCollectionNames()

Sometimes it's useful to see all collections as well as the indexes on the collections which are part of the overall namespace:

Here's how you would do that:

db.getCollectionNames().forEach(function(collection) {
    indexes = db[collection].getIndexes();
    print("Indexes for " + collection + ":");
    printjson(indexes);
});

Between the three commands and this snippet, you should be well covered!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sood
  • 169
  • 1
  • 6
13

I think one of the biggest confusions is the difference between what you can do with mongo (or an interactive/hybrid shell) vs. mongo --eval (or a pure JavaScript shell). I keep these helpful documents handy:

Here is an example of scripting what you might otherwise do with show commands:

# List all databases and the collections in them

mongo --eval "
    db.getMongo().getDBNames().forEach(
        function(v, i){
            print(
                v + '\n\t' +
                db.getSiblingDB(v).getCollectionNames().join('\n\t')
            )
        }
    )
"

Note: That works really well as a one-liner. (But it looks terrible on Stack Overflow.)

mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
12

The command used for displaying all the collections in the MongoDB database is

show collections

Before running the show collections command you have to select the database:

use mydb // mydb is the name of the database being selected

To see all the databases, you can use the command

show dbs // Shows all the database names present

For more information, visit see Getting Started.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kkk
  • 1,850
  • 1
  • 25
  • 45
11

If you want to show all collections from the MongoDB shell (command line), use the shell helper,

show collections

that shows all collections for the current database. If you want to get all collection lists from your application then you can use the MongoDB database method

db.getCollectionNames()

For more information about the MongoDB shell helper, you can see mongo Shell Quick Reference.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Engr. Hasanuzzaman Sumon
  • 2,103
  • 3
  • 29
  • 38
8
> show dbs        
anuradhfirst  0.000GB
local         0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
  • connect with the MongoDB database using mongo. This will start the connection.
  • then run show dbs command. This will show you all exiting/available databases.
  • then select the database you want. In the above it is anuradhfirst. Then run use anuradhfirst. This will switch to the database you want.
  • then run show collections command. This will show all the collections inside your selected database.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
caldera.sac
  • 4,918
  • 7
  • 37
  • 69
6

For switching to the database.

By:

use {your_database_name} example:

use friends

where friends is the name of your database.

Then write:

db.getCollectionNames()
show collections

This will give you the name of collections.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shashikant Pandit
  • 2,752
  • 22
  • 29
4

On >=2.x, you can do

db.listCollections()

On 1.x you can do

db.getCollectionNames()
Aniruddh
  • 7,648
  • 1
  • 26
  • 43
  • 1
    as @JohnnyHK [pointed out](http://stackoverflow.com/questions/8866041/how-to-list-all-collections-in-the-mongo-shell#comment-57467037) this only applies to the **node driver** and not the **mongo shell** per OP question – Jeff Puckett May 03 '16 at 21:06
  • @JeffPuckettII I don't use Node. This works perfectly for me inside mongo shell. I wonder why it would not? – Aniruddh May 07 '16 at 08:10
  • 1
    I'm running MongoDB shell version: 3.2.6, and when I run `db.getCollectionNames()` I get `[ "users" ]` because I have a users collection. If I try `db.listCollections()` then it results in `[thread1] TypeError: db.listCollections is not a function : @(shell):1:1` – Jeff Puckett May 07 '16 at 20:29
3

List all collections from the mongo shell:

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

Note: Collections will show from current database where you are in currently

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hasib Kamal Chowdhury
  • 2,476
  • 26
  • 28
2

show collections

This command usually works on the MongoDB shell once you have switched to the database.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PHINCY L PIOUS
  • 335
  • 1
  • 3
  • 7
2

In case anyone uses Python & PyMongo:

db.list_collection_names()

eggsloveoats
  • 453
  • 4
  • 14
1

For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.getCollectionNames() will return no data, even if there are existing collections.

For further details, please refer to this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul
  • 15,979
  • 4
  • 42
  • 63
  • [Related meta post](https://meta.stackoverflow.com/questions/394510/copy-from-documentation-and-mentioning-source-not-enough/394511#394511). – Peter Mortensen Mar 11 '20 at 03:54
0

Use the following command from the mongo shell:

show collections
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anoop Sharma
  • 381
  • 3
  • 3
0

I use listCollections (supports MongoDB 3.0 and up) for this purpose.

Example:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });

To fetch more information like the index of the collection:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });

To print just the collection names:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})

I feel this provides more flexibility.

Read more: listCollections

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
-1
show collections

or

show tables

or

db.getCollectionNames();
nixxo_raa
  • 391
  • 8
  • 21