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


当前回答

> show collections

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

其他回答

除了其他人建议的选项外:

show collections  // Output every collection
show tables
db.getCollectionNames() // Shows all collections as a list

如果您想知道每个集合是如何创建的(例如,它是一个具有特定大小的上限集合),还有另一种方法非常方便:

db.system.namespaces.find()

列出mongo shell中的所有集合:

db.getCollectionNames()显示表显示表格注意:集合将显示您所在的当前数据库目前

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

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

如果要显示MongoDB shell(命令行)中的所有集合,请使用shell助手,

show collections

显示当前数据库的所有集合。如果您想从应用程序中获取所有集合列表,那么可以使用MongoDB数据库方法

db.getCollectionNames()

有关MongoDB shell助手的更多信息,请参阅mongoShell快速参考。

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

show collections

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

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

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

show dbs // Shows all the database names present

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