[{'Text': '        Returns:\n            Formatted JSON string of results, or error message.\n        """\n        if pipeline_json:\n            try:\n                pipeline = json.loads(pipeline_json)\n            except json.JSONDecodeError as e:\n                return f"Error: Invalid JSON pipeline — {e}"\n        elif pipeline is None:\n            return "Error: Provide pipeline_json (JSON string) or pipeline (list)"\n\n        if not isinstance(pipeline, list):\n            return f"Error: Pipeline must be a JSON array, got {type(pipeline).__name__}"\n\n        try:\n            _validate_pipeline(pipeline)\n        except ValueError as e:\n            return f"Error: {e}"\n\n        # Auto-detect target collection from $match keys\n        collection = "Statistics"\n        first_stage = pipeline[0] if pipeline else {}\n        if "$match" in first_stage:\n            match = first_stage["$match"]\n            if isinstance(match, dict):\n                mkeys = set(match.keys())\n                if "isCurrent" in mkeys or "currentStatsPeriod" in mkeys:\n                    collection = "GlobalVariables"\n                elif (\n                    "operatorId" in mkeys or "type" in mkeys or "period" in mkeys\n                    or "state" in mkeys or "subscribers" in mkeys\n                ):\n                    collection = "Statistics"\n                elif "year" in mkeys and "quarter" in mkeys:\n                    collection = "Sources"\n                elif mkeys & {"name", "country", "isArchived", "technologies", "asn", "_id"}:\n                    collection = "Operator"\n\n        if collection not in _GBS_COLLECTIONS:\n            return f"Error: Collection \'{collection}\' not allowed: {sorted(_GBS_COLLECTIONS)}"\n\n        try:\n            result = mongo.run_aggregation(pipeline, collection=collection, timeout=60)\n        except RuntimeError as e:\n            return f"Query error: {e}"\n        except json.JSONDecodeError as e:\n            return f"Result parsing error: {e}"\n        except Exception as e:\n            return f"Unexpected error: {e}"\n\n        if not result:\n            return "[] — no documents matched the query"\n\n        try:\n            formatted = json.dumps(result, indent=2, default=str)\n'}]