# πŸƒ Sistema Documentazione con MongoDB ## NovitΓ  Versione 2.0 Il sistema Γ¨ stato **completamente migrato a MongoDB 7.0** per migliorare: - βœ… FlessibilitΓ  schema - βœ… Performance - βœ… ScalabilitΓ  - βœ… Developer experience ## πŸš€ Quick Start MongoDB ### 1. Local Development ```bash # Clone repository git clone https://git.company.local/infrastructure/datacenter-docs.git cd datacenter-docs # Setup environment cp .env.example .env nano .env # Edit MongoDB credentials # Start MongoDB + Redis docker-compose up -d mongodb redis # Install dependencies poetry install # Start API poetry run uvicorn datacenter_docs.api.main:app --reload ``` ### 2. Docker Compose (All-in-One) ```bash # Edit .env cp .env.example .env # MongoDB credentials MONGO_ROOT_USER=admin MONGO_ROOT_PASSWORD=your_secure_password MONGODB_URL=mongodb://admin:your_secure_password@mongodb:27017 MONGODB_DATABASE=datacenter_docs # Start everything docker-compose up -d # Check health curl http://localhost:8000/health # Response: {"status":"healthy","database":"mongodb",...} # Access services # API: http://localhost:8000/api/docs # Chat: http://localhost:8001 # Frontend: http://localhost # Flower: http://localhost:5555 ``` ### 3. Kubernetes ```bash # Apply manifests kubectl apply -f deploy/kubernetes/namespace.yaml # Create secrets kubectl create secret generic datacenter-secrets \ --from-literal=mongodb-url='mongodb://admin:password@mongodb:27017' \ --from-literal=mongodb-root-user='admin' \ --from-literal=mongodb-root-password='password' \ --from-literal=redis-url='redis://:password@redis:6379/0' \ --from-literal=mcp-api-key='your-key' \ --from-literal=anthropic-api-key='sk-ant-xxx' \ -n datacenter-docs # Deploy MongoDB (StatefulSet with replica set) kubectl apply -f deploy/kubernetes/mongodb.yaml # Deploy application kubectl apply -f deploy/kubernetes/deployment.yaml kubectl apply -f deploy/kubernetes/service.yaml kubectl apply -f deploy/kubernetes/ingress.yaml # Check status kubectl get pods -n datacenter-docs ``` ## πŸ“Š MongoDB Features ### Document Structure Tutti i dati sono memorizzati come documenti JSON nativi: ```json { "ticket_id": "INC-12345", "title": "Network issue", "description": "Cannot reach VLAN 100", "status": "resolved", "resolution": "Check switch configuration...", "suggested_actions": ["action1", "action2"], "confidence_score": 0.92, "metadata": { "source": "ServiceNow", "custom_field": "any value" }, "created_at": ISODate("2025-01-15T10:30:00Z") } ``` ### Collections - `tickets` - Ticket e risoluzioni - `documentation_sections` - Metadata sezioni doc - `chat_sessions` - Conversazioni chat - `system_metrics` - Metriche sistema - `audit_logs` - Audit trail ### Beanie ODM Utilizziamo **Beanie** (ODM moderno) per type-safe document operations: ```python from datacenter_docs.api.models import Ticket # Create ticket = Ticket( ticket_id="INC-001", title="Test", description="Testing MongoDB" ) await ticket.insert() # Find tickets = await Ticket.find(Ticket.status == "resolved").to_list() # Update ticket.status = "closed" await ticket.save() # Delete await ticket.delete() # Aggregation pipeline = [ {"$group": { "_id": "$category", "count": {"$sum": 1} }} ] result = await Ticket.aggregate(pipeline).to_list() ``` ## πŸ”§ Configurazione MongoDB ### Environment Variables ```bash # Required MONGODB_URL=mongodb://admin:password@mongodb:27017 MONGODB_DATABASE=datacenter_docs # Optional (for admin operations) MONGO_ROOT_USER=admin MONGO_ROOT_PASSWORD=secure_password ``` ### Connection String Examples ```bash # Local MONGODB_URL=mongodb://admin:password@localhost:27017 # Docker Compose MONGODB_URL=mongodb://admin:password@mongodb:27017 # Kubernetes (single) MONGODB_URL=mongodb://admin:password@mongodb.datacenter-docs.svc.cluster.local:27017 # Kubernetes (replica set) MONGODB_URL=mongodb://admin:password@mongodb-0.mongodb:27017,mongodb-1.mongodb:27017,mongodb-2.mongodb:27017/?replicaSet=rs0 # MongoDB Atlas (cloud) MONGODB_URL=mongodb+srv://user:password@cluster.mongodb.net/datacenter_docs?retryWrites=true&w=majority ``` ## πŸ” Query Examples ### Python API ```python # Simple queries resolved = await Ticket.find(Ticket.status == "resolved").to_list() high_priority = await Ticket.find( Ticket.priority == "high", Ticket.status == "processing" ).to_list() # Complex queries from datetime import datetime, timedelta recent = datetime.now() - timedelta(days=7) high_confidence = await Ticket.find( Ticket.created_at > recent, Ticket.confidence_score > 0.9 ).sort(-Ticket.created_at).to_list() # Text search search_results = await Ticket.find({ "$text": {"$search": "network connectivity"} }).to_list() # Aggregation stats = await Ticket.aggregate([ {"$group": { "_id": "$category", "total": {"$sum": 1}, "avg_confidence": {"$avg": "$confidence_score"} }}, {"$sort": {"total": -1}} ]).to_list() ``` ### MongoDB Shell ```javascript // Connect mongosh mongodb://admin:password@localhost:27017 use datacenter_docs // Basic queries db.tickets.find({ status: "resolved" }) db.tickets.countDocuments({ category: "network" }) // Complex queries db.tickets.find({ status: "resolved", confidence_score: { $gt: 0.8 }, created_at: { $gte: new Date("2025-01-01") } }) // Text search db.tickets.find({ $text: { $search: "network connectivity" } }) // Aggregation db.tickets.aggregate([ { $match: { status: "resolved" } }, { $group: { _id: "$category", count: { $sum: 1 }, avg_time: { $avg: "$processing_time" } }}, { $sort: { count: -1 } } ]) ``` ## πŸ› οΈ Maintenance ### Backup ```bash # Full backup docker-compose exec mongodb mongodump \ --username admin \ --password password \ --authenticationDatabase admin \ --out /data/backup # Restore docker-compose exec mongodb mongorestore \ --username admin \ --password password \ --authenticationDatabase admin \ /data/backup ``` ### Monitoring ```bash # Database stats docker-compose exec mongodb mongosh \ -u admin -p password --authenticationDatabase admin \ --eval "db.stats()" # Collection stats docker-compose exec mongodb mongosh \ -u admin -p password --authenticationDatabase admin \ datacenter_docs --eval "db.tickets.stats()" ``` ### Indexes ```javascript // Check indexes db.tickets.getIndexes() // Create custom index db.tickets.createIndex({ category: 1, status: 1 }) // Text search index db.tickets.createIndex({ title: "text", description: "text", resolution: "text" }) ``` ## πŸ” Security ### Authentication MongoDB usa autenticazione SCRAM-SHA-256: ```javascript // Create app user db.createUser({ user: "docs_app", pwd: "secure_password", roles: [ { role: "readWrite", db: "datacenter_docs" } ] }) ``` ### Authorization Roles disponibili: - `read` - Solo lettura - `readWrite` - Lettura + scrittura - `dbAdmin` - Amministrazione DB - `userAdmin` - Gestione utenti ### TLS/SSL ```bash # Generate certificates openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout mongodb.key -out mongodb.crt # Docker Compose con TLS mongodb: command: ["--tlsMode=requireTLS", "--tlsCertificateKeyFile=/cert/mongodb.pem"] volumes: - ./certs/mongodb.pem:/cert/mongodb.pem:ro ``` ## πŸ“ˆ Performance ### Connection Pooling ```python # Default: maxPoolSize=100 MONGODB_URL=mongodb://user:pass@host:27017/?maxPoolSize=200 ``` ### Read Preference ```python # Replica set - preferisci secondary per letture MONGODB_URL=mongodb://user:pass@host:27017/?readPreference=secondaryPreferred ``` ### Write Concern ```python # Majority (safe, slower) MONGODB_URL=mongodb://user:pass@host:27017/?w=majority # Faster (less safe) MONGODB_URL=mongodb://user:pass@host:27017/?w=1 ``` ## πŸš€ Scalability ### Replica Set (High Availability) ```yaml # docker-compose.yml services: mongodb-0: image: mongo:7.0 command: ["--replSet", "rs0"] mongodb-1: image: mongo:7.0 command: ["--replSet", "rs0"] mongodb-2: image: mongo:7.0 command: ["--replSet", "rs0"] ``` ### Sharding (Horizontal Scaling) Per dataset molto grandi (>1TB): ```javascript // Enable sharding sh.enableSharding("datacenter_docs") // Shard collection sh.shardCollection("datacenter_docs.tickets", { category: 1 }) ``` ## πŸ†š MongoDB vs PostgreSQL | Feature | MongoDB | PostgreSQL | |---------|---------|------------| | Schema | Flexible | Fixed | | Scaling | Horizontal (native) | Vertical (easier) | | Queries | JSON-like | SQL | | Transactions | Yes (4.0+) | Yes | | Performance (reads) | Excellent | Very good | | Performance (writes) | Excellent | Good | | JSON support | Native | JSONB | | Aggregation | Pipeline | SQL + CTEs | | Learning curve | Easy | Moderate | ## πŸ“š Documentation - πŸ“– [MONGODB_GUIDE.md](./MONGODB_GUIDE.md) - Guida completa MongoDB - πŸ“– [README_COMPLETE_SYSTEM.md](./README_COMPLETE_SYSTEM.md) - Sistema completo - πŸ“– [DEPLOYMENT_GUIDE.md](./DEPLOYMENT_GUIDE.md) - Deploy guide ## πŸ†˜ Troubleshooting ### Connection issues ```bash # Test MongoDB connection docker-compose exec api python -c " from motor.motor_asyncio import AsyncIOMotorClient import asyncio async def test(): client = AsyncIOMotorClient('mongodb://admin:password@mongodb:27017') await client.admin.command('ping') print('MongoDB OK') asyncio.run(test()) " ``` ### Authentication errors ```bash # Verify credentials docker-compose exec mongodb mongosh \ -u admin -p password --authenticationDatabase admin \ --eval "db.runCommand({connectionStatus: 1})" ``` ### Performance issues ```javascript // Check slow queries db.setProfilingLevel(2) // Log all queries db.system.profile.find().sort({ts:-1}).limit(5) // Analyze query db.tickets.find({status: "resolved"}).explain("executionStats") ``` --- **MongoDB Version**: 7.0 **Driver**: Motor 3.3+ (Async) **ODM**: Beanie 1.24+ **Minimum Python**: 3.10