简体   繁体   中英

Update MongoDB collection by matching _id

I am trying to update mongodb collection from Python using condition on _id

Like if I found match of _id in python dataframe I need to update corresponding document in colllection Below script is working, but it takes time for exeution if there are too many document is there any efficient way to handle this. Please advice

for document in db.AMTicketData.find():
    for index, row in AMTicketData1.iterrows():
        if(row['_id']==a['_id']):
            db.AMTicketData.update_one({'_id': row['_id']},{'$set': {'Application_Name': row['Application_Name']}}, upsert=True)
            break

I have used below bulk operation codes, was able to update collection in bulk

bulk = db.AMTicketData.initialize_unordered_bulk_op()
for index, row in AMTicketData1.iterrows():
    bulk.find({'_id':row['_id']}).update_one({'$set':{'Application_Name':row['Application_Name']}})

bulk.execute()

You could try using bulk write. You just have to create an array with all updates and apply it once with collection.bulk_write(list_of_updates)

Something like:

updates = []
for document in db.AMTicketData.find():
   for index, row in AMTicketData1.iterrows():
     if(row['_id']==a['_id']):
      updates.append(UpdateOne({'_id': row['_id']}, {'$set': {'Application_Name': row['Application_Name']}}, upsert=True)
      break

db.AMTicketData.bulk_write(updates)

https://docs.mongodb.com/manual/core/bulk-write-operations/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM