简体   繁体   English

PyMongo & Pandas - 通过匹配 id 从 dataframe 更新 MongoDB 集合中的多条记录

[英]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?是否可以通过匹配 id 从 dataframe 更新 MongoDB 集合中的多个记录?

dataframe 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但我得到了AttributeError: 'dict' object has no attribute 'iterrows'错误消息

You don't need the find loop, just use iterrows() to get the data and UpdateOne to perform the upsert.您不需要查找循环,只需使用iterrows()获取数据并使用UpdateOne执行 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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