๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

3-12_pymongo๋กœ DB์กฐ์ž‘ํ•˜๊ธฐ ๋ณธ๋ฌธ

[WEB & AI] (feat. ์ทจ์ค€)/์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ_์›น๊ฐœ๋ฐœ์ข…ํ•ฉ๋ฐ˜

3-12_pymongo๋กœ DB์กฐ์ž‘ํ•˜๊ธฐ

๋ถ€์‹œ๋งค๋‚˜_HA 2023. 11. 26. 21:50
728x90
๋ฐ˜์‘ํ˜•

โ… . ์ด๋ฒˆ ํšŒ์ฐจ ํ•™์Šต ๋ชฉํ‘œ (goal)


1. ๋ฐฐ์šฐ๊ณ ์ž ํ•˜๋Š” ๊ฒƒ

  • ๋ณธ๊ฒฉ์ ์œผ๋กœ mongoDB ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ
  • database ์— ๋ฐ์ดํ„ฐ๋ฅผ 1) ๋„ฃ๊ฑฐ๋‚˜ 2) ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ 3) ์ˆ˜์ • ํ•˜๊ฑฐ๋‚˜ 4) ์‚ญ์ œํ•˜๋Š” ๋ฒ•



โ…ก. ํ•ด๋ณด๊ธฐ


1. ๋ฐ์ดํ„ฐ ๋„ฃ๋Š” ๊ฒƒ

  • ๋Œ์•„๊ฐ€๊ธด ํ•จ.
    • ๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ๋Š” ์•ˆ ์”€ โญโญโญโญโญ
      from pymongo import MongoClient
      

mongoDB ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ๋ถ€๋ถ„ โœ…

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

[1์ฐจ] ์šฐ์„  ๋Œ์•„๊ฐ€๋Š” ์ฝ”๋“œ

doc = {

'name' : 'bob',

'age' : 27

}

db.users.insert_one(doc)

[2์ฐจ] ๋””๋ฒจ๋กญํ•œ ๋ถ€๋ถ„ : ๋ฐ˜๋ณต์„ ์ค„์ž„

db.users.insert_one({'name' : 'lion', 'age' : 100})
db.users.insert_one({'name' : 'deoks', 'age' : 20})
db.users.insert_one({'name' : 'jin', 'age' : 30})


- ๊ฒฐ๊ณผ๋ฌผ 
    - ์ž˜ ๋“ค์–ด๊ฐ€๊ณ  ์žˆ์Œ ![](https://i.imgur.com/WHoSNNR.png)


### ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ๋ณด๊ธฐ 


- users ๋Š” ์ฝœ๋ ‰์…˜์˜ users ์ž„! 
![](https://i.imgur.com/j34195K.png)

- ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ 
``` python 
from pymongo import MongoClient

# mongoDB ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ๋ถ€๋ถ„ โœ… 
client = MongoClient('mongodb+srv://test:sparta@cluster0.qsaohhz.mongodb.net/?retryWrites=true&w=majority')  
db = client.dbsparta

# ์กฐ๊ฑด์—†์ด, ๋‹ค ๋ณด๊ธฐ
all_users = list(db.users.find({}))

for user in all_users:
    print(user)
  • ๊ฒฐ๊ณผ๋ฌผ
  • ํ•ด์„
    id': ObjectId('63db56f72acd5be82bab92a8') : ๋ชฝ๊ณ DB ๊ฐ€ ์Šค์Šค๋กœ, ์ž๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„

  • id ์—†์ด ๋ณด๊ธฐ = ํ†ต์ƒ์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ๋ด„ โญโญโญ

    from pymongo import MongoClient
    

mongoDB ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ๋ถ€๋ถ„ โœ…

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

์กฐ๊ฑด์—†์ด, ๋‹ค ๋ณด๊ธฐ

all_users = list(db.users.find({}))

id ์ƒ์„ฑ์—†์ด, ๋ณด๊ธฐ

all_users = list(db.users.find({}, {'_id' : False}))

for user in all_users:
print(user)

![](https://i.imgur.com/S1KwgRy.png)



- bob ํ•˜๋‚˜๋งŒ ์ฐพ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ 
``` python 
from pymongo import MongoClient

# mongoDB ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ๋ถ€๋ถ„ โœ… 
client = MongoClient('mongodb+srv://test:sparta@cluster0.qsaohhz.mongodb.net/?retryWrites=true&w=majority')  
db = client.dbsparta

# ์กฐ๊ฑด์—†์ด, ๋‹ค ๋ณด๊ธฐ
# all_users = list(db.users.find({}))

# # id ์ƒ์„ฑ์—†์ด, ๋ณด๊ธฐ 
# all_users = list(db.users.find({}, {'_id' : False}))

# for user in all_users:
#     print(user)

# ์ด๋ฆ„์ด bob ์ธ๊ฑฐ, ํ•˜๋‚˜๋งŒ! ์ฐพ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ 
user = db.users.find_one({'name' : 'bob'})
print(user)
print(user['age']) #bob ์˜ age ๋งŒ ๋‚˜์˜ค๊ฒŒ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

์ˆ˜์ •ํ•ด๋ณด๊ธฐ

  • ์ฝ”๋“œ ์Šค๋‹ˆํŽซ

    db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
  • ํ•ด์„
    users.update_one : user ๋ผ๋Š” ์ฝœ๋ ‰์…˜์œผ๋กœ ๊ฐ€์„œ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๋‚˜ ํ•œ๋‹ค.
    ({'name':'bobby'},{'$set':{'age':19}}) : ์—…๋ฐ์ดํŠธ ์กฐ๊ฑด์€? 1) name ์ด bobby ์ธ ์• ๋ฅผ ์ฐพ์•„ 2) ๊ทธ ๋†ˆ์˜ age ๋ฅผ 19 ๋กœ ๋งŒ๋“ค์–ด~!

  • ๊ฒฐ๊ณผ๋ฌผ

๋ฐ”๋€Œ์–ด ์žˆ๋„ค!!!!!!!!!!!!!!!! ์™„์ „ ์‹ ๊ธฐํ•˜๋„ค!!!!!!!!!!!!!!

์‚ญ์ œํ•˜๊ธฐ

  • ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ
    db.users.delete_one({'name':'bob'})

์ฝ๊ธฐ
users.delete_one : user ๋ผ๋Š” ์ฝœ๋ ‰์…˜์œผ๋กœ ๊ฐ€์„œ 'ํ•˜๋‚˜๋ฅผ ์ง€์šธ๊ฑฐ์•ผ' ๋ญ˜ ์ง€์šธ๊ฑด๋ฐ? ์ง€์šฐ๋Š” ์กฐ๊ฑด์€?
({'name':'bob'}) ์ด๊ฑธ ์ง€์šธ๊ฑฐ์•ผ

  • ๊ฒฐ๊ณผ๋ฌผ

์—†์–ด์ง
๊ทธ๋Ÿฌ๋ฉด, ์ง€์šธ ๋Œ€์ƒ์œผ๋กœ ์ ์€๊ฒŒ ์—†์–ด์ง~!

  • ๋‹ค๋งŒ, ์ž˜ ์•ˆ ์“ฐ๊ธด ํ•จ

pymongo ์ฝ”๋“œ ์š”์•ฝ

  • ์ฝ”๋“œ ์Šค๋‹ˆํŽซ
    # ์ €์žฅ - ์˜ˆ์‹œ
    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'})



<br>
<br>
728x90
๋ฐ˜์‘ํ˜•
Comments