I have some documents in my MongoDB collection, i want to update those documents with the newest values retrieved from an API.
Here is what i tried:
def Updating(Data):
for x in Data():
Item = Data['item']
Price = Data['price']
update = db.col.update_many({}, {"$set":{"item": Item, "price": "Price"}})
Here Data
is the data retrieved from the API. Each document should be updated with the new value, so if i have:
item: First item
price: 394
item: Second item
price: 20
It should be updated with the new price, for example like this:
item: First item
price: 423
item: Second item
price: 25
The problem with my actual code is that all the documents in the collection are being updated with the same thing, just like a copy and paste applied to all the records, like this:
item: First item
price: 423
item: First item
price: 423
item: First item
price: 394
I don't understand why is this happening. I tried using update_one
and replace_one
but both of them will only update one document in the entire collection.