๋ฐ˜์‘ํ˜•
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-14_ Quiz_์›น์Šคํฌ๋ž˜ํ•‘ ๊ฒฐ๊ณผ ์ด์šฉํ•˜๊ธฐ ๋ณธ๋ฌธ

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

3-14_ Quiz_์›น์Šคํฌ๋ž˜ํ•‘ ๊ฒฐ๊ณผ ์ด์šฉํ•˜๊ธฐ

๋ถ€์‹œ๋งค๋‚˜_HA 2023. 11. 28. 07:34
728x90
๋ฐ˜์‘ํ˜•

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


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

  • ๊ฐ€๋ฒ„๋‚˜์›€ ํ‰์  ๊ฐ€์ ธ์˜ค๊ธฐ
  • ๊ฐ€๋ฒ„๋‚˜์›€๊ณผ ํ‰์  ๋™์ผํ•œ ์˜ํ™” ์ฐพ๊ธฐ

  • ๊ฐ€๋ฒ„๋‚˜์›€ ํ‰์ ์„ '0' ์œผ๋กœ ๋งŒ๋“ค๊ธฐ



โ…ก. ํ€ด์ฆˆ ํ’€์–ด๋ณด๊ธฐ


1. ๊ฐ€๋ฒ„๋‚˜์›€ ํ‰์  ๊ฐ€์ ธ์˜ค๊ธฐ

  • ์šฐ์„  ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ
from pymongo import MongoClient

# MongoDB ์—์„œ, 'ID:test PW:sparta, cluster0, ํ•ด๋‹น ์ฃผ์†Œ'๋กœ ๋“ค์–ด๊ฐ.
    # 'qsaohhz.mongodb.net/?retryWrites=true&w=majority' ์ด ์ฃผ์†Œ๋Š” ์…‹ํŒ…ํ•  ๋•Œ ๋‚˜์™”๋Š”๋ฐ ๋ณต์Šต์‹œ ๋‹ค์‹œ ํ™•์ธ 
client = MongoClient('mongodb+srv://test:sparta@cluster0.qsaohhz.mongodb.net/?retryWrites=true&w=majority')   

# cluster0 > dbsparta ๋ผ๋Š” ํ”„๋กœ์ ํŠธ(?)๋กœ ์ ‘๊ทผ
db = client.dbsparta

# cluster0 > dbsparta(ํ”„๋กœ์ ํŠธ) > movies(์ฝœ๋ ‰์…˜) ์ ‘๊ทผ
movie = db.movies.find_one({'title' : '๊ฐ€๋ฒ„๋‚˜์›€'})
print(movie)
print(movie['star'])     # user ๋”•์…”๋„ˆ๋ฆฌ ์ค‘ 'star' key ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

2. ๊ฐ€๋ฒ„๋‚˜์›€๊ณผ ํ‰์ ์ด ๊ฐ™์€ ์˜ํ™” ๊ฐ€์ ธ์˜ค๊ธฐ

  • ์šฐ์„  ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ
    from pymongo import MongoClient
    

MongoDB ์—์„œ, 'ID:test PW:sparta, cluster0, ํ•ด๋‹น ์ฃผ์†Œ'๋กœ ๋“ค์–ด๊ฐ.

# 'qsaohhz.mongodb.net/?retryWrites=true&w=majority' ์ด ์ฃผ์†Œ๋Š” ์…‹ํŒ…ํ•  ๋•Œ ๋‚˜์™”๋Š”๋ฐ ๋ณต์Šต์‹œ ๋‹ค์‹œ ํ™•์ธ 

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

cluster0 > dbsparta ๋ผ๋Š” ํ”„๋กœ์ ํŠธ(?)๋กœ ์ ‘๊ทผ

db = client.dbsparta

cluster0 > dbsparta(ํ”„๋กœ์ ํŠธ) > movies(์ฝœ๋ ‰์…˜) ์ ‘๊ทผ

์กฐ๊ฑด์ด ๋งž๋Š” ๊ฐ’ ์ฐพ์•„์˜ค๊ธฐ

all_movies= list(db.movies.find({'star' : '9.59'}, {'_id' : False})) # _id ์ด๋ ‡๊ฒŒ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋‚˜์˜ค๋Š”๊ฑฐ ์—†์ด ๋ณด๊ธฐ

for movie in all_movies:
print(movie['title'])


