简体   繁体   中英

PyMongo & Pandas - Update multiple records in MongoDB collection from dataframe by matching id

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

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)

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