JX405기_비트/mongoDB
Day17-3 몽고 디비와 Phyton 연동
_하루살이_
2023. 2. 8. 13:46
파이썬 설치후 주피터 설치
jupyter notebook --notebook-dir='E:\jupyter-notebook'
pymongo 설치하기
!pip install pymongo // pymongo 설치하기
mongoDB연결하기
import pymongo //모듈 불러오기
conn=pymongo.MongoClient() // 연결
db = conn.bitDB // bitDB데이터베이스 이름
users = db.users // 컬렉션
users = users.find({}).limit(3)
for user in users:
print(user)
read
// inventory에서 조회하기
inventory = db.inventory.find({},{'_id':0})
for inv in inventory:
print(inv)
insert
// 파이 몽고에서는 키는 쌍따옴표, '',""로 감쌀것
// 데이터를 추가한 다음에는 추가여부를 확인할 것
newInvent={"item":"speaker", "qty":50, "size" :{
"h":22, "w":22, "uom":"cm"}, "status" : "S"}
db.inventory.insert_one(newInvent)
newInvent=[{"item":"aaaaa", "qty":50, "size" :{
"h":22, "w":33, "uom":"cm"}, "status" : "SS"}]
db.inventory.insert_many(newInvent)
update
// update, 있는 값을 수정할 수도 있고 없는 키와 밸류를 추가 할 수 있음
db.inventory.update_many({"status":"SS"},{"$set":{"status":"P"}})