MongoDB显示所有集合中的所有内容

Ren*_*eno 128 collections find mongodb

是否可以在MongoDB中显示所有集合及其内容?

是一个一个一个展示的唯一方法吗?

小智 221

进入终端/命令行后,访问要使用的数据库/集合,如下所示:

show dbs
use <db name>
show collections
Run Code Online (Sandbox Code Playgroud)

选择您的集合并键入以下内容以查看该集合的所有内容:

db.collectionName.find()
Run Code Online (Sandbox Code Playgroud)

有关MongoDB快速参考指南的更多信息.

  • 如果您需要在视觉上整理呈现给您的集合,我还建议:`db.collectionName.find().pretty()` (14认同)
  • 请将此表述为正确答案。您只能通过编写代码来查看所有集合中的所有内容,而不能通过 cli 查询 (2认同)
  • 请记住,如果集合名称中包含某些字符(例如连字符),则此方法将无效。在这种情况下,请使用db [“ collection-name”]。find()` (2认同)
  • @imbatman,请**不要**将此标记为正确答案。你可以放弃“请”,因为事实是没有争议的。检查 Juan de Dios 的 [answer](/sf/answers/4002359791/)、yonzen 的 [answer](/sf/answers/3891689021/) 或 [我的](https ://stackoverflow.com/a/76953032/2571805)。所有这些都做了你反对的事情:回答问题。 (2认同)

Deb*_*tta 101

第1步:查看所有数据库:

show dbs
Run Code Online (Sandbox Code Playgroud)

第2步:选择数据库

use your_database_name
Run Code Online (Sandbox Code Playgroud)

第3步:显示集合

show collections
Run Code Online (Sandbox Code Playgroud)

这将列出所选数据库中的所有集合.

第4步:查看所有数据

db.collection_name.find() 
Run Code Online (Sandbox Code Playgroud)

要么

db.collection_name.find().pretty()
Run Code Online (Sandbox Code Playgroud)

  • 我用数据库名称替换了数据库名称,这继续给我错误。所以不要做像我这样的愚蠢的事情:D,坚持使用db。&lt;collection_name&gt; .find();` (2认同)

小智 44

第一步:进入MongoDB shell。

蒙戈

步骤2:用于显示所有数据库。

显示数据库;

第 3 步:对于选择数据库:

使用'databases_name'

第 4 步:用于数据库的统计信息。

db.stats()

第 5 步:列出所有集合(表)。

显示集合

第 6 步:打印特定集合中的数据。

db.'collection_name'.find().pretty()


Bru*_*ira 31

var collections = db.getCollectionNames();
for(var i = 0; i< collections.length; i++){    
   print('Collection: ' + collections[i]); // print the name of each collection
   db.getCollection(collections[i]).find().forEach(printjson); //and then print the json of each of its elements
}
Run Code Online (Sandbox Code Playgroud)

我认为这个脚本可能会得到你想要的.它打印每个集合的名称,然后在json中打印它的元素.


小智 7

这边走:

db.collection_name.find().toArray().then(...function...)
Run Code Online (Sandbox Code Playgroud)


yun*_*zen 6

这将:

db.getCollectionNames().forEach(c => {
    db[c].find().forEach(d => {
        print(c); 
        printjson(d)
    })
})
Run Code Online (Sandbox Code Playgroud)


Jua*_*ios 6

如果您使用mongoshell,我更喜欢另一种方法:

首先作为另一个答案:use my_database_name然后:

db.getCollectionNames().map( (name) => ({[name]: db[name].find().toArray().length}) )
Run Code Online (Sandbox Code Playgroud)

此查询将显示如下内容:

[
        {
                "agreements" : 60
        },
        {
                "libraries" : 45
        },
        {
                "templates" : 9
        },
        {
                "users" : 19
        }
]

Run Code Online (Sandbox Code Playgroud)

db.getCollectionInfos()如果您有如此多的数据并且也很有帮助,您可以使用类似的方法,它非常有用。

  • 使用 `count()` 而不是 `find()`: `db.getCollectionNames().map( (name) =&gt; ({[name]: db[name].count()}) )` (2认同)

Ami*_*mar 5

在编写以下查询之前,请先进入您的cmd或PowerShell

TYPE:
mongo             //To get into MongoDB shell
use <Your_dbName>      //For Creating or making use of existing db
Run Code Online (Sandbox Code Playgroud)

要列出所有集合名称,请使用以下选项之一:

show collections  //output every collection
  OR
show tables
  OR
db.getCollectionNames() //shows all collections as a list
Run Code Online (Sandbox Code Playgroud)

要显示所有集合的内容或数据,请使用下面列出的由Bruno_Ferreira发布的代码。

var collections = db.getCollectionNames();
for(var i = 0; i< collections.length; i++) {    
   print('Collection: ' + collections[i]); // print the name of each collection
   db.getCollection(collections[i]).find().forEach(printjson); //and then print     the json of each of its elements
}
Run Code Online (Sandbox Code Playgroud)