Home > Net >  TyperError while inserting list into mongodb via python
TyperError while inserting list into mongodb via python

Time:02-01

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.

CodePudding user response:

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)
  • Related