๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

DB์—์„œ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์ฝ”๋“œ์ฐธ๊ณ  ๋ณธ๋ฌธ

Deveolpment Study๐Ÿ—‚๏ธ/Databases

DB์—์„œ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์ฝ”๋“œ์ฐธ๊ณ 

bompeach 2022. 6. 17. 10:58

/์ฝ”๋“œ ์ฐธ๊ณ ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ/

 

app.py(์„œ๋ฒ„)์— ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ. (html์•„๋‹˜)

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

# ์ €์žฅ - ์˜ˆ์‹œ
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'})