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


当前回答

列出mongo shell中的所有集合:

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

其他回答

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

show collections

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

db.getCollectionNames()

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

显示表

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

列出mongo shell中的所有集合:

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

为此,我使用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

显示表

显示表格

or

db.getCollectionNames();