File size: 1,213 Bytes
c006a72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from urllib.parse import quote_plus

password = quote_plus('cidar@2024$')
uri = f"mongodb+srv://deemaa2:{password}@cluster0.qjruxcb.mongodb.net/?retryWrites=true&w=majority"

# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))

# Specify the database and collection
db = client.your_database_name
collection = db.your_collection_name

def save_response(response_data):
    """Saves a response to the MongoDB collection."""
    try:
        # Insert the response data into the collection
        collection.insert_one(response_data)
        print("Response saved successfully.")
    except Exception as e:
        print(f"An error occurred: {e}")


def read_responses(filter_query=None):
    """Reads responses from the MongoDB collection based on an optional filter."""
    try:
        if filter_query is None:
            filter_query = {}  # An empty query will return all documents
        responses = collection.find(filter_query)
        return list(responses)  # Convert cursor to list
    except Exception as e:
        print(f"An error occurred: {e}")
        return []