ChatGPT解决这个技术问题 Extra ChatGPT

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

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


P
Peter Mortensen

你可以做...

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

不错的功能。您可以遍历名称数组以执行其他操作(例如,从集合中删除所有项目)。
我们可以得到db.listCollections()作为此处显示的答案并以绿色选中吗?否则,人们在得到这个答案时犯了我无数次犯过的同样的错误 - 并尝试使用 db.getCollectionNames 并且错误又回来了 db.collectionNames is not a function
@niftylettuce 这个问题是关于 MongoDB shell,而不是 node.js 驱动程序。 db.getCollectionNames() 仍然是 shell 的正确答案。
@JohnnyHK 我不同意这里的 node 答案不合适!请查看谷歌结果排名,像我这样的人带着想要快速解决方案的节点环境来到这里。 Node 广泛用于 mongo。但是,这里的节点解决方案对我不起作用;(
C
Cameron
> show collections

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


您不能在脚本中使用 show collections 输出,但可以执行 x=db.getCollectionNames() 来获取所有名称的数组。
每次收藏后列出的两个数字是什么意思?两种尺寸? content 1145.586MB / 1506.855MB 例如。
@Dan:我有一段时间没有使用 MongoDB,但我最好的猜测是,它是存储在集合中的数据大小与分配给该集合的总量之比(以处理较小的更新和增长,而无需不断地重新分配整个集合内容的新空间)。
P
Peter Mortensen

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

三种方法

显示收藏

显示表格

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

+1 以获得最完整的答案。说明 show tables 对于那些来自关系 dbms 背景的人来说非常有帮助。
不,use 是使用数据库,与集合无关
我们也可以使用 db.collections
K
Kevin Meredith

> show tables

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


P
Peter Mortensen

除了其他人建议的选项:

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

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

db.system.namespaces.find()

T
Tarun Gupta

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

>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

u
user

尝试:

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

P
Peter Mortensen

您可以使用 show tablesshow collections


@LalitKumarB:那是怎么回事?根据其他答案,这是实际可能有效的合适答案。至少这是一种尝试回答。它是什么,是对已经发布了多个正确答案的非常古老的问题的答案。
P
Peter Mortensen

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

show collections

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

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

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

show dbs // Shows all the database names present

有关详细信息,请访问 Getting Started


P
Peter Mortensen

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

在这三个命令和这段代码之间,你应该得到很好的覆盖!


P
Peter Mortensen

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

show collections

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

db.getCollectionNames()

有关 MongoDB shell 帮助程序的更多信息,您可以查看 mongo Shell Quick Reference


P
Peter Mortensen

我认为最大的困惑之一是您可以使用 mongo(或交互式/混合 shell)与 mongo --eval(或纯 JavaScript shell)之间的区别。我把这些有用的文件放在手边:

交互式和脚本化 mongo 之间的区别

Mongo Shell 命令助手

下面是一个脚本示例,您可以使用 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')
            )
        }
    )
"

注意:这非常适合作为单线。 (但在 Stack Overflow 上看起来很糟糕。)

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

A
Amitesh Bharti
 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) })

我可以获取包含某些名称的集合列表,例如过滤器
@Praveen - 我已经更新了我的答案,包括对你的案例的回答。希望有帮助
谢谢阿米特什。我写了我的脚本 db.getCollectionNames().forEach(function(collName) { if (collName.startsWith("TestCollection_")) { print("droping index for " + collName); db.getCollection(collName).dropIndex(" ts_1"); } });
P
Peter Mortensen
> show dbs        
anuradhfirst  0.000GB
local         0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record

使用 mongo 连接 MongoDB 数据库。这将启动连接。

然后运行 show dbs 命令。这将向您显示所有现有/可用的数据库。

然后选择你想要的数据库。在上面它是anuradhfirst。然后运行使用 anuradhfirst。这将切换到您想要的数据库。

然后运行 show collections 命令。这将显示您选择的数据库中的所有集合。


这个答案显示了获取集合列表的正确步骤。
P
Peter Mortensen

用于切换到数据库。

经过:

使用 {your_database_name} 示例:

use friends

其中 friends 是您的数据库的名称。

然后写:

db.getCollectionNames()
show collections

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


这与以前的答案有何不同?
A
Aniruddh

在 >=2.x 上,您可以执行

db.listCollections()

在 1.x 上你可以做

db.getCollectionNames()

作为@JohnnyHK pointed out,这仅适用于 node driver 而不是 mongo shell 每个 OP 问题
@JeffPuckettII 我不使用节点。这在 mongo shell 中非常适合我。我想知道为什么它不会?
我正在运行 MongoDB shell 版本:3.2.6,当我运行 db.getCollectionNames() 时,我得到 [ "users" ],因为我有一个 users 集合。如果我尝试 db.listCollections() 那么它会导致 [thread1] TypeError: db.listCollections is not a function : @(shell):1:1
P
Peter Mortensen

列出 mongo shell 中的所有集合:

db.getCollectionNames() 显示集合 显示表格 注意:集合将显示您当前所在的当前数据库


这与以前的答案有何不同?
P
Peter Mortensen

显示收藏

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


e
eggsloveoats

如果有人使用 Python 和 PyMongo:

db.list_collection_names()


P
Peter Mortensen

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

有关详细信息,请参阅this


P
Peter Mortensen

为此,我使用 listCollections(支持 MongoDB 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)})

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

阅读更多:listCollections


n
nixxo_raa

显示收藏

或者

显示表格

或者

db.getCollectionNames();


这与以前的答案有何不同?
P
Peter Mortensen

mongo shell 中使用以下命令:

show collections

这与以前的答案有何不同?