How to list all collections in the mongo shell?

2019-01-12 13:24发布

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

21条回答
The star\"
2楼-- · 2019-01-12 13:35

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
查看更多
叼着烟拽天下
3楼-- · 2019-01-12 13:35

For switch 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.

查看更多
混吃等死
4楼-- · 2019-01-12 13:36

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 3 commands and this snippet you should be well covered!

查看更多
Anthone
5楼-- · 2019-01-12 13:38

The command used for displaying all the collection 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 info visit this link : http://docs.mongodb.org/manual/tutorial/getting-started/

查看更多
时光不老,我们不散
6楼-- · 2019-01-12 13:38

use following command from mongo shell :- show collections

查看更多
淡お忘
7楼-- · 2019-01-12 13:39

you can use show tables or show collections

查看更多
登录 后发表回答