how to update mongo using a dataframe for multiple documents

Daiyue Weng daiyueweng at gmail.com
Fri Sep 1 07:33:45 EDT 2017


Hi, I am trying to batch update mongo using a DataFrame,

df_dict = df[['id', 'name']].to_dict(orient='records')
[{'name': 'vendor1', 'id': '1'},
 {'name': 'vendor2', 'id': '2'},
 {'name': 'vendor3', 'id': '3'},
 {'name': 'vendor4', 'id': '4'},
 {'name': 'vendor5', 'id': '5'},
 {'name': 'vendor6', 'id': '6'},]
def update_database(update_dicts, table, database):
    for row_dict in update_dicts:
        database[table].update_one({'id': row_dict['id']},
                                {'$set': 'name': row_dict['name']})

when doing updating, pass df_dict to update_database function as the
argument for update_dicts; I am wondering is there a way to update mongo in
one go that doesn't have to go through the for loop and update_one document
at a time.

I am thinking if I don't go through the loop, it will be more efficient for
updating mongo.

I know there a Bulk.find.update() operation, one has to loop through the
list of dicts in order to initialize the Bulk() operation for each document
(using find) in order to update.

cheers



More information about the Python-list mailing list