Skip to main content

Command Palette

Search for a command to run...

Week3 Quiz2.

Published
2 min readView as Markdown

For this quiz, create a file named dbmovie.py and start with the code below.

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

Quiz1. What's the review point of '가버나움'?

image.png

when looking for a certain data, use user = db.users.find_one({'name':'bobby'})

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

movie = db.movies.find_one({'title':'가버나움'})
print(movie)

image.png

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

movie = db.movies.find_one({'title':'가버나움'})
print(movie['star'])

image.png


Quiz2. Show me all the movie titles that have the same review score as 가버나움.

image.png

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

movie = db.movies.find_one({'title':'가버나움'})
star = movie['star']
  • To find all data, use all_users = list(db.users.find({},{'_id':False}))

all_movies = list(db.movies.find({'star':star},{'_id':False}))

  • use for statement and print.
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

movie = db.movies.find_one({'title':'가버나움'})
star = movie['star']

all_movies = list(db.movies.find({'star':star},{'_id':False}))

for m in all_movies:
    print(m)

image.png

  • As a result, we want to see only the titles.
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

movie = db.movies.find_one({'title':'가버나움'})
star = movie['star']

all_movies = list(db.movies.find({'star':star},{'_id':False}))

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

image.png


Quiz3. Make the review point of 가버나움 0.

image.png

  • To update data, use db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

db.movies.update_one({'title':'가버나움'},{'$set':{'star':'0'}})

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

db.movies.update_one({'title':'가버나움'},{'$set':{'star':'0'}})

More from this blog

Ollie Seongyong Kim

85 posts