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


当前回答

如果有人使用Python和PyMongo:

db.list_collection_names()

其他回答

可以使用显示表或显示集合。

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

show collections

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

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

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

show dbs // Shows all the database names present

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

显示表

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

 1. show collections; // Display all collections
 2. show tables     // Display all collections
 3. db.getCollectionNames();   // Return array of collection. Example :[ "orders", "system.profile" ]

每个集合的详细信息:

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )

对于具有所需访问权限(授予数据库listCollections操作的权限)的用户,该方法将列出数据库的所有集合的名称。对于没有所需访问权限的用户,该方法仅列出用户具有权限的集合。例如,如果用户在数据库中找到了特定集合,则该方法将仅返回该集合。

根据搜索字符串列出集合列表。

db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })

示例:查找名称中包含“import”的所有集合

db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })

列出mongo shell中的所有集合:

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