Python

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

1703

목차

  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 



분류 제목
mongodb Python - MongoDB start (설치・연결)
mongodb Python - MongoDB Create Database (DB 생성)
mongodb Python - MongoDB Create Collection (컬렉션 생성)
mongodb Python - MongoDB Insert (데이터 삽입)
mongodb Python - MongoDB Find (데이터 찾기)
mongodb Python - MongoDB Query (검색 쿼리)
mongodb Python - MongoDB Sort (데이터 정렬)
mongodb Python - MongoDB Delete (데이터 삭제)
mongodb Python - MongoDB Drop Collection (컬렉션 삭제)
mongodb Python - MongoDB Update (데이터 수정)
mongodb Python - MongoDB Limit (데이터 출력개수)
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱닷컴 PC버전 로그인