840

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

4

23 回答 23

1265

你可以做...

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"
]

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

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info
于 2012-01-14T22:57:47.127 回答
447
> show collections

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

于 2012-01-14T22:56:24.183 回答
283

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

Three methods

  • 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

(or)

show tables

Output:

collection1
collection2
system.indexes

(or)

db.getCollectionNames()

Output:

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

To enter or use given collection

use collectionname
于 2014-05-19T10:08:36.207 回答
54

> show tables

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

于 2013-10-14T18:20:01.827 回答
31

除了其他人建议的选项:

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

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

db.system.namespaces.find()
于 2013-10-31T05:22:47.157 回答
25

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

>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
于 2013-11-09T07:54:28.357 回答
17

尝试:

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
于 2014-05-05T06:21:35.530 回答
16

You can use show tables or show collections.

于 2015-04-16T08:46:13.710 回答
12

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

show collections

Before running the show collections command you have to select the database:

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.

于 2015-04-04T16:38:39.577 回答
11

If you want to show all collections from the MongoDB shell (command line), use the 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

db.getCollectionNames()

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

于 2014-12-22T06:37:53.143 回答
11

The following commands on mongoshell are common.

show databases
show collections

Also,

show dbs
use mydb
db.getCollectionNames()

Sometimes it's useful to see all collections as well as the indexes on the collections which are part of the overall namespace:

Here's how you would do that:

db.getCollectionNames().forEach(function(collection) {
    indexes = db[collection].getIndexes();
    print("Indexes for " + collection + ":");
    printjson(indexes);
});

Between the three commands and this snippet, you should be well covered!

于 2016-01-24T17:05:54.280 回答
11

I think one of the biggest confusions is the difference between what you can do with mongo (or an interactive/hybrid shell) vs. mongo --eval (or a pure JavaScript shell). I keep these helpful documents handy:

Here is an example of scripting what you might otherwise do with show commands:

# List all databases and the collections in them

mongo --eval "
    db.getMongo().getDBNames().forEach(
        function(v, i){
            print(
                v + '\n\t' +
                db.getSiblingDB(v).getCollectionNames().join('\n\t')
            )
        }
    )
"

Note: That works really well as a one-liner. (But it looks terrible on Stack Overflow.)

mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
于 2017-01-03T21:04:30.753 回答
6
> show dbs        
anuradhfirst  0.000GB
local         0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
  • connect with the MongoDB database using mongo. This will start the connection.
  • then run show dbs command. This will show you all exiting/available databases.
  • then select the database you want. In the above it is anuradhfirst. Then run use anuradhfirst. This will switch to the database you want.
  • then run show collections command. This will show all the collections inside your selected database.
于 2016-08-22T05:42:54.343 回答
6
 1. show collections; // Display all collections
 2. show tables     // Display all collections
 3. db.getCollectionNames();   // Return array of collection. Example :[ "orders", "system.profile" ]

Detailed information for every collection:

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
  • For users with the required access (privileges that grant listCollections action on the database), the method lists the names of all collections for the database.
  • For users without the required access, the method lists only the collections for which the users has privileges. For example, if a user has find on a specific collection in a database, the method would return just that collection.

To list collections list based on a search string.

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

Example: Find all collection having "import" in the name

db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
于 2018-08-09T18:22:47.723 回答
5

For switching to the database.

By:

use {your_database_name} example:

use friends

where friends is the name of your database.

Then write:

db.getCollectionNames()
show collections

This will give you the name of collections.

于 2017-07-20T12:27:51.730 回答
4

On >=2.x, you can do

db.listCollections()

On 1.x you can do

db.getCollectionNames()
于 2016-01-12T02:30:44.907 回答
3

List all collections from the mongo shell:

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

Note: Collections will show from current database where you are in currently

于 2018-07-29T10:45:54.857 回答
2

show collections

This command usually works on the MongoDB shell once you have switched to the database.

于 2016-12-07T11:38:40.243 回答
2

In case anyone uses Python & PyMongo:

db.list_collection_names()

于 2021-10-28T23:01:01.663 回答
1

For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.getCollectionNames() will return no data, even if there are existing collections.

For further details, please refer to this.

于 2017-03-21T15:27:04.357 回答
0

I use listCollections (supports MongoDB 3.0 and up) for this purpose.

Example:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });

To fetch more information like the index of the collection:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });

To print just the collection names:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})

I feel this provides more flexibility.

Read more: listCollections

于 2018-11-19T09:09:08.467 回答
-1

Use the following command from the mongo shell:

show collections
于 2018-04-05T07:41:54.917 回答
-1
show collections

or

show tables

or

db.getCollectionNames();
于 2020-01-02T11:19:54.840 回答