Programing

MongoDB 모든 컬렉션의 모든 컨텐츠 표시

crosscheck 2020. 7. 8. 07:52
반응형

MongoDB 모든 컬렉션의 모든 컨텐츠 표시


MongoDB에 모든 컬렉션과 내용을 표시 할 수 있습니까?

하나씩 보여줄 수있는 유일한 방법입니까?


터미널 / 명령 행에 있으면 다음과 같이 사용하려는 데이터베이스 / 컬렉션에 액세스하십시오.

show dbs
use <db name>
show collections

컬렉션을 선택하고 다음을 입력하여 해당 컬렉션의 모든 내용을보십시오.

db.collectionName.find()

MongoDB 빠른 참조 안내서 에 대한 자세한 정보는 여기를 참조 하십시오 .


1 단계 : 모든 데이터베이스보기

show dbs

2 단계 : 데이터베이스 선택

use your_database_name

3 단계 : 컬렉션 표시

show collections

선택한 데이터베이스의 모든 모음이 나열됩니다.

4 단계 : 모든 데이터보기

db.collection_name.find() 

또는

db.collection_name.find().pretty()

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
}

이 스크립트가 원하는 것을 얻을 수 있다고 생각합니다. 각 콜렉션의 이름을 인쇄 한 다음 요소를 json으로 인쇄합니다.


아래 쿼리를 작성하기 전에 먼저 cmd 또는 PowerShell에 들어가십시오.

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

모든 컬렉션 이름을 나열하려면 아래 옵션 중 하나를 사용하십시오.

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

모든 컬렉션 컨텐츠 또는 데이터를 표시하려면 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
}

이 방법:

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

이것은 할 것이다 :

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

mongo쉘을 사용하는 경우 다른 접근법을 선호합니다 .

다른 사람이 먼저 대답하면 use my_database_name다음과 같습니다.

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

이 쿼리는 다음과 같이 표시됩니다.

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

비슷한 접근 방식을 사용할 수 있습니다 db.getCollectionInfos(). 데이터가 너무 많으면 매우 유용합니다.

참고 URL : https://stackoverflow.com/questions/24985684/mongodb-show-all-contents-from-all-collections

반응형