Python

[mongodb] Python - MongoDB Create Collection (컬렉션 생성)

1707

목차

  1. collection 생성
  2. collection 존재 체크

 

collection 생성

※ hz이라는 DB에 hz_member라는 컬렉션 생성.

 

import pymongo


myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['hz']

mycol = mydb["hz_member"]

 

※ 에러 없으면 정상적으로 생성된 것임.

※ 주의: MongoDB는 콘텐츠 가져올 때까지 Collection 생성 안 함.

※ 최초로 Collection 생성 시, 먼저 데이터를 넣어야 존재 체크 가능.

 

collection 존재 체크

[모든 collection 확인]

※ hz_level, hz_member 컬렉션 생성 후, 존재 확인.

 

import pymongo


myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['hz']

mycol = mydb["hz_member"]

mycol = mydb["hz_level"]

print(mydb.list_collection_names())

 

결과값: ['hz_level', 'hz_member']


[특정 collection 확인]

 

import pymongo


myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['hz']

mycol = mydb["hz_member"]

mycol = mydb["hz_level"]

collist = mydb.list_collection_names()

if "hz_member" in collist:

  print("hz_member 컬렉션 존재 O")

 

결과값: hz_member 컬렉션 존재 O 



분류 제목
basic Python - PIP (피프) - 패키지(모듈) 관리자
basic Python - Try...Except - 에러 테스트/제어. (= 트라이...익세프트)
basic Python - User Input (사용자 입력)
basic Python - String Format (문자열 포맷)
file Python - File Open - 파일열기
file Python - File Read - 파일읽기
file Python - File Write - 파일쓰기
file Python - Delete File (파일삭제)
mysql Python - MySQL Start (DB 사용)
mysql Python - MySQL Create Database (DB 생성)
mysql Python - MySQL Create Table (테이블 생성)
mysql Python - MySQL Insert Into Table (데이터 삽입)
mysql Python - MySQL Select From (데이터 선택)
mysql Python - MySQL Where (조건절)
mysql Python - MySQL Order By (정렬순서)
3/32
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱닷컴 PC버전 로그인