简体   繁体   中英

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)

Above code should be able to insert list into mongodb but it is giving following error.

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

how code should be instead of what it is.

Could you please try converting the message object to dict:

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)

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