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


当前回答

显示表

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

其他回答

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

show collections

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

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

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

show dbs // Shows all the database names present

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

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

三种方法

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


要列出所有数据库:

show dbs

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

use databasename

要列出所有集合:

show collections

输出:

集合1集合2系统索引

(or)

show tables

输出:

集合1集合2系统索引

(or)

db.getCollectionNames()

输出:

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


输入或使用给定集合

use collectionname

对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果从mongo shell的一个版本运行db.getCollectionNames()3.0之前的版本或3.0兼容版本之前的驱动程序版本,db.getCollectionNames()将不返回任何数据,即使存在现有集合。

有关详细信息,请参阅。

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

你可以。。。

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