Skip to content

Instantly share code, notes, and snippets.

@amitrani6
Last active August 2, 2019 10:51
Show Gist options
  • Save amitrani6/76d5847fc39019db5ec7142d3a80d8b9 to your computer and use it in GitHub Desktop.
Save amitrani6/76d5847fc39019db5ec7142d3a80d8b9 to your computer and use it in GitHub Desktop.
Updates a document in Mongo
#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