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


当前回答

显示表

切换到数据库后,该命令通常在MongoDB shell上运行。

其他回答

> show collections

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

用于切换到数据库。

By:

使用{your_database_name}示例:

use friends

其中friends是数据库的名称。

然后写下:

db.getCollectionNames()
show collections

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

在>=2.x时,您可以

db.listCollections()

在1.x上,您可以做到

db.getCollectionNames()

使用mongo shell中的以下命令:

show collections

你可以。。。

JavaScript(外壳):

db.getCollectionNames()

节点js:

db.listCollections()

非JavaScript(仅限shell):

show collections

我之所以称之为非JavaScript是因为:

$ mongo prodmongo/app --eval "show collections"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
2016-10-26T19:34:34.886-0400 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell eval):1:5

$ mongo prodmongo/app --eval "db.getCollectionNames()"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
[
    "Profiles",
    "Unit_Info"
]

如果你真的想得到甜蜜的节目集输出,你可以:

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info