如何在 MongoDB shell 中列出所有集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8866041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:25:13  来源:igfitidea点击:

How can I list all collections in the MongoDB shell?

mongodbmongo-shell

提问by coffee-grinder

In the MongoDB shell, how do I list all collections for the current database that I'm using?

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

回答by AdaTheDev

You can do...

你可以做...

JavaScript (shell):

JavaScript(外壳):

db.getCollectionNames()

Node.js:

节点.js:

db.listCollections()

Non-JavaScript (shell only):

非 JavaScript(仅限 shell):

show collections

The reason I call that non-JavaScript is because:

我称之为非 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"
]

If you really want that sweet, sweet show collectionsoutput, you can:

如果你真的想要那种甜蜜、甜蜜的show collections输出,你可以:

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info

回答by Cameron

> show collections

will list all the collections in the currently selected DB, as stated in the command line help (help).

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

回答by Bharadwaja Bapatla

How do I list all collections for the current database that I'm using?

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

Three methods

三种方法

  • show collections
  • show tables
  • db.getCollectionNames()
  • show collections
  • show tables
  • db.getCollectionNames()


To list all databases:

列出所有数据库

show dbs

To enter or use a given database:

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

use databasename


To list all collections:

列出所有集合

show collections

Output:

输出:

collection1
collection2
system.indexes
collection1
collection2
system.indexes

(or)

(或者)

show tables

Output:

输出:

collection1
collection2
system.indexes
collection1
collection2
system.indexes

(or)

(或者)

db.getCollectionNames()

Output:

输出:

[ "collection1", "collection2", "system.indexes" ]
[ "collection1", "collection2", "system.indexes" ]


To enter or use given collection

进入或使用给定的集合

use collectionname

回答by Kevin Meredith

> show tables

> show tables

It gives the same result as Cameron's answer.

它给出了与卡梅伦的回答相同的结果。

回答by Salvador Dali

Apart from the options suggested by other people:

除了其他人建议的选项:

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

There is also another way which can be really handy if you want to know how each of the collections was created (for example, it is a capped collection with a particular size):

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

db.system.namespaces.find()

回答by Tarun Gupta

First you need to use a database to show all collection/tables inside it.

首先,您需要使用数据库来显示其中的所有集合/表。

>show dbs
users 0.56787GB
test (empty)
>db.test.help() // this will give you all the function which can be used with this db
>use users
>show tables //will show all the collection in the db

回答by lxg

You can use show tablesor show collections.

您可以使用show tablesshow collections

回答by Indrajeet Singh

Try:

尝试:

help // To show all help methods
show dbs  // To show all dbs
use dbname  // To select your db
show collections // To show all collections in selected db

回答by kkk

The command used for displaying all the collections in the MongoDB database is

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

show collections

Before running the show collectionscommand you have to select the database:

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

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

To see all the databases, you can use the command

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

show dbs // Shows all the database names present

For more information, visit see Getting Started.

有关更多信息,请参阅入门

回答by Engr. Hasanuzzaman Sumon

If you want to show all collections from the MongoDB shell (command line), use the shell helper,

如果要显示来自 MongoDB shell(命令行)的所有集合,请使用 shell helper,

show collections

that shows all collections for the current database. If you want to get all collection lists from your application then you can use the MongoDB database method

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

db.getCollectionNames()

For more information about the MongoDB shell helper, you can see mongoShell Quick Reference.

有关 MongoDB shell helper 的更多信息,您可以查看mongoShell Quick Reference