programing

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

megabox 2023. 4. 4. 21:11
반응형

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

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

하나씩 보여주는 방법밖에 없나요?

단말기/커맨드라인에 들어가면 다음과 같이 사용하는 데이터베이스/컬렉션에 액세스합니다.

show dbs
use <db name>
show collections

컬렉션을 선택하고 다음을 입력하여 해당 컬렉션의 모든 내용을 표시합니다.

db.collectionName.find()

자세한 내용은 MongoDB 퀵 레퍼런스 가이드를 참조하십시오.

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
}

다른 방법을 사용하는 것이 좋습니다.mongo셸:

먼저 다른 사람이 대답할 때:use my_database_name그 후, 다음과 같이 합니다.

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

이 쿼리에는 다음과 같은 내용이 표시됩니다.

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

다음과 같은 방법을 사용할 수 있습니다.db.getCollectionInfos()데이터가 많으면 편리하고 도움이 됩니다.

이쪽:

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

이것으로 끝납니다.

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

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

반응형