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.
Diese Übung ist Teil des Kurses
Introduction to MongoDB in Python
Anleitung zur Übung
- 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.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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(____)