在MongoDB shell中,如何列出当前使用的数据库的所有集合?


当前回答

mongoshell上的以下命令是常见的。

show databases
show collections

而且

show dbs
use mydb
db.getCollectionNames()

有时,查看所有集合以及作为整体命名空间一部分的集合上的索引很有用:

以下是您的操作方法:

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

在这三个命令和这段代码之间,您应该很熟悉!

其他回答

使用mongo shell中的以下命令:

show collections
> show dbs        
anuradhfirst  0.000GB
local         0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record

使用mongo连接MongoDB数据库。这将启动连接。然后运行showdbs命令。这将显示所有退出/可用的数据库。然后选择所需的数据库。在上文中,它是第一个。然后运行use anuradhfirst。这将切换到所需的数据库。然后运行showcollections命令。这将显示所选数据库中的所有集合。

>显示表格

它给出了与卡梅伦的答案相同的结果。

对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果从mongo shell的一个版本运行db.getCollectionNames()3.0之前的版本或3.0兼容版本之前的驱动程序版本,db.getCollectionNames()将不返回任何数据,即使存在现有集合。

有关详细信息,请参阅。

首先,您需要使用数据库来显示其中的所有集合/表。

>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