簡體   English   中英

通過 python 將列表插入 mongodb 時出現 TyperError

[英]TyperError while inserting list into mongodb via python

for chat in chats:
    for message in client.iter_messages(chat, offset_date=datetime.date(2023, 1, 31), reverse=True):
        my_list.append(message)
collection.insert_many(my_list)

上面的代碼應該能夠將列表插入 mongodb 但它給出了以下錯誤。

Exception has occurred: TypeError
document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

代碼應該如何而不是它是什么。

您能否嘗試將消息 object 轉換為字典:

for chat in chats:
    for message in client.iter_messages(chat, offset_date=datetime.date(2023, 1, 31), reverse=True):
        my_list.append(message.to_dict()) <-- try this
collection.insert_many(my_list)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM