Dheeraj
Dheeraj

Reputation: 11

Want to Use UpdateMany instead of UpdateOne for syncing MongoDB Collection

I am a new user of MongoDB. I am working with a large amount of transaction data which is updated periodically after each day or week. I created my code for this use where I am using pymongo.UpdateOne but as I have a large amount of data, I want to use UpdateMany. Here’s a snippet of my code:

client = MongoClient()
database = client['Eg']
collection = database['eg']
start = datetime.now()

df = pd.read_csv("eg.csv")
df['_id'] = df["Factory_Id"] + df["Order ID"]
data = df.to_dict(orient="records")

requests = []
for doc in data:
    filter = {'_id': doc['_id']}
    update = {'$set': doc}
    request = pymongo.UpdateOne(filter, update, upsert=True)
    requests.append(request)

if requests:
    result = collection.bulk_write(requests)

Here I am using UpdateOne for syncing but I want it to sync once and not use a for-loop with the help of UpdateMany.

Upvotes: 1

Views: 111

Answers (1)

Belly Buster
Belly Buster

Reputation: 8834

You can't use UpdateMany in your example, as each filter is different for each update; UpdateMany only works where you are applying the same filter for all the updates.

As you are already using bulk_write, there isn't a performance benefit to be gained anyway.

Upvotes: 1

Related Questions