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


当前回答

如果有人使用Python和PyMongo:

db.list_collection_names()

其他回答

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

>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

用于显示MongoDB数据库中所有集合的命令是

show collections

在运行show collections命令之前,必须选择数据库:

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

要查看所有数据库,可以使用以下命令

show dbs // Shows all the database names present

有关详细信息,请参阅入门。

使用mongo shell中的以下命令:

show collections

显示表

切换到数据库后,该命令通常在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);
});

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