except errors.OperationFailure as error:
            logger.error(error)
        if cursor:
            items = [json.loads(document["History"]) for document in cursor]
        else:
            items = []
        messages = messages_from_dict(items)
        return messages
[docs]    def add_message(self, message: BaseMessage) -> None:
        """Append the message to the record in MongoDB"""
        from pymongo import errors
        try:
            self.collection.insert_one(
                {
                    "SessionId": self.session_id,
                    "History": json.dumps(_message_to_dict(message)),
                }
            )
        except errors.WriteError as err:
            logger.error(err)
[docs]    def clear(self) -> None:
        """Clear session memory from MongoDB"""
        from pymongo import errors
        try:
            self.collection.delete_many({"SessionId": self.session_id})
        except errors.WriteError as err:
            logger.error(err)