IT 프로그래밍/빅데이터

[Bigdata] python과 MongoDB연결 (+DeprecationWarning)

잉휴 2021. 3. 10. 17:46

1단계. python과 mongoDB연결하기

 

클라이언트와 서버cmd창을 모두 열어놓고,

pip install pymongo하기 (실행하고 닫아도됨)

 

기본적인 작업은
jupyter notebook로 실행->cmd로 확인작업

이렇게 합니다

from pymongo import MongoClient
client=MongoClient('localhost',27017)

jupyter에 MongoClient와 포트(27017)를 연결했으면

db와 테이블을 collection에 담아서 잘 연결되었는지 확인해줍니다. (생략)

이 작업까지 끝나면 도큐먼트들을 insert해보겠습니다.


db=client['test']
col=db['student']
s1={'sno':1,'name':'홍길동','major':'컴공'}
col.insert(s1)

오류가 뜰 수 있지만 ObjectId가 뜨면 정상적으로 insert 되었다는 뜻입니다. ↓

 

stud=col.find()
for s in stud:
    print(s)

하고 client콘솔로 가서

use test
show collections 
db.student.find()

 

show collections로 먼저 student테이블이 있는지 확인하고

find()를 하면 jupyter에서 실행한 내용과 같은 내용이 출력됩니다.

 

item을 insert, update,remove하는 작업도 주피터노트북을 통해 실행하면 변경된 내용이 

처리되었음을 cmd에서 확인할 수 있습니다