파이썬으로 몽고DB에 데이터를 입력하거나, 검색, 업데이트, 삭제를 수행할 수 있다.
MongoDB의 Python용 드라이버는 pymongo 이다.
STEP1. pymongo 드라이버 설치
c:\> pip install pymongo |
STEP2. pymongo 이용하여 MongoDB에 연결하기
import pymongo connection = pymongo.MongoClient("localhost",27017) |
STEP3. MongoDB에 데이터 삽입
db = connection.TestDB collection = db.testCollection collection.insert({"이름":"홍길동"}); |
데이터 업데이트 예제 코드
import pymongo connection = pymongo.MongoClient("localhost",27017) db = connection.TestDB collection = db.testCollection collection.insert({"이름":"홍길동"}) collectionInfo = db.collection_names() print collectionInfo collection.update({"이름":"홍길동"},{"이름":"이순신"}) collectionInfo = db.collection_names() print collectionInfo
|
데이터 검색 예제 코드
import pymongo connection = pymongo.MongoClient("localhost",27017) db = connection.TestDB collection = db.testCollection collection.insert({"이름":"홍길동", "나이": 10}) collection.insert({"이름":"이순신", "나이": 20}) collection.insert({"이름":"강감찬","나이":30}) collection.insert({"이름":"권율","나이":40}) document = collection.find({"이름":"홍길동"}) for i in document: print ( i ) |
'4차산업 > 빅데이터' 카테고리의 다른 글
NoSQL 선택시 고려사항 (0) | 2018.01.03 |
---|---|
[몽고DB] 몽고DB시작하기 (0) | 2017.12.19 |
[Google] Making Sense of Data ____ Unit 3. Finding Patterns and Relationships in Data (0) | 2014.04.07 |
[Google] Making Sense of Data ____ Unit 2. Answering Basic Questions with Fusion Table (0) | 2014.04.07 |
[Google] Making Sense of Data ____ Unit 1. Introducing the Data Process (0) | 2014.04.07 |