1

Is it possible to update multiple records in MongoDB collection from dataframe by matching id?

dataframe

    _id                                      text                            sentiment
0   5ec299fa905e038dec3c8e93    Kederi Yusof· basikal salah Najib.Tayar pa...   1
1   5ec49452bfcd4786382fe21f    Serindik.com·2 laaa mimpi UMNO.... XPM7 pul...  0
2   5ec40e8d28fb32986041df16    Newpaper24·4m1MDB: Najib Razak’s court, accuse...   -1
3   5ec44c0b255995f0522fe1ec    falseprophet· low pesuruh najib. Budak des...   1
4   5ed2ab347d23a5d56d59a730    Kamaluddin 阿列克斯 தீன்·11m-anak-najib-dalam-sena...   0

code

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

db.twitter.bulk_write(updates)

But I got AttributeError: 'dict' object has no attribute 'iterrows' error message

Nurdin
  • 23,382
  • 43
  • 130
  • 308

1 Answers1

3

You don't need the find loop, just use iterrows() to get the data and UpdateOne to perform the upsert.

from pymongo import MongoClient, UpdateOne
import pandas as pd

db = MongoClient()['mydatabase']

data = [['ec299fa905e038dec3c8e93', 'Kederi Yusof· basikal salah Najib.Tayar pa...', 1],
        ['ec49452bfcd4786382fe21f', 'Serindik.com·2 laaa mimpi UMNO.... XPM7 pul...', 0]]

df = pd.DataFrame(data, columns=['_id', 'text', 'sentiment'])

updates = []

for _, row in df.iterrows():
    updates.append(UpdateOne({'_id': row.get('_id')}, {'$set': {'sentiment': row.get('sentiment')}}, upsert=True))

db.twitter.bulk_write(updates)
Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • Do you have any alternative to `iterrows()` as it is very slow operation for a large dataframe (Let's say 1 million rows). – BetterCallMe Dec 28 '22 at 15:50