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


当前回答

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

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

其他回答

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

如果有人使用Python和PyMongo:

db.list_collection_names()

> show collections

将列出当前选定DB中的所有集合,如命令行帮助(help)中所述。

在>=2.x时,您可以

db.listCollections()

在1.x上,您可以做到

db.getCollectionNames()