In the MongoDB shell, how do I list all collections for the current database that I'm using?
This question is related to
mongodb
mongo-shell
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
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 } )
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) })
> show tables
It gives the same result as Cameron's answer.
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
show collections
This command usually works on the MongoDB shell once you have switched to the database.
Use the following command from the mongo
shell:
show collections
How do I list all collections for the current database that I'm using?
show collections
show tables
db.getCollectionNames()
show dbs
use databasename
show collections
Output:
collection1 collection2 system.indexes
(or)
show tables
Output:
collection1 collection2 system.indexes
(or)
db.getCollectionNames()
Output:
[ "collection1", "collection2", "system.indexes" ]
use collectionname
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!
You can use show tables
or show collections
.
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
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.
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
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.
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.
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
mongo
. This will start the connection.show dbs
command. This will show you all exiting/available databases.database
you want. In the above it is anuradhfirst
. Then run use anuradhfirst
. This will switch to the database you want.show collections
command. This will show all the collections
inside your selected database.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.
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()
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'))})"
On >=2.x, you can do
db.listCollections()
On 1.x you can do
db.getCollectionNames()
show collections
or
show tables
or
db.getCollectionNames();
> show collections
will list all the collections in the currently selected DB, as stated in the command line help (help
).
Source: Stackoverflow.com