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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24985684/
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 13:47:52  来源:igfitidea点击:

MongoDB Show all contents from all collections

mongodbcollectionsfind

提问by Reno

Is it possible to show all collections and its contents in MongoDB?

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

Is the only way to show one by one?

只能一一展示吗?

回答by sharkySharks

Once you are in terminal/command line, access the database/collection you want to use as follows:

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

show dbs
use <db name>
show collections

choose your collection and type the following to see all contents of that collection:

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

db.collectionName.find()

More info here on the MongoDB Quick Reference Guide.

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

回答by Debadatta

Step 1: See all your databases:

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

show dbs

Step 2: Select the database

第二步:选择数据库

use your_database_name

Step 3: Show the collections

第 3 步:显示集合

show collections

This will list all the collections in your selected database.

这将列出您选择的数据库中的所有集合。

Step 4: See all the data

第 4 步:查看所有数据

db.collection_name.find() 

or

或者

db.collection_name.find().pretty()

回答by 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
}

I think this script might get what you want. It prints the name of each collection and then prints its elements in json.

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

回答by Vaibhav gole

step 1: Enter into the MongoDB shell.

第一步:进入MongoDB shell。

mongo

蒙戈

step 2: for the display all the databases.

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

show dbs;

显示数据库;

step 3: for a select database :

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

use 'databases_name'

使用'databases_name'

step 4: for statistics of your database.

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

db.stats()

db.stats()

step 5: listing out all the collections(tables).

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

show collections

显示集合

step 6:print the data from a particular collection.

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

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

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

回答by Amit Kumar

Before writing below queries first get into your cmd or PowerShell

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

TYPE:
mongo             //To get into MongoDB shell
use <Your_dbName>      //For Creating or making use of existing db

To List All Collection Names use any one from below options :-

要列出所有集合名称,请使用以下选项中的任何一个:-

show collections  //output every collection
  OR
show tables
  OR
db.getCollectionNames() //shows all collections as a list

To show all collections content or data use below listed code which had been posted by Bruno_Ferreira.

要显示所有收藏内容或数据,请使用下面列出的由 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
}

回答by Vladimir Sostaric

This way:

这边走:

db.collection_name.find().toArray().then(...function...)

回答by yunzen

This will do:

这将:

db.getCollectionNames().forEach(c => {
    db[c].find().forEach(d => {
        print(c); 
        printjson(d)
    })
})

回答by Juan de Dios

I prefer another approach if you are using mongoshell:

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

First as the another answers: use my_database_namethen:

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

db.getCollectionNames().map( (name) => ({[name]: db[name].find().toArray().length}) )

This query will show you something like this:

此查询将显示如下内容:

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

You can use similar approach with db.getCollectionInfos()it is pretty useful if you have so much data.

db.getCollectionInfos()如果你有这么多数据,你可以使用类似的方法,它非常有用。