코딩과 결혼합니다

mongoDB 본문

2세/Python

mongoDB

코딩러버 2023. 5. 10. 14:03
728x90

웹개발 종합반 3주차 - mongoDB

 

 

 

선행

pip install dnspython

pip install pymongo

 

from pymongo import MongoClient
client = MongoClient('여기에 URL 입력')
db = client.dbsparta

 

url 가져오기

로그인 후 ➡️ Connect ➡️ Connect to your application ➡️ Driver-Python , Version-3.6 or later

                ➡️ url 복사

 

다음으로 url 안에 <passworld>부분을 내가 가입할때 입력했던 패스워드로 입력

from pymongo import MongoClient
client = MongoClient('mongodb+srv://sparta:<passworld>@cluster0.rgsaqvn.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

 

 

 

데이터 입력

doc = {
    'name':'영수',
    'age':24
}
db.uesers.insert_one(doc)

결과는 터미널에서 나타나지 않고 db에 저장이 된다.

데이터를 몇 개 더 추가

 

 

 

 

 

# 저장 - 예시
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)

# 한 개 찾기 - 예시
user = db.users.find_one({'name':'bobby'})

# 여러개 찾기 - 예시 ( _id 값은 제외하고 출력)
all_users = list(db.users.find({},{'_id':False}))

# 바꾸기 - 예시
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

# 지우기 - 예시
db.users.delete_one({'name':'bobby'})

'2세 > Python' 카테고리의 다른 글

Python 기초 문법 - 리스트  (0) 2023.05.10
3주차 숙제 (웹스크래핑)  (0) 2023.05.10
웹스크래핑(크롤링)  (0) 2023.05.08
requests 라이브러리  (0) 2023.05.08
python 외부 라이브러리 사용  (0) 2023.05.08