我正在尝试使用pandas进行一些分析后更新MongoDB中的集合,这是我的代码:
client=MongoClient()
db=client.database
cll=db.collection
cursor=cll.find()
df=pd.DataFrame(list(cursor))
df['new_field'] = df['existing_field_A'].apply(lambda x: personalized_function(x))
cll.update_many(filter={}, update=df.to_dict('record'))
但会引发下一个错误:
TypeError: update must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
原因df.to_dict('record')
为<class 'list'>
因此我将代码更改为:
client=MongoClient()
db=client.database
cll=db.collection
cursor=cll.find()
df=pd.DataFrame(list(cursor))
df['new_field'] = df['existing_field_A'].apply(lambda x: personalized_function(x))
for index, row in df.iterrows():
_id=row['_id']
new_value=row['new_field']
cll.update_one({'_id':_id}, {'$set':{'new_field':new_value}})
它工作正常,但我想知道是否有更好的方法来更新我的收藏。
我该怎么办?或者我如何更新整个集合而不是逐个文档?