- ํŠœํ„ฐ๋‹˜ ์ฝ”๋“œ (better design)
``` python 
from pymongo import MongoClient

# MongoDB ์—์„œ, 'ID:test PW:sparta, cluster0, ํ•ด๋‹น ์ฃผ์†Œ'๋กœ ๋“ค์–ด๊ฐ.
    # 'qsaohhz.mongodb.net/?retryWrites=true&w=majority' ์ด ์ฃผ์†Œ๋Š” ์…‹ํŒ…ํ•  ๋•Œ ๋‚˜์™”๋Š”๋ฐ ๋ณต์Šต์‹œ ๋‹ค์‹œ ํ™•์ธ 
client = MongoClient('mongodb+srv://test:sparta@cluster0.qsaohhz.mongodb.net/?retryWrites=true&w=majority')   

# cluster0 > dbsparta ๋ผ๋Š” ํ”„๋กœ์ ํŠธ(?)๋กœ ์ ‘๊ทผ
db = client.dbsparta

# cluster0 > dbsparta(ํ”„๋กœ์ ํŠธ) > movies(์ฝœ๋ ‰์…˜) ์ ‘๊ทผ

# ๊ฐ€๋ฒ„๋‚˜์›€ ํ‰์  ๊ฐ€์ ธ์™€์„œ star ์— ๋„ฃ๊ธฐ (# WIL๐Ÿ’ช)
movie = db.movies.find_one({'title' : '๊ฐ€๋ฒ„๋‚˜์›€'})
star = movie['star']

# ์กฐ๊ฑด์ด ๋งž๋Š” ๊ฐ’ ์ฐพ์•„์˜ค๊ธฐ  (# WIL๐Ÿ’ช)
all_movies= list(db.movies.find({'star' : star}, {'_id' : False}))    # _id ์ด๋ ‡๊ฒŒ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋‚˜์˜ค๋Š”๊ฑฐ ์—†์ด ๋ณด๊ธฐ 

for m in all_movies: (# WIL๐Ÿ’ช)
    print(m['title'])
  • ๋ฐฐ์šด์ 
    • ํ‰์ ์„ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ณ , ๋ณ€์ˆ˜์— ๋„ฃ์–ด์„œ, ์ž‘๋™์‹œํ‚จ๋‹ค๋Š” ๊ฒƒ โญโญโญโญโญ
    • ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค๋ ค๋ฉด, 'star' ๋ผ๋Š” key ์˜ ๊ฐ’์ด ๋ณ€์ˆ˜ star ์— ํ•ด๋‹นํ•˜๋ฉด ๊ฐ€์ ธ์™€๋ผ! (๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์กฐ๊ฑด์„ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Œ.) โญโญโญ
    • list ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ๋ณด๋ ค๋ฉด, ๊ทธ๋ƒฅ print(๋ณ€์ˆ˜) ํ•˜๋ฉด ๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์„œ ํ•˜๋‚˜์”ฉ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋ฐฐ์› ์Œ. โญโญโญ


3. ๊ฐ€๋ฒ„๋‚˜์›€ ํ‰์ ์„ 0 ์œผ๋กœ ๋งŒ๋“ค๊ธฐ

  • ์ฃผ์˜ํ•  ๊ฒƒ
    • '0' ์ด ์ˆซ์žํ˜•์ด ์•„๋‹ˆ๋ผ, '๋ฌธ์ž์—ด!' ์ž„์„ ์ฃผ์˜

  • ์šฐ์„  ๋Œ์•„๊ฐ€๋Š” ์ฝ”๋“œ
    from pymongo import MongoClient
    

MongoDB ์—์„œ, 'ID:test PW:sparta, cluster0, ํ•ด๋‹น ์ฃผ์†Œ'๋กœ ๋“ค์–ด๊ฐ.

# 'qsaohhz.mongodb.net/?retryWrites=true&w=majority' ์ด ์ฃผ์†Œ๋Š” ์…‹ํŒ…ํ•  ๋•Œ ๋‚˜์™”๋Š”๋ฐ ๋ณต์Šต์‹œ ๋‹ค์‹œ ํ™•์ธ 

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

cluster0 > dbsparta ๋ผ๋Š” ํ”„๋กœ์ ํŠธ(?)๋กœ ์ ‘๊ทผ

db = client.dbsparta

cluster0 > dbsparta(ํ”„๋กœ์ ํŠธ) > movies(์ฝœ๋ ‰์…˜) ์ ‘๊ทผ

๊ฐ’ ์ˆ˜์ •ํ•˜๊ณ  > ๊ฐ’ ๋ณ€๊ฒฝ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

db.movies.update_one({'title':'๊ฐ€๋ฒ„๋‚˜์›€'},{'$set':{'star':'0'}})

movie = db.movies.find_one({'title' : '๊ฐ€๋ฒ„๋‚˜์›€'})
star = movie['star']
print(star)


- ํŠœํ„ฐ๋‹˜ ์ฝ”๋“œ
``` python 
from pymongo import MongoClient

# MongoDB ์—์„œ, 'ID:test PW:sparta, cluster0, ํ•ด๋‹น ์ฃผ์†Œ'๋กœ ๋“ค์–ด๊ฐ.
    # 'qsaohhz.mongodb.net/?retryWrites=true&w=majority' ์ด ์ฃผ์†Œ๋Š” ์…‹ํŒ…ํ•  ๋•Œ ๋‚˜์™”๋Š”๋ฐ ๋ณต์Šต์‹œ ๋‹ค์‹œ ํ™•์ธ 
client = MongoClient('mongodb+srv://test:sparta@cluster0.qsaohhz.mongodb.net/?retryWrites=true&w=majority')   

# cluster0 > dbsparta ๋ผ๋Š” ํ”„๋กœ์ ํŠธ(?)๋กœ ์ ‘๊ทผ
db = client.dbsparta

# cluster0 > dbsparta(ํ”„๋กœ์ ํŠธ) > movies(์ฝœ๋ ‰์…˜) ์ ‘๊ทผ

# ๊ฐ’ ์ˆ˜์ •ํ•˜๊ณ  > ๊ฐ’ ๋ณ€๊ฒฝ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ 
db.movies.update_one({'title':'๊ฐ€๋ฒ„๋‚˜์›€'},{'$set':{'star':'0'}})
  • ๊ทธ๋ƒฅ, ๊ฐ’ ํ™•์ธ ๊ณผ์ • ์—†์ด ๊น”๋”.

  • ๊ฒฐ๊ณผ๋ฌผ

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