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


当前回答

> show collections

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

其他回答

> show collections

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

使用mongo shell中的以下命令:

show collections

为此,我使用listCollections(支持MongoDB 3.0及更高版本)。

例子:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });

要获取更多信息,如集合的索引:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });

要仅打印集合名称:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})

我觉得这提供了更多的灵活性。

阅读更多:listCollections

显示表

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

显示表

显示表格

or

db.getCollectionNames();