Search code examples
pythonmongodbpymongotelethon

Unable to save retrieved data into MongoDB - Python


I am retrieving data from telegram, it is being retrieved successfully but how can I save it into MongoDB.

for chat in chats:
with TelegramClient('sessionx', api_id, api_hash) as client:
    for message in client.iter_messages(chat, offset_date=datetime.date(2022, 11, 11), reverse=True):
        print(message)
        data = {"group": chat, "sender": message.sender_id,
                "text": message.text, "date": message.date}

        temp_df = pd.DataFrame(data, index=[1])
        df = df.append(temp_df)

collection.insert_many(df)error


Solution

  • Possible error :

    df is defined as a pandas DataFrame or else, it might not be able to parse your object to save it in mongo. Quick reminder, mongodb accept document following the BSON format.

    You could do something like this :

    my_list = []
    for chat in chats:
        with TelegramClient('sessionx', api_id, api_hash) as client:
            for message in client.iter_messages(chat, offset_date=datetime.date(2022, 11, 11), reverse=True):
                print(message)
                my_list.append({"group": chat, "sender": message.sender_id, "text": message.text, "date": message.date})
    
    collection.insert_many(my_list)