๋ฐ˜์‘ํ˜•
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-11_mongoDB ์—ฐ๊ฒฐํ•˜๊ธฐ ๋ณธ๋ฌธ

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

3-11_mongoDB ์—ฐ๊ฒฐํ•˜๊ธฐ

๋ถ€์‹œ๋งค๋‚˜_HA 2023. 10. 16. 07:37
728x90
๋ฐ˜์‘ํ˜•

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


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

  • ์ง€๊ธˆ๊นŒ์ง€ ํ•œ๊ฑด, mongoDB ์—์„œ ๋นŒ๋ ค์ฃผ๋Š” Atlas ์ปดํ“จํ„ฐ๋ฅผ ์šฐ๋ฆฌ์ชฝ์œผ๋กœ ํ•˜๋‚˜ ๋ฐฐ์ •๋ฐ›์€ ๊ฒƒ.
  • ๊ทธ ์ปดํ“จํ„ฐ์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” id : test / pw : sparta ๋ฅผ ๋งŒ๋“ฆ
    • ๋‚˜๋Š” id : DJ_test / pw : ์ฉก๋”๊ธฐ์ผ๊ณต์˜ค๋Š๋‚Œ์Šค
  • ์ด๋ฒˆ์—” ํŒŒ์ด์ฌ์œผ๋กœ DB ์— ์ ‘์†ํ•˜๊ธฐ

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


1. ํ•ด๋ด

  • ์ ‘์†ํ•˜๋ ค๋ฉด ํ•„์š”ํ•œ ํŒจํ‚ค์ง€
    pymongo, dnspython
  • vs code ์— ์ด๊ฑฐ ์„ค์น˜ ํ•จ

pymongo ์„ค์น˜

pip3 install pymongo # for mac 
pip install pymongo # for window
  • pymongo ๋ฒ„์ „ ํ™•์ธ
    • ๋‚˜๋Š” pip list ์ด๊ฑธ๋กœ ํ™•์ธ
  • ๋‚˜๋Š” pip install pymongo

pymongo extension ์€ ์–ด์ผ€ ๋˜๋Š”๊ฑฐ์ง€โ“โ“โ“

dnspython ์„ค์น˜

pip install dnspython

db prac ํŒŒ์ผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ธฐ

์—ฌ๊ธฐ ์— pymongo ๊ธฐ๋ณธ์ฝ”๋“œ ๋„ฃ๊ธฐ

from pymongo import MongoClient
client = MongoClient('์—ฌ๊ธฐ์— URL ์ž…๋ ฅ')
db = client.dbsparta
  • ํŒŒ์ด์ฌ ๋ฒ„์ „ ์„ค์ •
  • URL ๋„ฃ๊ธฐ ๋ถ€๋ถ„์— ํ™ˆํŽ˜์ด์ง€์—์„œ ์ด ๋ถ€๋ถ„ ๋ณต์‚ฌํ•ด์„œ ๋„ฃ๊ธฐ
  • ์•„์ด๋””, ํŒจ์Šค์›Œ๋“œ ๋ถ€๋ถ„
  • ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ด๋ฆ„ > Cluster0

  • ์šฐ์„ , ์—ฌ๊ธฐ๊นŒ์ง€ ์™„์„ฑํ•จ โญโญโญโญโญ
    python3 
    import pymongo 
    from pymongo import MongoClient
    

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

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



#### ์œ„์— ๋งŒ๋“ ๊ฑฐ ๊ธฐ๋ฐ˜์œผ๋กœ db ์— ์˜ฌ๋ ค๋ณด๊ธฐ 


``` python 
from pymongo import MongoClient

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

doc = {
    'name' : 'bob',
    'age' : 27
}

db.users.insert_one(doc)
  • ๊ฒฐ๊ณผ๋ฌผ
  1. ๊ทธ๋Ÿฌ๋ฉด, vs code ์ƒ์—๋Š” ์ด๋ ‡๊ฒŒ ๋‚˜์˜ด
  1. mongoDB ์ƒ์—์„œ๋Š” browse collections ํด๋ฆญ

  • ๊ฒฐ๊ณผ๋ฌผ ํ•ด์„
    • ์—ฌ๊ธฐ ์žˆ๋Š” users ๋Š” ์ฝ”๋“œ์—์„œ ์ด ๋ถ€๋ถ„์ž„!
    • ์ด users ๋ฅผ '์ฝœ๋ ‰์…˜' ์ด๋ผ๊ณ  ์ฝ์Œ โญโญโญโญโญ
    • users ๋ผ๋Š” ์ฝœ๋ ‰์…˜ ์•ˆ์— bob, 27 ์ด ๋“ค์–ด๊ฐ!

728x90
๋ฐ˜์‘ํ˜•
Comments