在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) })

其他回答

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

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

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

db.system.namespaces.find()

如何列出当前使用的数据库的所有集合?

三种方法

显示表显示表格db.getCollectionNames()


要列出所有数据库:

show dbs

要输入或使用给定数据库:

use databasename

要列出所有集合:

show collections

输出:

集合1集合2系统索引

(or)

show tables

输出:

集合1集合2系统索引

(or)

db.getCollectionNames()

输出:

[“collection1”,“collection2”,“system.indexs”]


输入或使用给定集合

use collectionname

你可以。。。

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

使用mongo shell中的以下命令:

show collections

显示表

显示表格

or

db.getCollectionNames();