我如何列出MongoDB Shell中的所有集合?

问题描述 投票:747回答:22

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

mongodb nosql mongo-shell
22个回答
1133
投票

您可以做...

JavaScript(shell):

db.getCollectionNames()

Node.js:

db.listCollections()

非JavaScript(仅外壳):

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

11
投票

如果要显示mongodb shell(命令行)中的所有集合,请使用shell帮助器

show collections

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

db.getCollectionNames()

有关更多信息,您可以看到mongodb shell助手http://docs.mongodb.org/manual/reference/mongo-shell/


11
投票

mongoshell上的以下命令很常见

show databases
show collections

也,

show dbs
use mydb
db.getCollectionNames()

有时查看所有集合以及集合中的索引是整体命名空间的一部分很有用:

您将如何执行此操作:

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

在这三个命令和这段代码之间,您应该被很好地介绍!


7
投票

[我认为最大的困惑之一是mongo(或交互式/混合外壳)与mongo --eval(或纯JavaScript外壳)之间的区别。我将这些有用的文档放在手边:

这里是编写脚本的示例,否则您可能会用show命令执行此操作:

# 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')
            )
        }
    )
"

注意:作为一个单一的内衬确实很好。 (但是在StackOverflow上看起来很糟糕。)

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

3
投票

在> = 2.x,可以执行

db.listCollections()

在1.x上可以执行

db.getCollectionNames()

3
投票

用于切换到数据库。通过:-使用{your_database_name}示例:

use friends

其中,朋友是您的数据库的名称。

然后写:-

db.getCollectionNames()
show collections

这将为您提供集合的名称。


3
投票

列出mongo shell中的所有集合:

  • db.getCollectionNames()
  • 显示收藏集
  • 显示表格

注意:集合将从您所在的当前数据库显示当前


1
投票
> show dbs        
anuradhfirst  0.000GB
local         0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
  • 使用mongo与mongo数据库连接,这将启动连接。
  • 然后运行show dbs命令,这将向您显示所有现有/可用的数据库。
  • 然后选择所需的database。在其上方的是anuradhfirst,然后运行use anuradhfirst。这将切换到所需的数据库。
  • 然后运行show collections命令,这将显示所选数据库中的所有collections

1
投票

显示收藏集

一旦切换到数据库,此命令通常在mongo shell上有效。


1
投票

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

有关更多详细信息,请参阅this


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

每个收藏的详细信息

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
  • 对于具有所需访问权限的用户(授予特权的用户对数据库执行listCollections操作),该方法列出了名称数据库的所有集合中的一个。
  • 对于没有所需访问权限的用户,该方法仅列出用户具有特权的集合。例如,如果用户在数据库中的特定集合上找到了该方法将只返回该集合。

419
投票
> show collections

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


0
投票

为此,我使用listCollections(支持mongo 3.0及更高版本。)>

示例:

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

要获取更多信息,例如集合索引:

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

仅打印集合名称:

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

我觉得这提供了更大的灵活性。

更多:https://docs.mongodb.com/manual/reference/command/listCollections/


0
投票
show collections

-1
投票

使用mongo shell中的以下命令:-显示收藏集


261
投票

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

3种方法

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

列出所有数据库

show dbs

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

use databasename

列出所有收藏集

show collections

输出:

collection1  
collection2  
system.indexes

(或)

show tables

输出:

collection1  
collection2  
system.indexes

(或)

db.getCollectionNames()

输出:

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

要输入或使用给定的收藏夹

use collectionname

52
投票

> show tables

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


30
投票

除了其他人建议的选项:

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

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

db.system.namespaces.find()

22
投票

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

>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

15
投票

您可以使用show tablesshow collections


14
投票

尝试:

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

12
投票

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

show collections 

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

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

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

show dbs // shows all the database names present 

有关更多信息,请访问此链接:http://docs.mongodb.org/manual/tutorial/getting-started/

© www.soinside.com 2019 - 2024. All rights reserved.