my homework file 1 #9
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
import pymongo
from pymongo import MongoClient
client = MongoClient()
database = client.test
collection = database.movies
cursor = collection.find({"genres": "Drama", "rated": "NOT RATED"})
number = collection.find({"genres": "Drama"}).count()
print('before updating: %d' %(number))
#A
#update database
for doc in cursor:
collection.update({"_id": doc["_id"]},{'$set':{"rated":"Pending rating"}})
#check status
number = collection.find({"genres": "Drama", "rated": "NOT RATED"}).count()
print('after updating: %d' %(number))
#B
collection.insert_one({"title": "Baby Boom","year": 1987, "countries": ["United States"],"genres": ["Drama", "Comedy", "Romance"], "directors": ["Charles Shyer"], "imdb": {"id":8888888, "rating":10.0, "votes":152}})
collection.find({"title" : "Your mom boom", "genres" : "Drama"})
#C
collection.aggregate([{ '$match' : {'genres' : "Drama"} }, { '$group' : {'_id': "Drama", 'count': {'$sum': 1}}}])
#D
result = collection.aggregate([{'$match':{"countries" : "Hungary", "rated" : "Pending rating"}},{'$group':{"count":{'$sum': 1}, "_id" : {"countries" : "Hungary", "rating": "Pending rating"}}}])
print(list(result))
#E
database.test2.insert({'name':'Liuqing Yang', 'age':'22'})
database.test3.insert({'name':'Liuqing Yang', 'age':'22'})
database.test2.insert({'name':'Liuqing Yang', 'age':'22'})
database.test3.insert({'name':'Liuqing Yang', 'age':'22'})
result = database.test2.aggregate([{'$lookup':{'from': 'test3', 'localField':'name', 'foreignField': 'name', 'as': 'result'}}])
print(list(result))