Computer >> 컴퓨터 >  >> 프로그래밍 >> MongoDB

MongoDB에서 특정 데이터베이스의 컬렉션 목록을 표시하는 방법

MongoDB에서 특정 데이터베이스의 컬렉션 목록 확인하기

MongoDB에서 특정 데이터베이스에 포함된 컬렉션들을 확인하려면 먼저 use 명령어를 사용해 해당 데이터베이스로 전환해야 합니다. 그런 다음 db.getCollectionNames() 메서드를 호출하면 됩니다.

기본 문법

use yourDatabaseName;
db.getCollectionNames();

실제 사용 예제

위 문법을 실제로 적용하여 'web'이라는 데이터베이스의 컬렉션 목록을 조회해 보겠습니다.

> use web
switched to db web
> db.getCollectionNames();

위 명령을 실행하면 다음과 같은 결과가 출력됩니다.

[
   "2015-myCollection",
   "2015-yourCollection",
   "2019-employeeCollection",
   "addColumnDemo",
   "applyConditionDemo",
   "calculateAverage",
   "calculateSumOfDocument",
   "changeSimpleFieldDemo",
   "check",
   "checkFieldDemo",
   "collationExample",
   "compoundIndexDemo",
   "countandsumdemo",
   "creatingAliasDemo",
   "decreasetimeusingindex",
   "demo1",
   "demo10",
   "demo11",
   "demo12",
   "demo13",
   "demo14",
   "demo15",
   "demo16",
   "demo17",
   "demo18",
   "demo19",
   "demo2",
   "demo20",
   "demo21",
   "demo22",
   "demo23",
   "demo24",
   "demo25",
   "demo26",
   "demo27",
   "demo28.example",
   "demo29",
   "demo3",
   "demo30",
   "demo31",
   "demo32",
   "demo33",
   "demo34",
   "demo35",
   "demo36",
   "demo37",
   "demo38",
   "demo39",
   "demo4",
   "demo5",
   "demo6",
   "demo7",
   "demo8",
   "demo9",
   "destinationCollection",
   "emp_info",
   "emptyCollection",
   "extractParticularElementDemo",
   "getSpecificData",
   "getTheMaxValueDemo",
   "indexCreationDemo",
   "missingDocumentDemo",
   "promoteSubfieldsDemo",
   "pullWithPositionalOperatorDemo",
   "removeNullDemo",
   "replacevaluedemo",
   "searchDocumentsDemo",
   "searchSubFieldDemo",
   "sourceCollection",
   "splitString",
   "testInArray",
   "twoSpecificIdsDemo",
   "updatingDemo",
   "version"
]

참고 사항

db.getCollectionNames()는 데이터베이스 내 모든 컬렉션의 이름을 배열 형태로 반환합니다. 만약 MongoDB 4.0 이상 버전을 사용하고 있다면, 필터 조건을 인자로 전달하여 특정 이름 패턴의 컬렉션만 조회할 수도 있습니다.

> db.getCollectionNames({ name: /demo/ });

또한 show collections 명령어를 사용하면 동일한 정보를 더 간단하게 확인할 수 있으니 상황에 맞게 활용하시기 바랍니다.