Updating multiple documents
Sometimes, multiple documents need the same update: when correcting an error or tagging records that meet certain conditions, for example. That's where .update_many()
comes in.
In this exercise, you'll use it to tag Oscar-winning movies that also have a rating above 7.0 as favorites.
This exercise is part of the course
Introduction to MongoDB in Python
Exercise instructions
- Define
query_filter
so it matches all movies where"won_oscar"
is equal toTrue,
and"rating"
is greater than7.0
. - Define
update
to$set
a new field"is_favorite"
toTrue
. - Perform the update for all documents that match
query_filter
. - Print out how many documents were affected by this update.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Filter for Oscar winning movies with rating greater than 7.0
query_filter = {
"____": ____,
"____": { "____": ____ }
}
# Update to set is_favorite to True
update = ____
# Perform update for all documents that match filter
res = ____
# How many documents were affected?
print(____)