티스토리 뷰
728x90
반응형
파이썬에서 mongoDB 연결해서 모든 데이터베이스명과 컬렉션명 출력하는 방법!
MongoClient에서 host에 ip를 입력하고, 데이터베이스 로그인할 username과 password를 입력하여 연결한 후,
데이터베이스명과 데이터베이스의 컬렉션명을 모두 추출할 수 있다
from pymongo import MongoClient
mg = MongoClient(host="ip", port="27017", username="username", password="password", authSource="admin") # DB연결
database_names = mg.list_database_names() #데이터베이스명 리스트
for db_num, db in enumerate(database_names):
print(mg[db].list_collection_names()) #데이터베이스의 컬렉션명 모두 출력
#python #mongo #db #mongodb #mongoclient #collection #데이터베이스 #컬렉션 #출력
728x90
반응형
'프로그래밍 > Python' 카테고리의 다른 글
[오류해결] raise OSError(f"cannot write mode {im.mode} as JPEG") from eOSError: cannot write mode RGBA as JPEG (0) | 2024.07.02 |
---|---|
[python] 현재 디렉토리에 있는 이미지파일 목록 리스트로 가져와서 읽기 (0) | 2024.06.27 |
[Python] 파이썬에서 진행률(progress 바) 출력하는 방법 (42) | 2024.01.08 |
python3.x 원하는 버전의 pip 설치하기 (0) | 2023.09.01 |
[mongoDB] 맥북에 몽고DB 설치하는 방법 (1) | 2023.07.14 |
댓글