[Python] list(collection.find()) not returning any data in pyhton

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    your text``import json
    from flask import Flask, jsonify, request
    from kafka_producer import send_user_query_to_kafka, send_business_rules_to_kafka
    from kafka_consumer import consume_and_process
    from pymongo import MongoClient

    app = Flask(__name__)

    # MongoDB connection setup
    def get_mongo_client():
    # MongoDB is running in a Docker container
    client = MongoClient('mongodb://localhost:27017')
    # testing mongodb connection
    server_info = client.server_info() # This will raise an exception if unable to connect
    print("Connected to MongoDB:", server_info)
    return client

    def get_business_rules():
    """
    Retrieves business rules from MongoDB.

    Returns:
    - dict: The business rules retrieved from MongoDB.
    """
    client = get_mongo_client()
    db = client['businessrules']
    collection = db['testCollection']

    # Assuming the business rules are stored in the collection as documents
    business_rules = list(collection.find()) # Retrieve all documents

    # If rules are found, return them, otherwise return a default message or empty dict
    if business_rules:
    #return business_rules # Return the business rules as a dictionary
    return business_rules # Convert the business rule document to a string
    else:
    return "no business rules found " # Empty dictionary if no business rules found


    @app.route('/send_query', methods=['POST'])
    def send_query():
    user_query = request.json['query']

    # Retrieve business rules from MongoDB
    business_rules = get_business_rules()
    print("BR:", business_rules)

    # Send user query and business rules to Kafka
    send_user_query_to_kafka(user_query)
    send_business_rules_to_kafka(business_rules)


    if user_query:
    processed_result = consume_and_process()

    # Print the response data to the terminal/console
    print("Response Data:", processed_result)

    return jsonify({
    "status": "success",
    "message": "Query and Business rules sent to Kafka",
    "query": user_query,
    "business_rules": business_rules,
    "processed_result": processed_result # Include the processed result from LLM
    }), 200
    else:
    return jsonify({
    "status": "error",
    "message": "No query received"
    }), 400


    if __name__ == '__main__':
    app.run(debug=True, port=5000)


    this is my code that is supposed to retrieve business rules from a mongodb docker container and send it along with a user query to an llm that will turn a responce using kafka, but when i do test of business rules i get "no business rules found"

    I tried chnaging the code many times , using cursor, using a different collection , retrieving using a filter , changing json.dump to change the business rules from json to string and many other things but i keep getting same error message .... if there is a way this can b fixed i'd like to have the business rules retrieved from mongo

    Continue reading...

Compartilhe esta Página