{"record":{"id":"4e9f41ced722c5b8","repo":"hsliuping/TradingAgents-CN","slug":"mongodb-mongodb-connection-string","errorCode":null,"errorMessage":"MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\\n例如: MONGODB_CONNECTION_STRING=mongodb://localhost:27017/","messagePattern":"MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\\\\n例如: MONGODB_CONNECTION_STRING=mongodb://localhost:27017/","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/config/database_config.py","lineNumber":27,"sourceCode":"\n\nclass DatabaseConfig:\n    \"\"\"数据库配置管理类\"\"\"\n    \n    @staticmethod\n    def get_mongodb_config() -> Dict[str, Any]:\n        \"\"\"\n        获取MongoDB配置\n        \n        Returns:\n            Dict[str, Any]: MongoDB配置字典\n            \n        Raises:\n            ValueError: 当必要的配置未设置时\n        \"\"\"\n        connection_string = os.getenv('MONGODB_CONNECTION_STRING')\n        if not connection_string:\n            raise ValueError(\n                \"MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\\n\"\n                \"例如: MONGODB_CONNECTION_STRING=mongodb://localhost:27017/\"\n            )\n        \n        return {\n            'connection_string': connection_string,\n            'database': os.getenv('MONGODB_DATABASE', 'tradingagents'),\n            'auth_source': os.getenv('MONGODB_AUTH_SOURCE', 'admin')\n        }\n    \n    @staticmethod\n    def get_redis_config() -> Dict[str, Any]:\n        \"\"\"\n        获取Redis配置\n        \n        Returns:\n            Dict[str, Any]: Redis配置字典\n            ","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/config/database_config.py#L9-L45","documentation":"Raised by get_mongodb_config() when the MONGODB_CONNECTION_STRING environment variable is empty or unset. The config layer refuses to fall back to a default, so any validate_config() run without MongoDB configured fails immediately.","triggerScenarios":"Calling tradingagents.config.database_config.validate_config() (which calls get_mongodb_config) with MONGODB_CONNECTION_STRING unset or set to empty string in the process environment / .env.","commonSituations":"Fresh clone without .env, .env not loaded because python-dotenv load_dotenv() was never called, CI runners lacking the variable, or docker env vars not passed through.","solutions":["export MONGODB_CONNECTION_STRING=mongodb://localhost:27017/ (or add to .env)","Ensure load_dotenv() runs before validate_config()","If MongoDB is optional, gate the validate_config call behind a feature flag or catch ValueError"],"exampleFix":"# before\nvalidate_config()  # raises\n# after\nos.environ.setdefault('MONGODB_CONNECTION_STRING','mongodb://localhost:27017/')\nvalidate_config()","handlingStrategy":"validation","validationCode":"import os\nif not os.getenv('MONGODB_CONNECTION_STRING'):\n    raise SystemExit('Set MONGODB_CONNECTION_STRING before starting')\nvalidate_config()","typeGuard":null,"tryCatchPattern":"try:\n    cfg = get_mongodb_config()\nexcept ValueError as e:\n    if 'MONGODB_CONNECTION_STRING' in str(e):\n        # degrade gracefully without mongo\n        cfg = None","preventionTips":["Load .env with python-dotenv at process start","Add a startup preflight that checks required env vars","Fail fast in docker-compose with required: env var semantics"],"tags":["python","mongodb","env-var","config"],"backgroundTag":"missing-env-var","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}