在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命令。这将显示所选数据库中的所有集合。

如果有人使用Python和PyMongo:

db.list_collection_names()

如何列出当前使用的数据库的所有集合?

三种方法

显示表显示表格db.getCollectionNames()


要列出所有数据库:

show dbs

要输入或使用给定数据库:

use databasename

要列出所有集合:

show collections

输出:

集合1集合2系统索引

(or)

show tables

输出:

集合1集合2系统索引

(or)

db.getCollectionNames()

输出:

[“collection1”,“collection2”,“system.indexs”]


输入或使用给定集合

use collectionname
> show collections

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

用于切换到数据库。

By:

使用{your_database_name}示例:

use friends

其中friends是数据库的名称。

然后写下:

db.getCollectionNames()
show collections

这将为您提供集合的名称。