Last active
August 2, 2019 10:51
-
-
Save amitrani6/76d5847fc39019db5ec7142d3a80d8b9 to your computer and use it in GitHub Desktop.
Updates a document in Mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This block of code updates each television in the Mongo collection with | |
#the product information found on its specific page, which is accessed | |
#by the url obtained earlier | |
#This query obtains each url key-value pair in the collection | |
#The key-value pair is the unique identifier for the televisions | |
#I do not need the '_id' field so I set it to 0 | |
url_query = product_info_collection.find({}, {'_id': 0, 'url': 1}) | |
#This for loop iterates through each document found in the query | |
for item in url_query: | |
#Obtains the product information dictionary | |
product_info_dictionary = obtain_product_info_dict(item['url']) | |
#Creates a variable to hold the dictionary in the format required by | |
#the update_many() method | |
update = {'$set': product_info_dictionary} | |
#Updates the collection | |
#'item' is the filter dictionary variable, in this case the url key-value pair | |
#'update' is the product information dictionary obtained from the function | |
#described above | |
product_info_collection.update_many(item, update) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment