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快速参考指南的更多信息.
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)
小智 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)
这将:
db.getCollectionNames().forEach(c => {
db[c].find().forEach(d => {
print(c);
printjson(d)
})
})
Run Code Online (Sandbox Code Playgroud)
如果您使用mongo
shell,我更喜欢另一种方法:
首先作为另一个答案: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()
如果您有如此多的数据并且也很有帮助,您可以使用类似的方法,它非常有用。
在编写以下查询之前,请先进入您的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)
归档时间: |
|
查看次数: |
261211 次 |
最近记录: |