Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

python - Pymongo and updating records

I have a dataset that I am pulling from an API. The dataset contains fields such as store_id, store_description, monthly_sales, total_sales.

There are 16,219 records in this dataset

I would like to automate the pulling of this data but when I pull from the API more than once I get duplicate records of the data instead of each record being updated. Below is the code I am using to update the data:

for i in json.loads(data):
    for j in col.find({}):
        if i['store_id'] == j['store_id']:
            col.update_one({j}, {i})
        else:
            col.insert_one({i})

I am not really sure what exaclty I am doing wrong here. I would appreciate any help.

question from:https://stackoverflow.com/questions/65876192/pymongo-and-updating-records

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your aim is to match on a key of store_id and update the entirety of the record if it matches an existing record, or insert it if it doesn't, then use replace_one() with upsert=True, i.e.:

col.replace_one({'store_id': i['store_id']}, i, upsert=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...