Skip to content

2025

MongoDB Data Archiving and Lifecycle Management: Advanced Strategies for Automated Data Retention, Performance Optimization, and Compliance

Production database systems accumulate vast amounts of data over time, creating significant challenges for performance optimization, storage cost management, and regulatory compliance. Traditional database systems often struggle with efficient data archiving strategies that balance query performance, storage costs, and data accessibility requirements while maintaining operational efficiency and compliance with data retention policies.

MongoDB provides comprehensive data lifecycle management capabilities that enable sophisticated archiving strategies through automated retention policies, performance-optimized data movement, and flexible storage tiering. Unlike traditional databases that require complex partitioning schemes and manual maintenance processes, MongoDB's document-based architecture and built-in features support seamless data archiving workflows that scale with growing data volumes while maintaining operational simplicity.

The Traditional Data Archiving Challenge

Conventional database systems face significant limitations when implementing data archiving and lifecycle management:

-- Traditional PostgreSQL data archiving - complex and maintenance-intensive approach

-- Create archive tables with identical structures (manual maintenance required)
CREATE TABLE orders_2023_archive (
    order_id SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    order_date TIMESTAMP NOT NULL,
    status VARCHAR(50) NOT NULL,
    total_amount DECIMAL(10,2) NOT NULL,
    items JSONB,
    shipping_address TEXT,
    billing_address TEXT,

    -- Archive-specific metadata
    archived_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    archived_by VARCHAR(100) DEFAULT current_user,
    archive_reason VARCHAR(200),
    original_table VARCHAR(100) DEFAULT 'orders',

    -- Compliance tracking
    retention_policy VARCHAR(100),
    scheduled_deletion_date DATE,
    legal_hold BOOLEAN DEFAULT false,

    -- Performance considerations
    CONSTRAINT orders_2023_archive_date_check 
        CHECK (order_date >= '2023-01-01' AND order_date < '2024-01-01')
);

-- Create indexes for archive table (must mirror production indexes)
CREATE INDEX orders_2023_archive_customer_id_idx ON orders_2023_archive(customer_id);
CREATE INDEX orders_2023_archive_date_idx ON orders_2023_archive(order_date);
CREATE INDEX orders_2023_archive_status_idx ON orders_2023_archive(status);
CREATE INDEX orders_2023_archive_archived_date_idx ON orders_2023_archive(archived_date);

-- Similar structure needed for each year and potentially each table
CREATE TABLE customer_interactions_2023_archive (
    interaction_id SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    interaction_date TIMESTAMP NOT NULL,
    interaction_type VARCHAR(100),
    details JSONB,
    outcome VARCHAR(100),

    -- Archive metadata
    archived_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    archived_by VARCHAR(100) DEFAULT current_user,
    archive_reason VARCHAR(200),
    original_table VARCHAR(100) DEFAULT 'customer_interactions',

    CONSTRAINT customer_interactions_2023_archive_date_check 
        CHECK (interaction_date >= '2023-01-01' AND interaction_date < '2024-01-01')
);

-- Complex archiving procedure with limited automation
CREATE OR REPLACE FUNCTION archive_old_data(
    source_table VARCHAR(100),
    archive_table VARCHAR(100), 
    cutoff_date DATE,
    batch_size INTEGER DEFAULT 1000,
    archive_reason VARCHAR(200) DEFAULT 'automated_archiving'
) RETURNS TABLE (
    records_archived INTEGER,
    batches_processed INTEGER,
    total_processing_time_seconds INTEGER,
    errors_encountered INTEGER,
    last_archived_id BIGINT
) AS $$
DECLARE
    current_batch INTEGER := 0;
    total_archived INTEGER := 0;
    start_time TIMESTAMP := clock_timestamp();
    last_id BIGINT := 0;
    batch_result INTEGER;
    error_count INTEGER := 0;
    sql_command TEXT;
    archive_command TEXT;
BEGIN

    LOOP
        -- Dynamic SQL for flexible table handling (security risk)
        sql_command := FORMAT('
            WITH batch_data AS (
                SELECT * FROM %I 
                WHERE created_date < %L 
                AND id > %L
                ORDER BY id 
                LIMIT %L
            ),
            archived_batch AS (
                INSERT INTO %I 
                SELECT *, CURRENT_TIMESTAMP, %L, %L, %L
                FROM batch_data
                RETURNING id
            ),
            deleted_batch AS (
                DELETE FROM %I 
                WHERE id IN (SELECT id FROM archived_batch)
                RETURNING id
            )
            SELECT COUNT(*), MAX(id) FROM deleted_batch',
            source_table,
            cutoff_date,
            last_id,
            batch_size,
            archive_table,
            current_user,
            archive_reason,
            source_table,
            source_table
        );

        BEGIN
            EXECUTE sql_command INTO batch_result, last_id;

            -- Exit if no more records to process
            IF batch_result = 0 OR last_id IS NULL THEN
                EXIT;
            END IF;

            total_archived := total_archived + batch_result;
            current_batch := current_batch + 1;

            -- Commit every batch to avoid long-running transactions
            COMMIT;

            -- Brief pause to avoid overwhelming the system
            PERFORM pg_sleep(0.1);

        EXCEPTION WHEN OTHERS THEN
            error_count := error_count + 1;

            -- Log error details (basic error handling)
            INSERT INTO archive_error_log (
                source_table,
                archive_table,
                batch_number,
                last_processed_id,
                error_message,
                error_timestamp
            ) VALUES (
                source_table,
                archive_table,
                current_batch,
                last_id,
                SQLERRM,
                CURRENT_TIMESTAMP
            );

            -- Stop after too many errors
            IF error_count > 10 THEN
                EXIT;
            END IF;
        END;
    END LOOP;

    RETURN QUERY SELECT 
        total_archived,
        current_batch,
        EXTRACT(SECONDS FROM clock_timestamp() - start_time)::INTEGER,
        error_count,
        COALESCE(last_id, 0);

EXCEPTION WHEN OTHERS THEN
    -- Global error handling
    INSERT INTO archive_error_log (
        source_table,
        archive_table,
        batch_number,
        last_processed_id,
        error_message,
        error_timestamp
    ) VALUES (
        source_table,
        archive_table,
        -1,
        -1,
        'Global archiving error: ' || SQLERRM,
        CURRENT_TIMESTAMP
    );

    RETURN QUERY SELECT 0, 0, 0, 1, 0::BIGINT;
END;
$$ LANGUAGE plpgsql;

-- Manual data archiving execution (error-prone and inflexible)
DO $$
DECLARE
    archive_result RECORD;
    tables_to_archive VARCHAR(100)[] := ARRAY['orders', 'customer_interactions', 'payment_transactions', 'audit_logs'];
    current_table VARCHAR(100);
    archive_table_name VARCHAR(100);
    cutoff_date DATE := CURRENT_DATE - INTERVAL '2 years';
BEGIN

    FOREACH current_table IN ARRAY tables_to_archive
    LOOP
        -- Generate archive table name
        archive_table_name := current_table || '_' || EXTRACT(YEAR FROM cutoff_date) || '_archive';

        -- Check if archive table exists (manual verification)
        IF NOT EXISTS (SELECT 1 FROM information_schema.tables 
                      WHERE table_name = archive_table_name) THEN
            RAISE NOTICE 'Archive table % does not exist, skipping %', archive_table_name, current_table;
            CONTINUE;
        END IF;

        RAISE NOTICE 'Starting archival of % to %', current_table, archive_table_name;

        -- Execute archiving function
        FOR archive_result IN 
            SELECT * FROM archive_old_data(
                current_table, 
                archive_table_name, 
                cutoff_date,
                1000,  -- batch size
                'automated_yearly_archival'
            )
        LOOP
            RAISE NOTICE 'Archived % records from % in % batches, % errors, processing time: % seconds',
                archive_result.records_archived,
                current_table,
                archive_result.batches_processed,
                archive_result.errors_encountered,
                archive_result.total_processing_time_seconds;
        END LOOP;

        -- Basic statistics update (manual maintenance)
        EXECUTE FORMAT('ANALYZE %I', archive_table_name);

    END LOOP;
END;
$$;

-- Attempt at automated retention policy management (very limited)
CREATE TABLE data_retention_policies (
    policy_id SERIAL PRIMARY KEY,
    table_name VARCHAR(100) NOT NULL,
    retention_period_months INTEGER NOT NULL,
    archive_after_months INTEGER,
    delete_after_months INTEGER,

    -- Policy configuration
    policy_enabled BOOLEAN DEFAULT true,
    date_field VARCHAR(100) NOT NULL DEFAULT 'created_date',
    archive_storage_location VARCHAR(200),

    -- Compliance settings
    legal_hold_exemption BOOLEAN DEFAULT false,
    gdpr_applicable BOOLEAN DEFAULT false,
    custom_retention_rules JSONB,

    -- Execution tracking
    last_executed TIMESTAMP,
    last_execution_status VARCHAR(50),
    last_execution_error TEXT,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Insert basic retention policies (manual configuration)
INSERT INTO data_retention_policies (
    table_name, retention_period_months, archive_after_months, delete_after_months,
    date_field, archive_storage_location
) VALUES 
('orders', 84, 24, 96, 'order_date', '/archives/orders/'),
('customer_interactions', 60, 12, 72, 'interaction_date', '/archives/interactions/'),
('payment_transactions', 120, 36, 144, 'transaction_date', '/archives/payments/'),
('audit_logs', 36, 6, 48, 'log_timestamp', '/archives/audit/');

-- Rudimentary retention policy execution function
CREATE OR REPLACE FUNCTION execute_retention_policies()
RETURNS TABLE (
    policy_name VARCHAR(100),
    execution_status VARCHAR(50),
    records_processed INTEGER,
    execution_time_seconds INTEGER,
    error_message TEXT
) AS $$
DECLARE
    policy_record RECORD;
    archive_cutoff DATE;
    delete_cutoff DATE;
    execution_start TIMESTAMP;
    archive_result RECORD;
    records_affected INTEGER;
BEGIN

    FOR policy_record IN 
        SELECT * FROM data_retention_policies 
        WHERE policy_enabled = true
    LOOP
        execution_start := clock_timestamp();

        BEGIN
            -- Calculate cutoff dates based on policy
            archive_cutoff := CURRENT_DATE - (policy_record.archive_after_months || ' months')::INTERVAL;
            delete_cutoff := CURRENT_DATE - (policy_record.delete_after_months || ' months')::INTERVAL;

            -- Archival phase (if configured)
            IF policy_record.archive_after_months IS NOT NULL THEN
                SELECT * INTO archive_result FROM archive_old_data(
                    policy_record.table_name,
                    policy_record.table_name || '_archive',
                    archive_cutoff,
                    500,
                    'retention_policy_execution'
                );

                records_affected := archive_result.records_archived;
            END IF;

            -- Update execution status
            UPDATE data_retention_policies 
            SET 
                last_executed = CURRENT_TIMESTAMP,
                last_execution_status = 'success',
                last_execution_error = NULL
            WHERE policy_id = policy_record.policy_id;

            RETURN QUERY SELECT 
                policy_record.table_name,
                'success'::VARCHAR(50),
                COALESCE(records_affected, 0),
                EXTRACT(SECONDS FROM clock_timestamp() - execution_start)::INTEGER,
                NULL::TEXT;

        EXCEPTION WHEN OTHERS THEN
            -- Update error status
            UPDATE data_retention_policies 
            SET 
                last_executed = CURRENT_TIMESTAMP,
                last_execution_status = 'error',
                last_execution_error = SQLERRM
            WHERE policy_id = policy_record.policy_id;

            RETURN QUERY SELECT 
                policy_record.table_name,
                'error'::VARCHAR(50),
                0,
                EXTRACT(SECONDS FROM clock_timestamp() - execution_start)::INTEGER,
                SQLERRM;
        END;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

-- Problems with traditional archiving approaches:
-- 1. Manual archive table creation and maintenance for each table and time period
-- 2. Complex partitioning schemes that require ongoing schema management
-- 3. Limited automation capabilities requiring extensive custom development
-- 4. Poor performance during archiving operations that impact production systems
-- 5. Inflexible retention policies that don't adapt to changing business requirements
-- 6. Minimal integration with cloud storage and tiered storage strategies
-- 7. Limited compliance tracking and audit trail capabilities
-- 8. No built-in data lifecycle automation or policy-driven management
-- 9. Complex disaster recovery for archived data across multiple table structures
-- 10. High maintenance overhead for managing archive table schemas and indexes

MongoDB provides sophisticated data lifecycle management with automated archiving capabilities:

// MongoDB Data Archiving and Lifecycle Management - comprehensive automation system
const { MongoClient, GridFSBucket } = require('mongodb');
const { createReadStream, createWriteStream } = require('fs');
const { S3Client, PutObjectCommand, GetObjectCommand } = require('@aws-sdk/client-s3');
const { promisify } = require('util');
const zlib = require('zlib');

// Advanced data lifecycle management and archiving system
class MongoDataLifecycleManager {
  constructor(connectionUri, options = {}) {
    this.client = new MongoClient(connectionUri);
    this.db = null;
    this.collections = new Map();

    // Lifecycle management configuration
    this.config = {
      // Archive storage configuration
      archiveStorage: {
        type: options.archiveStorage?.type || 'mongodb', // mongodb, gridfs, s3, filesystem
        location: options.archiveStorage?.location || 'archives',
        compression: options.archiveStorage?.compression || 'gzip',
        encryption: options.archiveStorage?.encryption || false,
        checksumVerification: options.archiveStorage?.checksumVerification || true
      },

      // Performance optimization settings
      performance: {
        batchSize: options.performance?.batchSize || 1000,
        maxConcurrentOperations: options.performance?.maxConcurrentOperations || 3,
        throttleDelayMs: options.performance?.throttleDelayMs || 10,
        memoryLimitMB: options.performance?.memoryLimitMB || 512,
        indexOptimization: options.performance?.indexOptimization || true
      },

      // Compliance and audit settings
      compliance: {
        auditLogging: options.compliance?.auditLogging !== false,
        legalHoldSupport: options.compliance?.legalHoldSupport !== false,
        gdprCompliance: options.compliance?.gdprCompliance || false,
        dataClassification: options.compliance?.dataClassification || {},
        retentionPolicyEnforcement: options.compliance?.retentionPolicyEnforcement !== false
      },

      // Automation settings
      automation: {
        scheduledExecution: options.automation?.scheduledExecution || false,
        executionInterval: options.automation?.executionInterval || 86400000, // 24 hours
        failureRetryAttempts: options.automation?.failureRetryAttempts || 3,
        alerting: options.automation?.alerting || false,
        monitoringEnabled: options.automation?.monitoringEnabled !== false
      }
    };

    // External storage clients
    this.s3Client = options.s3Config ? new S3Client(options.s3Config) : null;
    this.gridFSBucket = null;

    // Operational state management
    this.retentionPolicies = new Map();
    this.executionHistory = [];
    this.activeOperations = new Map();
    this.performanceMetrics = {
      totalRecordsArchived: 0,
      totalStorageSaved: 0,
      averageOperationTime: 0,
      lastExecutionTime: null
    };
  }

  async initialize(dbName) {
    console.log('Initializing MongoDB Data Lifecycle Management system...');

    try {
      await this.client.connect();
      this.db = this.client.db(dbName);

      // Initialize GridFS bucket if needed
      if (this.config.archiveStorage.type === 'gridfs') {
        this.gridFSBucket = new GridFSBucket(this.db, { 
          bucketName: this.config.archiveStorage.location || 'archives' 
        });
      }

      // Setup system collections
      await this.setupSystemCollections();

      // Load existing retention policies
      await this.loadRetentionPolicies();

      // Setup automation if enabled
      if (this.config.automation.scheduledExecution) {
        await this.setupAutomatedExecution();
      }

      console.log('Data lifecycle management system initialized successfully');

    } catch (error) {
      console.error('Error initializing data lifecycle management:', error);
      throw error;
    }
  }

  async setupSystemCollections() {
    console.log('Setting up system collections for data lifecycle management...');

    // Retention policies collection
    const retentionPolicies = this.db.collection('data_retention_policies');
    await retentionPolicies.createIndexes([
      { key: { collection_name: 1 }, unique: true },
      { key: { policy_enabled: 1 } },
      { key: { next_execution: 1 } }
    ]);

    // Archive metadata collection
    const archiveMetadata = this.db.collection('archive_metadata');
    await archiveMetadata.createIndexes([
      { key: { source_collection: 1, archive_date: -1 } },
      { key: { archive_id: 1 }, unique: true },
      { key: { retention_policy_id: 1 } },
      { key: { compliance_status: 1 } }
    ]);

    // Execution audit log
    const executionAudit = this.db.collection('lifecycle_execution_audit');
    await executionAudit.createIndexes([
      { key: { execution_timestamp: -1 } },
      { key: { policy_id: 1, execution_timestamp: -1 } },
      { key: { operation_type: 1 } }
    ]);

    // Legal hold registry (compliance feature)
    if (this.config.compliance.legalHoldSupport) {
      const legalHolds = this.db.collection('legal_hold_registry');
      await legalHolds.createIndexes([
        { key: { hold_id: 1 }, unique: true },
        { key: { affected_collections: 1 } },
        { key: { hold_status: 1 } }
      ]);
    }
  }

  async defineRetentionPolicy(policyConfig) {
    console.log(`Defining retention policy for collection: ${policyConfig.collectionName}`);

    const policy = {
      policy_id: policyConfig.policyId || this.generatePolicyId(),
      collection_name: policyConfig.collectionName,

      // Retention timeline configuration
      retention_phases: {
        active_period_days: policyConfig.activePeriod || 365,
        archive_after_days: policyConfig.archiveAfter || 730,
        delete_after_days: policyConfig.deleteAfter || 2555, // 7 years default

        // Advanced retention phases
        cold_storage_after_days: policyConfig.coldStorageAfter,
        compliance_review_after_days: policyConfig.complianceReviewAfter
      },

      // Data identification and filtering
      date_field: policyConfig.dateField || 'created_at',
      additional_filters: policyConfig.filters || {},
      exclusion_criteria: policyConfig.exclusions || {},

      // Archive configuration
      archive_settings: {
        storage_type: policyConfig.archiveStorage || this.config.archiveStorage.type,
        compression_enabled: policyConfig.compression !== false,
        encryption_required: policyConfig.encryption || false,
        batch_size: policyConfig.batchSize || this.config.performance.batchSize,

        // Performance optimization
        index_hints: policyConfig.indexHints || [],
        sort_optimization: policyConfig.sortField || policyConfig.dateField,
        memory_limit: policyConfig.memoryLimit || '200M'
      },

      // Compliance configuration
      compliance_settings: {
        legal_hold_exempt: policyConfig.legalHoldExempt || false,
        data_classification: policyConfig.dataClassification || 'standard',
        gdpr_applicable: policyConfig.gdprApplicable || false,
        audit_level: policyConfig.auditLevel || 'standard',

        // Data sensitivity handling
        pii_fields: policyConfig.piiFields || [],
        anonymization_rules: policyConfig.anonymizationRules || {}
      },

      // Execution configuration
      execution_settings: {
        policy_enabled: policyConfig.enabled !== false,
        execution_schedule: policyConfig.schedule || '0 2 * * *', // Daily at 2 AM
        max_execution_time_minutes: policyConfig.maxExecutionTime || 120,
        failure_retry_attempts: policyConfig.retryAttempts || 3,
        notification_settings: policyConfig.notifications || {}
      },

      // Metadata and tracking
      policy_metadata: {
        created_by: policyConfig.createdBy || 'system',
        created_at: new Date(),
        last_modified: new Date(),
        policy_version: policyConfig.version || '1.0',
        description: policyConfig.description || '',
        business_justification: policyConfig.businessJustification || ''
      }
    };

    // Store retention policy
    const retentionPolicies = this.db.collection('data_retention_policies');
    await retentionPolicies.replaceOne(
      { collection_name: policy.collection_name },
      policy,
      { upsert: true }
    );

    // Cache policy for operational use
    this.retentionPolicies.set(policy.collection_name, policy);

    console.log(`Retention policy defined successfully for ${policy.collection_name}`);
    return policy.policy_id;
  }

  async executeDataArchiving(collectionName, options = {}) {
    console.log(`Starting data archiving for collection: ${collectionName}`);

    const policy = this.retentionPolicies.get(collectionName);
    if (!policy || !policy.execution_settings.policy_enabled) {
      throw new Error(`No enabled retention policy found for collection: ${collectionName}`);
    }

    const operationId = this.generateOperationId();
    const startTime = Date.now();

    try {
      // Check for legal holds
      if (this.config.compliance.legalHoldSupport) {
        await this.checkLegalHolds(collectionName, policy);
      }

      // Calculate archive cutoff date
      const cutoffDate = new Date();
      cutoffDate.setDate(cutoffDate.getDate() - policy.retention_phases.archive_after_days);

      // Build archive query with optimization
      const archiveQuery = {
        [policy.date_field]: { $lt: cutoffDate },
        ...policy.additional_filters,
        ...(policy.exclusion_criteria && { $nor: [policy.exclusion_criteria] })
      };

      // Count records to archive
      const sourceCollection = this.db.collection(collectionName);
      const recordCount = await sourceCollection.countDocuments(archiveQuery);

      if (recordCount === 0) {
        console.log(`No records found for archiving in ${collectionName}`);
        return { success: true, recordsProcessed: 0, operationId };
      }

      console.log(`Found ${recordCount} records to archive from ${collectionName}`);

      // Execute archiving in batches
      const archiveResult = await this.executeBatchArchiving(
        sourceCollection,
        archiveQuery,
        policy,
        operationId,
        options
      );

      // Create archive metadata record
      await this.createArchiveMetadata({
        archive_id: operationId,
        source_collection: collectionName,
        archive_date: new Date(),
        record_count: archiveResult.recordsArchived,
        archive_size: archiveResult.archiveSize,
        policy_id: policy.policy_id,
        archive_location: archiveResult.archiveLocation,
        checksum: archiveResult.checksum,

        compliance_info: {
          legal_hold_checked: this.config.compliance.legalHoldSupport,
          gdpr_compliant: policy.compliance_settings.gdpr_applicable,
          audit_trail: archiveResult.auditTrail
        }
      });

      // Log execution in audit trail
      await this.logExecutionAudit({
        operation_id: operationId,
        operation_type: 'archive',
        collection_name: collectionName,
        policy_id: policy.policy_id,
        execution_timestamp: new Date(),
        records_processed: archiveResult.recordsArchived,
        execution_duration_ms: Date.now() - startTime,
        status: 'success',
        performance_metrics: archiveResult.performanceMetrics
      });

      console.log(`Data archiving completed successfully for ${collectionName}`);
      return {
        success: true,
        operationId,
        recordsArchived: archiveResult.recordsArchived,
        archiveSize: archiveResult.archiveSize,
        executionTime: Date.now() - startTime
      };

    } catch (error) {
      console.error(`Error during data archiving for ${collectionName}:`, error);

      // Log error in audit trail
      await this.logExecutionAudit({
        operation_id: operationId,
        operation_type: 'archive',
        collection_name: collectionName,
        policy_id: policy?.policy_id,
        execution_timestamp: new Date(),
        execution_duration_ms: Date.now() - startTime,
        status: 'error',
        error_message: error.message
      });

      throw error;
    }
  }

  async executeBatchArchiving(sourceCollection, archiveQuery, policy, operationId, options) {
    console.log('Executing batch archiving with performance optimization...');

    const batchSize = policy.archive_settings.batch_size;
    const archiveLocation = await this.prepareArchiveLocation(operationId, policy);

    let totalArchived = 0;
    let totalSize = 0;
    let batchNumber = 0;
    const auditTrail = [];
    const performanceMetrics = {
      avgBatchTime: 0,
      maxBatchTime: 0,
      totalBatches: 0,
      throughputRecordsPerSecond: 0
    };

    // Create cursor with optimization hints
    const cursor = sourceCollection.find(archiveQuery)
      .sort({ [policy.archive_settings.sort_optimization]: 1 })
      .batchSize(batchSize);

    // Add index hint if specified
    if (policy.archive_settings.index_hints.length > 0) {
      cursor.hint(policy.archive_settings.index_hints[0]);
    }

    let batch = [];
    let batchStartTime = Date.now();

    for await (const document of cursor) {
      batch.push(document);

      // Process batch when full
      if (batch.length >= batchSize) {
        const batchResult = await this.processBatch(
          batch,
          archiveLocation,
          policy,
          batchNumber,
          operationId
        );

        const batchTime = Date.now() - batchStartTime;
        performanceMetrics.avgBatchTime = (performanceMetrics.avgBatchTime * batchNumber + batchTime) / (batchNumber + 1);
        performanceMetrics.maxBatchTime = Math.max(performanceMetrics.maxBatchTime, batchTime);

        totalArchived += batchResult.recordsProcessed;
        totalSize += batchResult.batchSize;
        batchNumber++;

        auditTrail.push({
          batch_number: batchNumber,
          records_processed: batchResult.recordsProcessed,
          batch_size: batchResult.batchSize,
          processing_time_ms: batchTime
        });

        // Reset batch
        batch = [];
        batchStartTime = Date.now();

        // Throttle to avoid overwhelming the system
        if (this.config.performance.throttleDelayMs > 0) {
          await new Promise(resolve => setTimeout(resolve, this.config.performance.throttleDelayMs));
        }
      }
    }

    // Process final partial batch
    if (batch.length > 0) {
      const batchResult = await this.processBatch(
        batch,
        archiveLocation,
        policy,
        batchNumber,
        operationId
      );

      totalArchived += batchResult.recordsProcessed;
      totalSize += batchResult.batchSize;
      batchNumber++;
    }

    // Calculate final performance metrics
    performanceMetrics.totalBatches = batchNumber;
    performanceMetrics.throughputRecordsPerSecond = totalArchived / ((Date.now() - batchStartTime) / 1000);

    // Generate archive checksum for integrity verification
    const checksum = await this.generateArchiveChecksum(archiveLocation, totalArchived);

    console.log(`Batch archiving completed: ${totalArchived} records in ${batchNumber} batches`);

    return {
      recordsArchived: totalArchived,
      archiveSize: totalSize,
      archiveLocation,
      checksum,
      auditTrail,
      performanceMetrics
    };
  }

  async processBatch(batch, archiveLocation, policy, batchNumber, operationId) {
    const batchStartTime = Date.now();

    // Apply data transformations if needed (PII anonymization, etc.)
    const processedBatch = await this.applyDataTransformations(batch, policy);

    // Store batch based on configured storage type
    let batchSize;
    switch (this.config.archiveStorage.type) {
      case 'mongodb':
        batchSize = await this.storeBatchToMongoDB(processedBatch, archiveLocation);
        break;
      case 'gridfs':
        batchSize = await this.storeBatchToGridFS(processedBatch, archiveLocation, batchNumber);
        break;
      case 's3':
        batchSize = await this.storeBatchToS3(processedBatch, archiveLocation, batchNumber);
        break;
      case 'filesystem':
        batchSize = await this.storeBatchToFileSystem(processedBatch, archiveLocation, batchNumber);
        break;
      default:
        throw new Error(`Unsupported archive storage type: ${this.config.archiveStorage.type}`);
    }

    // Remove archived documents from source collection
    const documentIds = batch.map(doc => doc._id);
    const deleteResult = await this.db.collection(policy.collection_name).deleteMany({
      _id: { $in: documentIds }
    });

    console.log(`Batch ${batchNumber}: archived ${batch.length} records, size: ${batchSize} bytes`);

    return {
      recordsProcessed: batch.length,
      batchSize,
      deletedRecords: deleteResult.deletedCount,
      processingTime: Date.now() - batchStartTime
    };
  }

  async applyDataTransformations(batch, policy) {
    if (!policy.compliance_settings.pii_fields.length && 
        !Object.keys(policy.compliance_settings.anonymization_rules).length) {
      return batch; // No transformations needed
    }

    console.log('Applying data transformations for compliance...');

    return batch.map(document => {
      let processedDoc = { ...document };

      // Apply PII field anonymization
      policy.compliance_settings.pii_fields.forEach(field => {
        if (processedDoc[field]) {
          processedDoc[field] = this.anonymizeField(processedDoc[field], field);
        }
      });

      // Apply custom anonymization rules
      Object.entries(policy.compliance_settings.anonymization_rules).forEach(([field, rule]) => {
        if (processedDoc[field]) {
          processedDoc[field] = this.applyAnonymizationRule(processedDoc[field], rule);
        }
      });

      // Add transformation metadata
      processedDoc._archive_metadata = {
        original_id: document._id,
        archived_at: new Date(),
        transformations_applied: [
          ...policy.compliance_settings.pii_fields.map(field => `pii_anonymization:${field}`),
          ...Object.keys(policy.compliance_settings.anonymization_rules).map(field => `custom_rule:${field}`)
        ]
      };

      return processedDoc;
    });
  }

  async storeBatchToMongoDB(batch, archiveLocation) {
    const archiveCollection = this.db.collection(archiveLocation);
    const insertResult = await archiveCollection.insertMany(batch, { 
      ordered: false,
      writeConcern: { w: 'majority', j: true }
    });

    return JSON.stringify(batch).length; // Approximate size
  }

  async storeBatchToGridFS(batch, archiveLocation, batchNumber) {
    const fileName = `${archiveLocation}_batch_${batchNumber.toString().padStart(6, '0')}.json`;
    const batchData = JSON.stringify(batch);

    if (this.config.archiveStorage.compression === 'gzip') {
      const compressedData = await promisify(zlib.gzip)(batchData);
      const uploadStream = this.gridFSBucket.openUploadStream(`${fileName}.gz`, {
        metadata: {
          batch_number: batchNumber,
          record_count: batch.length,
          compression: 'gzip',
          archived_at: new Date()
        }
      });

      uploadStream.end(compressedData);
      return compressedData.length;
    } else {
      const uploadStream = this.gridFSBucket.openUploadStream(fileName, {
        metadata: {
          batch_number: batchNumber,
          record_count: batch.length,
          archived_at: new Date()
        }
      });

      uploadStream.end(Buffer.from(batchData));
      return batchData.length;
    }
  }

  async storeBatchToS3(batch, archiveLocation, batchNumber) {
    if (!this.s3Client) {
      throw new Error('S3 client not configured for archive storage');
    }

    const key = `${archiveLocation}/batch_${batchNumber.toString().padStart(6, '0')}.json`;
    let data = JSON.stringify(batch);

    if (this.config.archiveStorage.compression === 'gzip') {
      data = await promisify(zlib.gzip)(data);
    }

    const putCommand = new PutObjectCommand({
      Bucket: this.config.archiveStorage.location,
      Key: key,
      Body: data,
      ContentType: 'application/json',
      ContentEncoding: this.config.archiveStorage.compression === 'gzip' ? 'gzip' : undefined,
      Metadata: {
        batch_number: batchNumber.toString(),
        record_count: batch.length.toString(),
        archived_at: new Date().toISOString()
      }
    });

    await this.s3Client.send(putCommand);
    return data.length;
  }

  async setupAutomaticDataDeletion(collectionName, options = {}) {
    console.log(`Setting up automatic data deletion for: ${collectionName}`);

    const policy = this.retentionPolicies.get(collectionName);
    if (!policy) {
      throw new Error(`No retention policy found for collection: ${collectionName}`);
    }

    // Use MongoDB TTL index for automatic deletion where possible
    const collection = this.db.collection(collectionName);

    // Create TTL index based on retention policy
    const ttlSeconds = policy.retention_phases.delete_after_days * 24 * 60 * 60;

    try {
      await collection.createIndex(
        { [policy.date_field]: 1 },
        { 
          expireAfterSeconds: ttlSeconds,
          background: true,
          name: `ttl_${policy.date_field}_${ttlSeconds}s`
        }
      );

      console.log(`TTL index created for automatic deletion: ${ttlSeconds} seconds`);

      // Update policy to track TTL index usage
      await this.db.collection('data_retention_policies').updateOne(
        { collection_name: collectionName },
        { 
          $set: { 
            'deletion_settings.ttl_enabled': true,
            'deletion_settings.ttl_seconds': ttlSeconds,
            'deletion_settings.ttl_field': policy.date_field
          }
        }
      );

      return { success: true, ttlSeconds, indexName: `ttl_${policy.date_field}_${ttlSeconds}s` };

    } catch (error) {
      console.error('Error setting up TTL index:', error);
      throw error;
    }
  }

  async retrieveArchivedData(archiveId, query = {}, options = {}) {
    console.log(`Retrieving archived data for archive ID: ${archiveId}`);

    // Get archive metadata
    const archiveMetadata = await this.db.collection('archive_metadata')
      .findOne({ archive_id: archiveId });

    if (!archiveMetadata) {
      throw new Error(`Archive not found: ${archiveId}`);
    }

    const { limit = 100, skip = 0, projection = {} } = options;
    let retrievedData = [];

    // Retrieve data based on storage type
    switch (this.config.archiveStorage.type) {
      case 'mongodb':
        const archiveCollection = this.db.collection(archiveMetadata.archive_location);
        retrievedData = await archiveCollection
          .find(query, { projection })
          .skip(skip)
          .limit(limit)
          .toArray();
        break;

      case 'gridfs':
        retrievedData = await this.retrieveFromGridFS(archiveMetadata, query, options);
        break;

      case 's3':
        retrievedData = await this.retrieveFromS3(archiveMetadata, query, options);
        break;

      default:
        throw new Error(`Archive retrieval not supported for storage type: ${this.config.archiveStorage.type}`);
    }

    // Log retrieval for audit purposes
    await this.logExecutionAudit({
      operation_id: this.generateOperationId(),
      operation_type: 'retrieve',
      archive_id: archiveId,
      execution_timestamp: new Date(),
      records_retrieved: retrievedData.length,
      retrieval_query: query,
      status: 'success'
    });

    return {
      archiveMetadata,
      data: retrievedData,
      totalRecords: archiveMetadata.record_count,
      retrievedCount: retrievedData.length
    };
  }

  async generateComplianceReport(collectionName, options = {}) {
    console.log(`Generating compliance report for: ${collectionName}`);

    const {
      startDate = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000), // 1 year ago
      endDate = new Date(),
      includeMetrics = true,
      includeAuditTrail = true
    } = options;

    const policy = this.retentionPolicies.get(collectionName);
    if (!policy) {
      throw new Error(`No retention policy found for collection: ${collectionName}`);
    }

    // Collect compliance data
    const complianceData = {
      collection_name: collectionName,
      policy_id: policy.policy_id,
      report_generated_at: new Date(),
      reporting_period: { start: startDate, end: endDate },

      // Policy compliance status
      policy_compliance: {
        policy_enabled: policy.execution_settings.policy_enabled,
        gdpr_compliant: policy.compliance_settings.gdpr_applicable,
        legal_hold_support: this.config.compliance.legalHoldSupport,
        audit_level: policy.compliance_settings.audit_level
      },

      // Archive operations summary
      archive_summary: await this.getArchiveSummary(collectionName, startDate, endDate),

      // Current data status
      data_status: await this.getCurrentDataStatus(collectionName, policy)
    };

    if (includeMetrics) {
      complianceData.performance_metrics = await this.getPerformanceMetrics(collectionName, startDate, endDate);
    }

    if (includeAuditTrail) {
      complianceData.audit_trail = await this.getAuditTrail(collectionName, startDate, endDate);
    }

    // Check for any compliance issues
    complianceData.compliance_issues = await this.identifyComplianceIssues(collectionName, policy);

    return complianceData;
  }

  async loadRetentionPolicies() {
    const policies = await this.db.collection('data_retention_policies')
      .find({ 'execution_settings.policy_enabled': true })
      .toArray();

    policies.forEach(policy => {
      this.retentionPolicies.set(policy.collection_name, policy);
    });

    console.log(`Loaded ${policies.length} retention policies`);
  }

  generatePolicyId() {
    return `policy_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  generateOperationId() {
    return `op_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  anonymizeField(value, fieldType) {
    // Simple anonymization - in production, use proper anonymization libraries
    if (typeof value === 'string') {
      if (fieldType.includes('email')) {
        return '[email protected]';
      } else if (fieldType.includes('name')) {
        return 'ANONYMIZED';
      } else {
        return '***REDACTED***';
      }
    }
    return null;
  }

  async createArchiveMetadata(metadata) {
    return await this.db.collection('archive_metadata').insertOne(metadata);
  }

  async logExecutionAudit(auditRecord) {
    if (this.config.compliance.auditLogging) {
      return await this.db.collection('lifecycle_execution_audit').insertOne(auditRecord);
    }
  }
}

// Benefits of MongoDB Data Lifecycle Management:
// - Automated retention policy enforcement with minimal manual intervention
// - Flexible storage tiering supporting MongoDB, GridFS, S3, and filesystem storage
// - Built-in compliance features including legal hold support and audit trails  
// - Performance-optimized batch processing with throttling and memory management
// - Comprehensive data transformation capabilities for PII protection and anonymization
// - TTL index integration for automatic deletion without application logic
// - Real-time monitoring and alerting for policy execution and compliance status
// - Scalable architecture supporting large-scale data archiving operations
// - Integrated backup and recovery capabilities for archived data
// - SQL-compatible lifecycle management operations through QueryLeaf integration

module.exports = {
  MongoDataLifecycleManager
};

Understanding MongoDB Data Lifecycle Architecture

Advanced Archiving Strategies and Compliance Management

Implement sophisticated data lifecycle policies with enterprise-grade compliance and automation:

// Production-ready data lifecycle automation with enterprise compliance features
class EnterpriseDataLifecycleManager extends MongoDataLifecycleManager {
  constructor(connectionUri, enterpriseConfig) {
    super(connectionUri, enterpriseConfig);

    this.enterpriseFeatures = {
      // Advanced compliance management
      complianceIntegration: {
        gdprAutomation: true,
        legalHoldWorkflows: true,
        auditTrailEncryption: true,
        regulatoryReporting: true,
        dataSubjectRequests: true
      },

      // Enterprise storage integration
      storageIntegration: {
        multiTierStorage: true,
        cloudStorageIntegration: true,
        compressionOptimization: true,
        encryptionAtRest: true,
        geographicReplication: true
      },

      // Advanced automation
      automationCapabilities: {
        mlPredictiveArchiving: true,
        workloadOptimization: true,
        costOptimization: true,
        capacityPlanning: true,
        performanceTuning: true
      }
    };

    this.initializeEnterpriseFeatures();
  }

  async implementIntelligentArchiving(collectionName, options = {}) {
    console.log('Implementing intelligent archiving with machine learning optimization...');

    const archivingStrategy = {
      // Predictive analysis for optimal archiving timing
      predictiveModeling: {
        accessPatternAnalysis: true,
        queryFrequencyPrediction: true,
        storageOptimization: true,
        performanceImpactMinimization: true
      },

      // Cost-optimized storage tiering
      costOptimization: {
        automaticTierSelection: true,
        compressionOptimization: true,
        geographicOptimization: true,
        providerOptimization: true
      },

      // Performance-aware archiving
      performanceOptimization: {
        nonBlockingArchiving: true,
        priorityBasedProcessing: true,
        resourceThrottling: true,
        systemImpactMinimization: true
      }
    };

    return await this.deployIntelligentArchiving(collectionName, archivingStrategy, options);
  }

  async setupAdvancedComplianceWorkflows(complianceConfig) {
    console.log('Setting up advanced compliance workflows...');

    const complianceWorkflows = {
      // GDPR compliance automation
      gdprCompliance: {
        dataSubjectRequestHandling: true,
        rightToErasureAutomation: true,
        dataPortabilitySupport: true,
        consentManagement: true,
        breachNotificationIntegration: true
      },

      // Industry-specific compliance
      industryCompliance: {
        soxCompliance: complianceConfig.sox || false,
        hipaaCompliance: complianceConfig.hipaa || false,
        pciDssCompliance: complianceConfig.pciDss || false,
        iso27001Compliance: complianceConfig.iso27001 || false
      },

      // Legal hold management
      legalHoldManagement: {
        automaticHoldEnforcement: true,
        holdNotificationWorkflows: true,
        custodyChainTracking: true,
        releaseAutomation: true
      }
    };

    return await this.deployComplianceWorkflows(complianceWorkflows);
  }
}

SQL-Style Data Lifecycle Management with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB data archiving and lifecycle management:

-- QueryLeaf data lifecycle management with SQL-familiar patterns for MongoDB

-- Define comprehensive data retention policy with advanced features
CREATE RETENTION_POLICY order_data_lifecycle AS (
  -- Target collection and identification
  collection_name = 'orders',
  policy_enabled = true,

  -- Retention phases with flexible timing
  active_retention_days = 365,     -- Keep in active storage for 1 year
  archive_after_days = 730,        -- Archive after 2 years
  cold_storage_after_days = 1825,  -- Move to cold storage after 5 years  
  delete_after_days = 2555,        -- Delete after 7 years (regulatory requirement)

  -- Data identification and filtering
  date_field = 'order_date',
  additional_filters = JSON_BUILD_OBJECT(
    'status', JSON_BUILD_ARRAY('completed', 'shipped', 'delivered'),
    'total_amount', JSON_BUILD_OBJECT('$gt', 0)
  ),

  -- Exclude from archiving (VIP customers, ongoing disputes, etc.)
  exclusion_criteria = JSON_BUILD_OBJECT(
    '$or', JSON_BUILD_ARRAY(
      JSON_BUILD_OBJECT('customer_tier', 'vip'),
      JSON_BUILD_OBJECT('dispute_status', 'active'),
      JSON_BUILD_OBJECT('legal_hold', true)
    )
  ),

  -- Archive storage configuration
  archive_storage_type = 'gridfs',
  compression_enabled = true,
  encryption_required = false,
  batch_size = 1000,

  -- Performance optimization
  index_hints = JSON_BUILD_ARRAY('order_date_status_idx', 'customer_id_idx'),
  sort_field = 'order_date',
  memory_limit = '512M',
  max_execution_time_minutes = 180,

  -- Compliance settings
  gdpr_applicable = true,
  legal_hold_exempt = false,
  audit_level = 'detailed',
  pii_fields = JSON_BUILD_ARRAY('customer_email', 'billing_address', 'shipping_address'),

  -- Automation configuration
  execution_schedule = '0 2 * * 0',  -- Weekly on Sunday at 2 AM
  failure_retry_attempts = 3,
  notification_enabled = true,

  -- Business metadata
  business_justification = 'Regulatory compliance and performance optimization',
  data_owner = 'sales_operations_team',
  policy_version = '2.1'
);

-- Advanced customer data retention with PII protection
CREATE RETENTION_POLICY customer_data_lifecycle AS (
  collection_name = 'customers',
  policy_enabled = true,

  -- GDPR-compliant retention periods
  active_retention_days = 1095,    -- 3 years active retention
  archive_after_days = 1825,       -- Archive after 5 years  
  delete_after_days = 2555,        -- Delete after 7 years

  date_field = 'last_activity_date',

  -- PII anonymization before archiving
  pii_protection = JSON_BUILD_OBJECT(
    'anonymize_before_archive', true,
    'pii_fields', JSON_BUILD_ARRAY(
      'email', 'phone', 'address', 'birth_date', 'social_security_number'
    ),
    'anonymization_method', 'hash_with_salt'
  ),

  -- Data subject request handling
  gdpr_compliance = JSON_BUILD_OBJECT(
    'right_to_erasure_enabled', true,
    'data_portability_enabled', true,
    'consent_tracking_required', true,
    'processing_lawfulness_basis', 'legitimate_interest'
  ),

  archive_storage_type = 's3',
  s3_configuration = JSON_BUILD_OBJECT(
    'bucket', 'customer-data-archives',
    'storage_class', 'STANDARD_IA',
    'encryption', 'AES256'
  )
);

-- Execute data archiving with comprehensive monitoring
WITH archiving_execution AS (
  SELECT 
    collection_name,
    policy_id,

    -- Calculate records eligible for archiving
    (SELECT COUNT(*) 
     FROM orders 
     WHERE order_date < CURRENT_DATE - INTERVAL '2 years'
       AND status IN ('completed', 'shipped', 'delivered')
       AND total_amount > 0
       AND NOT (customer_tier = 'vip' OR dispute_status = 'active' OR legal_hold = true)
    ) as eligible_records,

    -- Estimate archive size and processing time
    (SELECT 
       ROUND(AVG(LENGTH(to_jsonb(o)::text))::numeric, 0) * COUNT(*) / 1024 / 1024
     FROM orders o 
     WHERE order_date < CURRENT_DATE - INTERVAL '2 years'
    ) as estimated_archive_size_mb,

    -- Performance projections
    CASE 
      WHEN eligible_records > 100000 THEN 'large_dataset_optimization_required'
      WHEN eligible_records > 10000 THEN 'standard_optimization_recommended'
      ELSE 'minimal_optimization_needed'
    END as performance_category,

    -- Compliance checks
    CASE 
      WHEN EXISTS (
        SELECT 1 FROM legal_hold_registry 
        WHERE collection_name = 'orders' 
        AND hold_status = 'active'
      ) THEN 'legal_hold_active_check_required'
      ELSE 'cleared_for_archiving'
    END as compliance_status

  FROM data_retention_policies 
  WHERE collection_name = 'orders' 
    AND policy_enabled = true
),

-- Execute archiving with batch processing and monitoring
archiving_results AS (
  EXECUTE_ARCHIVING(
    collection_name => 'orders',

    -- Batch processing configuration
    batch_processing => JSON_BUILD_OBJECT(
      'batch_size', 1000,
      'max_concurrent_batches', 3,
      'throttle_delay_ms', 10,
      'memory_limit_per_batch', '100M'
    ),

    -- Performance optimization
    performance_options => JSON_BUILD_OBJECT(
      'use_index_hints', true,
      'parallel_processing', true,
      'compression_level', 'standard',
      'checksum_validation', true
    ),

    -- Archive destination
    archive_destination => JSON_BUILD_OBJECT(
      'storage_type', 'gridfs',
      'bucket_name', 'order_archives',
      'naming_pattern', 'orders_archive_{year}_{month}_{batch}',
      'metadata_tags', JSON_BUILD_OBJECT(
        'department', 'sales',
        'retention_policy', 'order_data_lifecycle',
        'compliance_level', 'standard'
      )
    ),

    -- Compliance and audit settings
    compliance_settings => JSON_BUILD_OBJECT(
      'audit_logging', 'detailed',
      'pii_anonymization', false,  -- Orders don't contain direct PII
      'legal_hold_check', true,
      'gdpr_processing_log', true
    )
  )
)

SELECT 
  ae.collection_name,
  ae.eligible_records,
  ae.estimated_archive_size_mb,
  ae.performance_category,
  ae.compliance_status,

  -- Archiving execution results
  ar.operation_id,
  ar.records_archived,
  ar.archive_size_actual_mb,
  ar.execution_time_seconds,
  ar.batches_processed,

  -- Performance metrics
  ROUND(ar.records_archived::numeric / ar.execution_time_seconds, 2) as records_per_second,
  ROUND(ar.archive_size_actual_mb::numeric / ar.execution_time_seconds, 3) as mb_per_second,

  -- Compliance verification
  ar.compliance_checks_passed,
  ar.audit_trail_id,
  ar.archive_location,
  ar.checksum_verified,

  -- Success indicators
  CASE 
    WHEN ar.records_archived = ae.eligible_records THEN 'complete_success'
    WHEN ar.records_archived > ae.eligible_records * 0.95 THEN 'successful_with_minor_issues'
    WHEN ar.records_archived > 0 THEN 'partial_success_requires_review'
    ELSE 'failed_requires_investigation'
  END as execution_status,

  -- Recommendations for optimization
  CASE 
    WHEN ar.records_per_second < 10 THEN 'consider_batch_size_increase'
    WHEN ar.execution_time_seconds > 3600 THEN 'consider_parallel_processing_increase'
    WHEN ar.archive_size_actual_mb > ae.estimated_archive_size_mb * 1.5 THEN 'investigate_compression_efficiency'
    ELSE 'performance_within_expected_parameters'
  END as optimization_recommendation

FROM archiving_execution ae
CROSS JOIN archiving_results ar;

-- Monitor archiving operations with real-time dashboard
WITH current_archiving_operations AS (
  SELECT 
    operation_id,
    collection_name,
    policy_id,
    operation_type,
    started_at,

    -- Progress tracking
    records_processed,
    estimated_total_records,
    ROUND((records_processed::numeric / estimated_total_records) * 100, 1) as progress_percentage,

    -- Performance monitoring
    EXTRACT(SECONDS FROM CURRENT_TIMESTAMP - started_at) as elapsed_seconds,
    ROUND(records_processed::numeric / EXTRACT(SECONDS FROM CURRENT_TIMESTAMP - started_at), 2) as current_throughput,

    -- Resource utilization
    memory_usage_mb,
    cpu_utilization_percent,
    io_operations_per_second,

    -- Status indicators
    operation_status,
    error_count,
    last_error_message,

    -- ETA calculation
    CASE 
      WHEN records_processed > 0 AND operation_status = 'running' THEN
        CURRENT_TIMESTAMP + 
        (INTERVAL '1 second' * 
         ((estimated_total_records - records_processed) / 
          (records_processed::numeric / EXTRACT(SECONDS FROM CURRENT_TIMESTAMP - started_at))))
      ELSE NULL
    END as estimated_completion_time

  FROM lifecycle_operation_status
  WHERE operation_status IN ('running', 'paused', 'starting')
),

-- Historical performance analysis
archiving_performance_trends AS (
  SELECT 
    DATE_TRUNC('day', execution_timestamp) as execution_date,
    collection_name,

    -- Daily aggregated metrics
    COUNT(*) as operations_executed,
    SUM(records_processed) as total_records_archived,
    AVG(execution_duration_seconds) as avg_execution_time,
    AVG(records_processed::numeric / execution_duration_seconds) as avg_throughput,

    -- Success rate tracking
    COUNT(*) FILTER (WHERE status = 'success') as successful_operations,
    ROUND(
      (COUNT(*) FILTER (WHERE status = 'success')::numeric / COUNT(*)) * 100, 1
    ) as success_rate_percent,

    -- Resource efficiency metrics
    AVG(archive_size_mb::numeric / execution_duration_seconds) as avg_mb_per_second,
    AVG(memory_peak_usage_mb) as avg_peak_memory_usage,

    -- Trend indicators
    LAG(SUM(records_processed)) OVER (
      PARTITION BY collection_name 
      ORDER BY DATE_TRUNC('day', execution_timestamp)
    ) as previous_day_records,

    LAG(AVG(records_processed::numeric / execution_duration_seconds)) OVER (
      PARTITION BY collection_name
      ORDER BY DATE_TRUNC('day', execution_timestamp)  
    ) as previous_day_throughput

  FROM lifecycle_execution_audit
  WHERE execution_timestamp >= CURRENT_DATE - INTERVAL '30 days'
    AND operation_type = 'archive'
  GROUP BY DATE_TRUNC('day', execution_timestamp), collection_name
),

-- Data retention compliance dashboard
retention_compliance_status AS (
  SELECT 
    drp.collection_name,
    drp.policy_id,
    drp.policy_enabled,

    -- Current data status
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLLECTIONS 
     WHERE collection_name = drp.collection_name) as active_record_count,

    -- Retention phase analysis
    CASE 
      WHEN drp.active_retention_days IS NOT NULL THEN
        (SELECT COUNT(*) 
         FROM dynamic_collection_query(drp.collection_name)
         WHERE date_field_value < CURRENT_DATE - (drp.active_retention_days || ' days')::INTERVAL)
      ELSE 0
    END as records_past_active_retention,

    CASE 
      WHEN drp.archive_after_days IS NOT NULL THEN
        (SELECT COUNT(*) 
         FROM dynamic_collection_query(drp.collection_name)
         WHERE date_field_value < CURRENT_DATE - (drp.archive_after_days || ' days')::INTERVAL)
      ELSE 0
    END as records_ready_for_archive,

    CASE 
      WHEN drp.delete_after_days IS NOT NULL THEN
        (SELECT COUNT(*) 
         FROM dynamic_collection_query(drp.collection_name)
         WHERE date_field_value < CURRENT_DATE - (drp.delete_after_days || ' days')::INTERVAL)
      ELSE 0
    END as records_past_deletion_date,

    -- Compliance indicators
    CASE 
      WHEN records_past_deletion_date > 0 THEN 'non_compliant_immediate_attention'
      WHEN records_ready_for_archive > 10000 THEN 'compliance_risk_action_needed'
      WHEN records_past_active_retention > active_record_count * 0.3 THEN 'optimization_opportunity'
      ELSE 'compliant'
    END as compliance_status,

    -- Archive statistics
    (SELECT COUNT(*) FROM archive_metadata WHERE source_collection = drp.collection_name) as total_archives_created,
    (SELECT SUM(record_count) FROM archive_metadata WHERE source_collection = drp.collection_name) as total_records_archived,
    (SELECT MAX(archive_date) FROM archive_metadata WHERE source_collection = drp.collection_name) as last_archive_date,

    -- Next scheduled execution
    drp.next_execution_scheduled,
    EXTRACT(HOURS FROM drp.next_execution_scheduled - CURRENT_TIMESTAMP) as hours_until_next_execution

  FROM data_retention_policies drp
  WHERE drp.policy_enabled = true
)

SELECT 
  -- Current operations status
  'ACTIVE_OPERATIONS' as section,
  JSON_AGG(
    JSON_BUILD_OBJECT(
      'operation_id', cao.operation_id,
      'collection', cao.collection_name,
      'progress', cao.progress_percentage || '%',
      'throughput', cao.current_throughput || ' rec/sec',
      'eta', cao.estimated_completion_time,
      'status', cao.operation_status
    )
  ) as current_operations

FROM current_archiving_operations cao
WHERE cao.operation_status = 'running'

UNION ALL

SELECT 
  -- Performance trends
  'PERFORMANCE_TRENDS' as section,
  JSON_AGG(
    JSON_BUILD_OBJECT(
      'date', apt.execution_date,
      'collection', apt.collection_name,
      'records_archived', apt.total_records_archived,
      'avg_throughput', apt.avg_throughput || ' rec/sec',
      'success_rate', apt.success_rate_percent || '%',
      'trend', CASE 
        WHEN apt.avg_throughput > apt.previous_day_throughput * 1.1 THEN 'improving'
        WHEN apt.avg_throughput < apt.previous_day_throughput * 0.9 THEN 'declining'
        ELSE 'stable'
      END
    )
  ) as performance_data

FROM archiving_performance_trends apt
WHERE apt.execution_date >= CURRENT_DATE - INTERVAL '7 days'

UNION ALL

SELECT 
  -- Compliance status
  'COMPLIANCE_STATUS' as section,
  JSON_AGG(
    JSON_BUILD_OBJECT(
      'collection', rcs.collection_name,
      'compliance_status', rcs.compliance_status,
      'active_records', rcs.active_record_count,
      'ready_for_archive', rcs.records_ready_for_archive,
      'past_deletion_date', rcs.records_past_deletion_date,
      'last_archive', rcs.last_archive_date,
      'next_execution', rcs.hours_until_next_execution || ' hours',
      'total_archived', rcs.total_records_archived
    )
  ) as compliance_data

FROM retention_compliance_status rcs;

-- Advanced archive data retrieval with query optimization
WITH archive_query_optimization AS (
  SELECT 
    archive_id,
    source_collection,
    archive_date,
    record_count,
    archive_size_mb,
    storage_type,
    archive_location,

    -- Query complexity assessment
    CASE 
      WHEN record_count > 1000000 THEN 'complex_query_optimization_required'
      WHEN record_count > 100000 THEN 'standard_optimization_recommended'  
      ELSE 'direct_query_suitable'
    END as query_complexity,

    -- Storage access strategy
    CASE storage_type
      WHEN 'mongodb' THEN 'direct_collection_access'
      WHEN 'gridfs' THEN 'streaming_batch_retrieval'
      WHEN 's3' THEN 'cloud_storage_download_and_parse'
      ELSE 'custom_retrieval_strategy'
    END as retrieval_strategy

  FROM archive_metadata
  WHERE source_collection = 'orders'
    AND archive_date >= CURRENT_DATE - INTERVAL '1 year'
)

-- Execute optimized archive data retrieval
SELECT 
  RETRIEVE_ARCHIVED_DATA(
    archive_id => aqo.archive_id,

    -- Query parameters
    query_filter => JSON_BUILD_OBJECT(
      'customer_id', '507f1f77bcf86cd799439011',
      'total_amount', JSON_BUILD_OBJECT('$gte', 100),
      'order_date', JSON_BUILD_OBJECT(
        '$gte', '2023-01-01',
        '$lte', '2023-12-31'
      )
    ),

    -- Retrieval optimization
    retrieval_options => JSON_BUILD_OBJECT(
      'batch_size', CASE 
        WHEN aqo.query_complexity = 'complex_query_optimization_required' THEN 100
        WHEN aqo.query_complexity = 'standard_optimization_recommended' THEN 500
        ELSE 1000
      END,
      'parallel_processing', aqo.query_complexity != 'direct_query_suitable',
      'result_streaming', aqo.record_count > 10000,
      'compression_handling', 'automatic'
    ),

    -- Performance settings
    performance_limits => JSON_BUILD_OBJECT(
      'max_execution_time_seconds', 300,
      'memory_limit_mb', 256,
      'max_results', 10000
    )
  ) as retrieval_results

FROM archive_query_optimization aqo
WHERE aqo.archive_id IN (
  SELECT archive_id 
  FROM archive_metadata 
  WHERE source_collection = 'orders'
  ORDER BY archive_date DESC 
  LIMIT 5
);

-- QueryLeaf data lifecycle management features:
-- 1. SQL-familiar syntax for MongoDB data retention policy definition
-- 2. Automated archiving execution with batch processing and performance optimization
-- 3. Comprehensive compliance management including GDPR, legal holds, and audit trails
-- 4. Real-time monitoring dashboard for archiving operations and performance metrics
-- 5. Advanced archive data retrieval with query optimization and result streaming
-- 6. Intelligent data lifecycle automation with predictive analysis capabilities
-- 7. Multi-tier storage integration supporting MongoDB, GridFS, S3, and custom storage
-- 8. Performance-aware processing with resource throttling and system impact minimization
-- 9. Enterprise compliance workflows with automated reporting and alert generation
-- 10. Cost optimization strategies with intelligent storage tiering and compression

Best Practices for MongoDB Data Lifecycle Management

Archiving Strategy Design

Essential principles for effective MongoDB data archiving and lifecycle management:

  1. Policy-Driven Approach: Define comprehensive retention policies based on business requirements, regulatory compliance, and performance optimization goals
  2. Performance Optimization: Implement batch processing, indexing strategies, and resource throttling to minimize impact on production systems
  3. Compliance Integration: Build automated compliance workflows that address regulatory requirements like GDPR, HIPAA, and industry-specific standards
  4. Storage Optimization: Utilize multi-tier storage strategies with compression, encryption, and geographic distribution for cost and performance optimization
  5. Monitoring and Alerting: Deploy comprehensive monitoring systems that track archiving performance, compliance status, and operational health
  6. Recovery Planning: Design archive retrieval processes that support both routine access and emergency recovery scenarios

Production Deployment Strategies

Optimize MongoDB data lifecycle management for enterprise-scale requirements:

  1. Automated Execution: Implement scheduled archiving processes with intelligent failure recovery and retry mechanisms
  2. Resource Management: Configure memory limits, CPU throttling, and I/O optimization to prevent system impact during archiving operations
  3. Compliance Automation: Deploy automated compliance reporting, audit trail generation, and regulatory requirement enforcement
  4. Cost Optimization: Implement intelligent storage tiering that automatically moves data to appropriate storage classes based on access patterns
  5. Performance Monitoring: Monitor archiving throughput, resource utilization, and system performance to optimize operations
  6. Security Integration: Ensure data encryption, access controls, and audit logging meet enterprise security requirements

Conclusion

MongoDB data lifecycle management provides comprehensive capabilities for automated data archiving, compliance enforcement, and performance optimization that scale from simple retention policies to enterprise-wide governance programs. The flexible document-based architecture and built-in lifecycle features enable sophisticated archiving strategies that adapt to changing business requirements while maintaining operational efficiency.

Key MongoDB Data Lifecycle Management benefits include:

  • Automated Governance: Policy-driven data lifecycle management with minimal manual intervention and maximum compliance assurance
  • Performance Optimization: Intelligent archiving processes that maintain production system performance while managing large-scale data movement
  • Compliance Excellence: Built-in support for regulatory requirements including GDPR, industry standards, and legal hold management
  • Cost Efficiency: Multi-tier storage strategies with automated optimization that reduce storage costs while maintaining data accessibility
  • Operational Simplicity: Streamlined management processes that reduce administrative overhead while ensuring data governance
  • Scalable Architecture: Enterprise-ready capabilities that support growing data volumes and evolving compliance requirements

Whether you're building regulatory compliance systems, optimizing database performance, managing storage costs, or implementing enterprise data governance, MongoDB's data lifecycle management capabilities with QueryLeaf's familiar SQL interface provide the foundation for comprehensive, automated data archiving at scale.

QueryLeaf Integration: QueryLeaf automatically translates SQL-style data lifecycle management commands into optimized MongoDB operations, providing familiar retention policy syntax, archiving execution commands, and compliance reporting queries. Advanced lifecycle management patterns, performance optimization, and regulatory compliance workflows are seamlessly accessible through familiar SQL constructs, making sophisticated data governance both powerful and approachable for SQL-oriented operations teams.

The combination of MongoDB's flexible data lifecycle capabilities with SQL-style governance operations makes it an ideal platform for modern data management applications that require both comprehensive archiving functionality and operational simplicity, ensuring your data governance programs can scale efficiently while meeting evolving regulatory and business requirements.

MongoDB GridFS File Storage Management: Advanced Strategies for Large File Handling, Streaming, and Content Distribution with SQL-Style File Operations

Modern applications require sophisticated file storage solutions that can handle large media files, document repositories, streaming content, and complex file management workflows while maintaining high performance, scalability, and reliability across distributed systems. Traditional file storage approaches often struggle with large file limitations, metadata management complexity, and the challenges of integrating file operations with database transactions, leading to performance bottlenecks, storage inefficiencies, and operational complexity in production environments.

MongoDB GridFS provides comprehensive large file storage capabilities through intelligent file chunking, sophisticated metadata management, and seamless integration with MongoDB's document database features that enable applications to store, retrieve, and stream files of any size while maintaining ACID transaction support and distributed system reliability. Unlike traditional file systems that impose size limitations and separate file metadata from database operations, GridFS integrates advanced file storage directly with MongoDB's query engine, indexing capabilities, and replication features.

The Traditional File Storage Challenge

Conventional approaches to large file storage in enterprise applications face significant limitations in scalability and integration:

-- Traditional PostgreSQL file storage - limited and fragmented approach

-- Basic file metadata table (limited capabilities)
CREATE TABLE file_metadata (
    file_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    filename VARCHAR(255) NOT NULL,
    file_path VARCHAR(500) NOT NULL,
    file_size BIGINT NOT NULL,

    -- Basic file information
    mime_type VARCHAR(100),
    file_extension VARCHAR(10),
    original_filename VARCHAR(255),

    -- Simple metadata (limited structure)
    file_description TEXT,
    file_category VARCHAR(50),
    tags TEXT[], -- Basic array support

    -- Upload information
    uploaded_by UUID,
    uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Storage information (filesystem dependent)
    storage_location VARCHAR(100) DEFAULT 'local', -- local, s3, azure, gcs
    storage_path VARCHAR(500),
    storage_bucket VARCHAR(100),

    -- Basic versioning (very limited)
    version_number INTEGER DEFAULT 1,
    is_current_version BOOLEAN DEFAULT TRUE,
    parent_file_id UUID REFERENCES file_metadata(file_id),

    -- Simple access control
    is_public BOOLEAN DEFAULT FALSE,
    access_permissions JSONB,

    -- Basic status tracking
    processing_status VARCHAR(20) DEFAULT 'uploaded', -- uploaded, processing, ready, error

    -- Audit fields
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- File chunks table for large file handling (manual implementation)
CREATE TABLE file_chunks (
    chunk_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES file_metadata(file_id) ON DELETE CASCADE,
    chunk_number INTEGER NOT NULL,
    chunk_size INTEGER NOT NULL,

    -- Chunk data (limited by database constraints)
    chunk_data BYTEA, -- Limited to ~1GB in PostgreSQL

    -- Chunk integrity
    chunk_checksum VARCHAR(64), -- MD5 or SHA256 hash

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    UNIQUE (file_id, chunk_number)
);

-- File access log (basic tracking)
CREATE TABLE file_access_log (
    access_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES file_metadata(file_id),

    -- Access information
    accessed_by UUID,
    access_type VARCHAR(20), -- read, write, delete, stream
    access_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Request details
    client_ip INET,
    user_agent TEXT,
    request_method VARCHAR(10),

    -- Response information
    bytes_transferred BIGINT,
    response_status INTEGER,
    response_time_ms INTEGER,

    -- Streaming information (limited)
    stream_start_position BIGINT DEFAULT 0,
    stream_end_position BIGINT,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Complex query to manage file operations (expensive and limited)
WITH file_statistics AS (
    SELECT 
        fm.file_id,
        fm.filename,
        fm.file_size,
        fm.mime_type,
        fm.storage_location,
        fm.processing_status,

        -- Calculate chunk information (expensive operation)
        COUNT(fc.chunk_id) as total_chunks,
        SUM(fc.chunk_size) as total_chunk_size,

        -- Basic integrity check
        CASE 
            WHEN fm.file_size = SUM(fc.chunk_size) THEN 'intact'
            WHEN SUM(fc.chunk_size) IS NULL THEN 'no_chunks'
            ELSE 'corrupted'
        END as file_integrity,

        -- Recent access statistics (limited analysis)
        COUNT(CASE WHEN fal.access_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours' 
                   THEN 1 END) as daily_access_count,
        COUNT(CASE WHEN fal.access_timestamp >= CURRENT_TIMESTAMP - INTERVAL '7 days' 
                   THEN 1 END) as weekly_access_count,

        -- Data transfer statistics
        SUM(CASE WHEN fal.access_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours' 
                 THEN fal.bytes_transferred ELSE 0 END) as daily_bytes_transferred,

        -- Performance metrics
        AVG(CASE WHEN fal.access_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours' 
                 THEN fal.response_time_ms END) as avg_response_time_ms

    FROM file_metadata fm
    LEFT JOIN file_chunks fc ON fm.file_id = fc.file_id
    LEFT JOIN file_access_log fal ON fm.file_id = fal.file_id
    WHERE fm.is_current_version = TRUE
    GROUP BY fm.file_id, fm.filename, fm.file_size, fm.mime_type, 
             fm.storage_location, fm.processing_status
),

storage_analysis AS (
    SELECT 
        storage_location,
        COUNT(*) as file_count,
        SUM(file_size) as total_storage_bytes,
        AVG(file_size) as avg_file_size,

        -- Storage health indicators
        COUNT(CASE WHEN file_integrity = 'corrupted' THEN 1 END) as corrupted_files,
        COUNT(CASE WHEN processing_status = 'error' THEN 1 END) as error_files,

        -- Access patterns
        AVG(daily_access_count) as avg_daily_access,
        SUM(daily_bytes_transferred) as total_daily_transfer,

        -- Performance indicators
        AVG(avg_response_time_ms) as avg_response_time

    FROM file_statistics
    GROUP BY storage_location
)

SELECT 
    fs.filename,
    fs.file_size,
    fs.mime_type,
    fs.storage_location,
    fs.total_chunks,
    fs.file_integrity,
    fs.processing_status,

    -- Access metrics
    fs.daily_access_count,
    fs.weekly_access_count,
    fs.avg_response_time_ms,

    -- Data transfer
    ROUND(fs.daily_bytes_transferred / 1024.0 / 1024.0, 2) as daily_mb_transferred,

    -- Storage efficiency (limited calculation)
    ROUND((fs.total_chunk_size::DECIMAL / fs.file_size) * 100, 2) as storage_efficiency_percent,

    -- Health indicators
    CASE 
        WHEN fs.file_integrity = 'corrupted' THEN 'Critical - File Corrupted'
        WHEN fs.processing_status = 'error' THEN 'Error - Processing Failed'
        WHEN fs.avg_response_time_ms > 5000 THEN 'Warning - Slow Response'
        WHEN fs.daily_access_count > 1000 THEN 'High Usage'
        ELSE 'Normal'
    END as file_status

FROM file_statistics fs
ORDER BY fs.daily_access_count DESC, fs.file_size DESC
LIMIT 100;

-- Problems with traditional file storage approach:
-- 1. Database size limitations prevent storing large files
-- 2. Manual chunking implementation is complex and error-prone
-- 3. Limited integration between file operations and database transactions
-- 4. Poor performance for streaming and partial file access
-- 5. Complex metadata management across multiple tables
-- 6. Limited support for file versioning and content management
-- 7. Expensive joins required for file operations
-- 8. No built-in support for distributed file storage
-- 9. Manual implementation of file integrity and consistency checks
-- 10. Limited indexing and query capabilities for file metadata

MongoDB GridFS eliminates these limitations with intelligent file management:

// MongoDB GridFS - comprehensive large file storage and management
const { MongoClient, GridFSBucket } = require('mongodb');
const crypto = require('crypto');
const fs = require('fs');

// Advanced GridFS file management system
class MongoGridFSManager {
  constructor(client, databaseName, bucketName = 'files') {
    this.client = client;
    this.db = client.db(databaseName);
    this.bucket = new GridFSBucket(this.db, { 
      bucketName: bucketName,
      chunkSizeBytes: 1024 * 1024 // 1MB chunks for optimal performance
    });

    this.fileMetrics = {
      totalUploads: 0,
      totalDownloads: 0,
      totalStreams: 0,
      bytesUploaded: 0,
      bytesDownloaded: 0,
      averageUploadTime: 0,
      averageDownloadTime: 0,
      errorCount: 0
    };
  }

  // Upload large files with comprehensive metadata and progress tracking
  async uploadFile(filePath, options = {}) {
    const startTime = Date.now();

    try {
      // Generate comprehensive file metadata
      const fileStats = fs.statSync(filePath);
      const filename = options.filename || path.basename(filePath);

      // Create file hash for integrity checking
      const fileHash = await this.generateFileHash(filePath);

      // Comprehensive metadata for advanced file management
      const metadata = {
        // Basic file information
        originalName: filename,
        uploadedAt: new Date(),
        fileSize: fileStats.size,
        mimeType: options.mimeType || this.detectMimeType(filename),

        // File integrity and versioning
        md5Hash: fileHash.md5,
        sha256Hash: fileHash.sha256,
        version: options.version || 1,
        parentFileId: options.parentFileId || null,

        // Content management
        description: options.description || '',
        category: options.category || 'general',
        tags: options.tags || [],

        // Access control and permissions
        uploadedBy: options.uploadedBy || 'system',
        isPublic: options.isPublic || false,
        accessPermissions: options.accessPermissions || { read: ['authenticated'] },

        // Processing and workflow
        processingStatus: 'uploaded',
        processingMetadata: {},

        // Content-specific metadata
        contentMetadata: options.contentMetadata || {},

        // Storage and performance optimization
        compressionType: options.compression || 'none',
        encryptionStatus: options.encrypted || false,
        storageClass: options.storageClass || 'standard', // standard, archival, frequent_access

        // Business context
        projectId: options.projectId,
        customFields: options.customFields || {},

        // Audit and compliance
        retentionPolicy: options.retentionPolicy || 'standard',
        complianceFlags: options.complianceFlags || [],

        // Performance tracking
        uploadDuration: null, // Will be set after upload completes
        lastAccessedAt: new Date(),
        accessCount: 0,
        totalBytesTransferred: 0
      };

      return new Promise((resolve, reject) => {
        // Create upload stream with progress tracking
        const uploadStream = this.bucket.openUploadStream(filename, {
          metadata: metadata,
          chunkSizeBytes: options.chunkSize || (1024 * 1024) // 1MB default chunks
        });

        // Progress tracking variables
        let bytesUploaded = 0;
        const totalBytes = fileStats.size;

        // Create read stream from file
        const readStream = fs.createReadStream(filePath);

        // Progress tracking
        readStream.on('data', (chunk) => {
          bytesUploaded += chunk.length;

          if (options.onProgress) {
            const progress = {
              bytesUploaded: bytesUploaded,
              totalBytes: totalBytes,
              percentage: (bytesUploaded / totalBytes) * 100,
              remainingBytes: totalBytes - bytesUploaded,
              elapsedTime: Date.now() - startTime
            };
            options.onProgress(progress);
          }
        });

        // Handle upload completion
        uploadStream.on('finish', async () => {
          const uploadDuration = Date.now() - startTime;

          // Update file metadata with final upload information
          await this.db.collection(`${this.bucket.options.bucketName}.files`).updateOne(
            { _id: uploadStream.id },
            { 
              $set: { 
                'metadata.uploadDuration': uploadDuration,
                'metadata.uploadCompletedAt': new Date()
              }
            }
          );

          // Update metrics
          this.fileMetrics.totalUploads++;
          this.fileMetrics.bytesUploaded += totalBytes;
          this.fileMetrics.averageUploadTime = 
            (this.fileMetrics.averageUploadTime + uploadDuration) / this.fileMetrics.totalUploads;

          console.log(`File uploaded successfully: ${filename} (${totalBytes} bytes, ${uploadDuration}ms)`);

          resolve({
            fileId: uploadStream.id,
            filename: filename,
            size: totalBytes,
            uploadDuration: uploadDuration,
            metadata: metadata,
            chunksCount: Math.ceil(totalBytes / (options.chunkSize || (1024 * 1024)))
          });
        });

        // Handle upload errors
        uploadStream.on('error', (error) => {
          this.fileMetrics.errorCount++;
          console.error('Upload error:', error);
          reject(error);
        });

        // Start the upload
        readStream.pipe(uploadStream);
      });

    } catch (error) {
      this.fileMetrics.errorCount++;
      console.error('File upload error:', error);
      throw error;
    }
  }

  // Advanced file streaming with range support and performance optimization
  async streamFile(fileId, options = {}) {
    const startTime = Date.now();

    try {
      // Get file information for streaming optimization
      const fileInfo = await this.getFileInfo(fileId);
      if (!fileInfo) {
        throw new Error('File not found');
      }

      // Update access metrics
      await this.updateAccessMetrics(fileId);

      // Create download stream with optional range support
      const downloadOptions = {};

      // Support for HTTP range requests (partial content)
      if (options.start !== undefined || options.end !== undefined) {
        downloadOptions.start = options.start || 0;
        downloadOptions.end = options.end || fileInfo.length - 1;

        console.log(`Streaming file range: ${downloadOptions.start}-${downloadOptions.end}/${fileInfo.length}`);
      }

      const downloadStream = this.bucket.openDownloadStream(fileId, downloadOptions);

      // Track streaming metrics
      let bytesStreamed = 0;

      downloadStream.on('data', (chunk) => {
        bytesStreamed += chunk.length;

        if (options.onProgress) {
          const progress = {
            bytesStreamed: bytesStreamed,
            totalBytes: fileInfo.length,
            percentage: (bytesStreamed / fileInfo.length) * 100,
            elapsedTime: Date.now() - startTime
          };
          options.onProgress(progress);
        }
      });

      downloadStream.on('end', () => {
        const streamDuration = Date.now() - startTime;

        // Update metrics
        this.fileMetrics.totalStreams++;
        this.fileMetrics.bytesDownloaded += bytesStreamed;
        this.fileMetrics.averageDownloadTime = 
          (this.fileMetrics.averageDownloadTime + streamDuration) / this.fileMetrics.totalStreams;

        console.log(`File streamed: ${fileInfo.filename} (${bytesStreamed} bytes, ${streamDuration}ms)`);
      });

      downloadStream.on('error', (error) => {
        this.fileMetrics.errorCount++;
        console.error('Streaming error:', error);
      });

      return downloadStream;

    } catch (error) {
      this.fileMetrics.errorCount++;
      console.error('File streaming error:', error);
      throw error;
    }
  }

  // Comprehensive file search and metadata querying
  async searchFiles(query = {}, options = {}) {
    try {
      const searchCriteria = {};

      // Build comprehensive search query
      if (query.filename) {
        searchCriteria.filename = new RegExp(query.filename, 'i');
      }

      if (query.mimeType) {
        searchCriteria['metadata.mimeType'] = query.mimeType;
      }

      if (query.category) {
        searchCriteria['metadata.category'] = query.category;
      }

      if (query.tags && query.tags.length > 0) {
        searchCriteria['metadata.tags'] = { $in: query.tags };
      }

      if (query.uploadedBy) {
        searchCriteria['metadata.uploadedBy'] = query.uploadedBy;
      }

      if (query.dateRange) {
        searchCriteria.uploadDate = {};
        if (query.dateRange.from) {
          searchCriteria.uploadDate.$gte = new Date(query.dateRange.from);
        }
        if (query.dateRange.to) {
          searchCriteria.uploadDate.$lte = new Date(query.dateRange.to);
        }
      }

      if (query.sizeRange) {
        searchCriteria.length = {};
        if (query.sizeRange.min) {
          searchCriteria.length.$gte = query.sizeRange.min;
        }
        if (query.sizeRange.max) {
          searchCriteria.length.$lte = query.sizeRange.max;
        }
      }

      if (query.isPublic !== undefined) {
        searchCriteria['metadata.isPublic'] = query.isPublic;
      }

      // Full-text search in description and custom fields
      if (query.textSearch) {
        searchCriteria.$or = [
          { 'metadata.description': new RegExp(query.textSearch, 'i') },
          { 'metadata.customFields': new RegExp(query.textSearch, 'i') }
        ];
      }

      // Execute search with aggregation pipeline for advanced features
      const pipeline = [
        { $match: searchCriteria },

        // Add computed fields for enhanced results
        {
          $addFields: {
            fileSizeMB: { $divide: ['$length', 1024 * 1024] },
            uploadAge: { 
              $divide: [
                { $subtract: [new Date(), '$uploadDate'] },
                1000 * 60 * 60 * 24 // Convert to days
              ]
            }
          }
        },

        // Sort by relevance and recency
        {
          $sort: options.sortBy === 'size' ? { length: -1 } :
                 options.sortBy === 'name' ? { filename: 1 } :
                 { uploadDate: -1 } // Default: newest first
        },

        // Pagination
        { $skip: options.skip || 0 },
        { $limit: options.limit || 50 },

        // Project only needed fields for performance
        {
          $project: {
            _id: 1,
            filename: 1,
            length: 1,
            fileSizeMB: 1,
            uploadDate: 1,
            uploadAge: 1,
            md5: 1,
            'metadata.mimeType': 1,
            'metadata.category': 1,
            'metadata.tags': 1,
            'metadata.description': 1,
            'metadata.uploadedBy': 1,
            'metadata.isPublic': 1,
            'metadata.accessCount': 1,
            'metadata.lastAccessedAt': 1,
            'metadata.processingStatus': 1
          }
        }
      ];

      const files = await this.db.collection(`${this.bucket.options.bucketName}.files`)
        .aggregate(pipeline)
        .toArray();

      // Get total count for pagination
      const totalCount = await this.db.collection(`${this.bucket.options.bucketName}.files`)
        .countDocuments(searchCriteria);

      return {
        files: files,
        totalCount: totalCount,
        hasMore: (options.skip || 0) + files.length < totalCount,
        searchCriteria: searchCriteria,
        executionTime: Date.now()
      };

    } catch (error) {
      console.error('File search error:', error);
      throw error;
    }
  }

  // File versioning and content management
  async createFileVersion(originalFileId, newFilePath, versionOptions = {}) {
    try {
      // Get original file information
      const originalFile = await this.getFileInfo(originalFileId);
      if (!originalFile) {
        throw new Error('Original file not found');
      }

      // Create new version with inherited metadata
      const versionMetadata = {
        ...originalFile.metadata,
        version: (originalFile.metadata.version || 1) + 1,
        parentFileId: originalFileId,
        versionDescription: versionOptions.description || '',
        versionCreatedAt: new Date(),
        versionCreatedBy: versionOptions.createdBy || 'system',
        changeLog: versionOptions.changeLog || []
      };

      // Upload new version
      const uploadResult = await this.uploadFile(newFilePath, {
        filename: originalFile.filename,
        metadata: versionMetadata,
        ...versionOptions
      });

      // Update version tracking
      await this.updateVersionHistory(originalFileId, uploadResult.fileId);

      return {
        newVersionId: uploadResult.fileId,
        versionNumber: versionMetadata.version,
        originalFileId: originalFileId,
        uploadResult: uploadResult
      };

    } catch (error) {
      console.error('File versioning error:', error);
      throw error;
    }
  }

  // Advanced file analytics and reporting
  async getFileAnalytics(timeRange = '30d') {
    try {
      const now = new Date();
      const timeRanges = {
        '1d': 1,
        '7d': 7,
        '30d': 30,
        '90d': 90,
        '365d': 365
      };

      const days = timeRanges[timeRange] || 30;
      const startDate = new Date(now.getTime() - (days * 24 * 60 * 60 * 1000));

      // Comprehensive analytics aggregation
      const analyticsResults = await Promise.all([

        // Storage analytics
        this.db.collection(`${this.bucket.options.bucketName}.files`).aggregate([
          {
            $group: {
              _id: null,
              totalFiles: { $sum: 1 },
              totalStorageBytes: { $sum: '$length' },
              averageFileSize: { $avg: '$length' },
              largestFile: { $max: '$length' },
              smallestFile: { $min: '$length' }
            }
          }
        ]).toArray(),

        // Upload trends
        this.db.collection(`${this.bucket.options.bucketName}.files`).aggregate([
          {
            $match: {
              uploadDate: { $gte: startDate }
            }
          },
          {
            $group: {
              _id: {
                year: { $year: '$uploadDate' },
                month: { $month: '$uploadDate' },
                day: { $dayOfMonth: '$uploadDate' }
              },
              dailyUploads: { $sum: 1 },
              dailyStorageAdded: { $sum: '$length' }
            }
          },
          {
            $sort: { '_id.year': 1, '_id.month': 1, '_id.day': 1 }
          }
        ]).toArray(),

        // File type distribution
        this.db.collection(`${this.bucket.options.bucketName}.files`).aggregate([
          {
            $group: {
              _id: '$metadata.mimeType',
              fileCount: { $sum: 1 },
              totalSize: { $sum: '$length' },
              averageSize: { $avg: '$length' }
            }
          },
          {
            $sort: { fileCount: -1 }
          },
          {
            $limit: 20
          }
        ]).toArray(),

        // Category analysis
        this.db.collection(`${this.bucket.options.bucketName}.files`).aggregate([
          {
            $group: {
              _id: '$metadata.category',
              fileCount: { $sum: 1 },
              totalSize: { $sum: '$length' }
            }
          },
          {
            $sort: { fileCount: -1 }
          }
        ]).toArray(),

        // Access patterns
        this.db.collection(`${this.bucket.options.bucketName}.files`).aggregate([
          {
            $match: {
              'metadata.lastAccessedAt': { $gte: startDate }
            }
          },
          {
            $group: {
              _id: null,
              averageAccessCount: { $avg: '$metadata.accessCount' },
              totalBytesTransferred: { $sum: '$metadata.totalBytesTransferred' },
              mostAccessedFiles: { $push: {
                filename: '$filename',
                accessCount: '$metadata.accessCount'
              }}
            }
          }
        ]).toArray()
      ]);

      // Compile comprehensive analytics report
      const [storageStats, uploadTrends, fileTypeStats, categoryStats, accessStats] = analyticsResults;

      const analytics = {
        reportGeneratedAt: new Date(),
        timeRange: timeRange,

        // Storage overview
        storage: storageStats[0] || {
          totalFiles: 0,
          totalStorageBytes: 0,
          averageFileSize: 0,
          largestFile: 0,
          smallestFile: 0
        },

        // Upload trends
        uploadTrends: uploadTrends,

        // File type distribution
        fileTypes: fileTypeStats,

        // Category distribution
        categories: categoryStats,

        // Access patterns
        accessPatterns: accessStats[0] || {
          averageAccessCount: 0,
          totalBytesTransferred: 0,
          mostAccessedFiles: []
        },

        // Performance metrics
        performanceMetrics: {
          ...this.fileMetrics,
          reportedAt: new Date()
        },

        // Storage efficiency calculations
        efficiency: {
          storageUtilizationMB: Math.round((storageStats[0]?.totalStorageBytes || 0) / (1024 * 1024)),
          averageFileSizeMB: Math.round((storageStats[0]?.averageFileSize || 0) / (1024 * 1024)),
          chunksPerFile: Math.ceil((storageStats[0]?.averageFileSize || 0) / (1024 * 1024)), // Assumes 1MB chunks
          compressionRatio: 1.0 // Would be calculated from actual compression data
        }
      };

      return analytics;

    } catch (error) {
      console.error('Analytics generation error:', error);
      throw error;
    }
  }

  // Utility methods for file operations
  async getFileInfo(fileId) {
    try {
      const fileInfo = await this.db.collection(`${this.bucket.options.bucketName}.files`)
        .findOne({ _id: fileId });
      return fileInfo;
    } catch (error) {
      console.error('Get file info error:', error);
      return null;
    }
  }

  async updateAccessMetrics(fileId) {
    try {
      await this.db.collection(`${this.bucket.options.bucketName}.files`).updateOne(
        { _id: fileId },
        {
          $inc: { 'metadata.accessCount': 1 },
          $set: { 'metadata.lastAccessedAt': new Date() }
        }
      );
    } catch (error) {
      console.error('Access metrics update error:', error);
    }
  }

  async generateFileHash(filePath) {
    return new Promise((resolve, reject) => {
      const md5Hash = crypto.createHash('md5');
      const sha256Hash = crypto.createHash('sha256');
      const stream = fs.createReadStream(filePath);

      stream.on('data', (data) => {
        md5Hash.update(data);
        sha256Hash.update(data);
      });

      stream.on('end', () => {
        resolve({
          md5: md5Hash.digest('hex'),
          sha256: sha256Hash.digest('hex')
        });
      });

      stream.on('error', reject);
    });
  }

  detectMimeType(filename) {
    const extension = filename.toLowerCase().split('.').pop();
    const mimeTypes = {
      'jpg': 'image/jpeg',
      'jpeg': 'image/jpeg',
      'png': 'image/png',
      'gif': 'image/gif',
      'pdf': 'application/pdf',
      'mp4': 'video/mp4',
      'mp3': 'audio/mpeg',
      'txt': 'text/plain',
      'json': 'application/json',
      'zip': 'application/zip'
    };
    return mimeTypes[extension] || 'application/octet-stream';
  }

  async updateVersionHistory(originalFileId, newVersionId) {
    // Implementation for version history tracking
    await this.db.collection('file_versions').insertOne({
      originalFileId: originalFileId,
      versionId: newVersionId,
      createdAt: new Date()
    });
  }

  // File cleanup and maintenance
  async deleteFile(fileId) {
    try {
      await this.bucket.delete(fileId);
      console.log(`File deleted: ${fileId}`);
      return { success: true, deletedAt: new Date() };
    } catch (error) {
      console.error('File deletion error:', error);
      throw error;
    }
  }

  // Get comprehensive system metrics
  getSystemMetrics() {
    return {
      ...this.fileMetrics,
      timestamp: new Date(),
      bucketName: this.bucket.options.bucketName,
      chunkSize: this.bucket.options.chunkSizeBytes
    };
  }
}

// Example usage demonstrating comprehensive GridFS functionality
async function demonstrateGridFSOperations() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();

  const gridFSManager = new MongoGridFSManager(client, 'mediaStorage', 'uploads');

  try {
    console.log('Demonstrating MongoDB GridFS advanced file management...');

    // Upload a large file with comprehensive metadata
    console.log('Uploading large file...');
    const uploadResult = await gridFSManager.uploadFile('/path/to/large-video.mp4', {
      description: 'Corporate training video',
      category: 'training',
      tags: ['corporate', 'training', 'hr'],
      uploadedBy: 'admin',
      isPublic: false,
      contentMetadata: {
        duration: 3600, // seconds
        resolution: '1920x1080',
        codec: 'h264'
      },
      onProgress: (progress) => {
        console.log(`Upload progress: ${progress.percentage.toFixed(1)}%`);
      }
    });

    console.log('Upload completed:', uploadResult);

    // Search for files with comprehensive criteria
    console.log('Searching for video files...');
    const searchResults = await gridFSManager.searchFiles({
      mimeType: 'video/mp4',
      category: 'training',
      tags: ['corporate'],
      sizeRange: { min: 100 * 1024 * 1024 } // Files larger than 100MB
    }, {
      sortBy: 'size',
      limit: 10
    });

    console.log(`Found ${searchResults.totalCount} matching files`);
    searchResults.files.forEach(file => {
      console.log(`- ${file.filename} (${file.fileSizeMB.toFixed(1)} MB)`);
    });

    // Stream file with range support
    if (searchResults.files.length > 0) {
      const fileToStream = searchResults.files[0];
      console.log(`Streaming file: ${fileToStream.filename}`);

      const streamOptions = {
        start: 0,
        end: 1024 * 1024, // First 1MB
        onProgress: (progress) => {
          console.log(`Streaming progress: ${progress.percentage.toFixed(1)}%`);
        }
      };

      const stream = await gridFSManager.streamFile(fileToStream._id, streamOptions);

      // In a real application, you would pipe this to a response or file
      stream.on('end', () => {
        console.log('Streaming completed');
      });
    }

    // Generate comprehensive analytics
    console.log('Generating file analytics...');
    const analytics = await gridFSManager.getFileAnalytics('30d');

    console.log('Storage Analytics:');
    console.log(`Total Files: ${analytics.storage.totalFiles}`);
    console.log(`Total Storage: ${(analytics.storage.totalStorageBytes / (1024 * 1024 * 1024)).toFixed(2)} GB`);
    console.log(`Average File Size: ${(analytics.storage.averageFileSize / (1024 * 1024)).toFixed(2)} MB`);

    console.log('File Type Distribution:');
    analytics.fileTypes.slice(0, 5).forEach(type => {
      console.log(`- ${type._id}: ${type.fileCount} files (${(type.totalSize / (1024 * 1024)).toFixed(1)} MB)`);
    });

    // Get system metrics
    const metrics = gridFSManager.getSystemMetrics();
    console.log('System Performance Metrics:', metrics);

  } catch (error) {
    console.error('GridFS demonstration error:', error);
  } finally {
    await client.close();
  }
}

// Benefits of MongoDB GridFS:
// - Seamless large file storage without size limitations
// - Automatic file chunking with optimal performance
// - Comprehensive metadata management with flexible schemas
// - Built-in streaming support with range request capabilities
// - Integration with MongoDB's query engine and indexing
// - ACID transaction support for file operations
// - Advanced search and analytics capabilities
// - Automatic replication and distributed storage
// - File versioning and content management features
// - High-performance concurrent access and streaming

SQL-Style File Operations with QueryLeaf

QueryLeaf provides familiar approaches to MongoDB GridFS file management and operations:

-- QueryLeaf GridFS file management with SQL-familiar syntax

-- Upload file with comprehensive metadata
INSERT INTO FILES (filename, file_data, metadata) VALUES (
  'corporate-training.mp4',
  UPLOAD_FILE('/path/to/video.mp4'),
  JSON_OBJECT(
    'description', 'Corporate training video on data security',
    'category', 'training',
    'tags', JSON_ARRAY('corporate', 'security', 'training'),
    'uploadedBy', '[email protected]',
    'isPublic', false,
    'contentMetadata', JSON_OBJECT(
      'duration', 3600,
      'resolution', '1920x1080',
      'codec', 'h264',
      'bitrate', '2000kbps'
    ),
    'processingStatus', 'uploaded',
    'retentionPolicy', 'business-7years',
    'complianceFlags', JSON_ARRAY('gdpr', 'sox')
  )
);

-- Search and query files with comprehensive criteria
SELECT 
  file_id,
  filename,
  file_size,
  ROUND(file_size / 1024.0 / 1024.0, 2) as file_size_mb,
  upload_date,

  -- Extract metadata fields
  JSON_EXTRACT(metadata, '$.description') as description,
  JSON_EXTRACT(metadata, '$.category') as category,
  JSON_EXTRACT(metadata, '$.tags') as tags,
  JSON_EXTRACT(metadata, '$.uploadedBy') as uploaded_by,
  JSON_EXTRACT(metadata, '$.contentMetadata.duration') as duration_seconds,
  JSON_EXTRACT(metadata, '$.processingStatus') as processing_status,

  -- Access metrics
  JSON_EXTRACT(metadata, '$.accessCount') as access_count,
  JSON_EXTRACT(metadata, '$.lastAccessedAt') as last_accessed,
  JSON_EXTRACT(metadata, '$.totalBytesTransferred') as total_bytes_transferred,

  -- File integrity
  md5_hash,
  chunk_count,

  -- Computed fields
  CASE 
    WHEN file_size > 1024*1024*1024 THEN 'Large (>1GB)'
    WHEN file_size > 100*1024*1024 THEN 'Medium (>100MB)'
    ELSE 'Small (<100MB)'
  END as size_category,

  DATEDIFF(CURRENT_DATE(), upload_date) as days_since_upload

FROM GRIDFS_FILES()
WHERE 
  -- File type filtering
  JSON_EXTRACT(metadata, '$.mimeType') LIKE 'video/%'

  -- Category and tag filtering
  AND JSON_EXTRACT(metadata, '$.category') = 'training'
  AND JSON_CONTAINS(JSON_EXTRACT(metadata, '$.tags'), '"corporate"')

  -- Size filtering
  AND file_size > 100 * 1024 * 1024  -- Files larger than 100MB

  -- Date range filtering
  AND upload_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)

  -- Access pattern filtering
  AND CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) > 5

  -- Processing status filtering
  AND JSON_EXTRACT(metadata, '$.processingStatus') = 'ready'

ORDER BY 
  CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) DESC,
  upload_date DESC
LIMIT 50;

-- File analytics and usage patterns
WITH file_analytics AS (
  SELECT 
    DATE_FORMAT(upload_date, '%Y-%m') as upload_month,
    JSON_EXTRACT(metadata, '$.category') as category,
    JSON_EXTRACT(metadata, '$.mimeType') as mime_type,

    -- File metrics
    COUNT(*) as file_count,
    SUM(file_size) as total_size_bytes,
    AVG(file_size) as avg_file_size,
    MIN(file_size) as min_file_size,
    MAX(file_size) as max_file_size,

    -- Access metrics
    SUM(CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED)) as total_access_count,
    AVG(CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED)) as avg_access_count,
    SUM(CAST(JSON_EXTRACT(metadata, '$.totalBytesTransferred') AS UNSIGNED)) as total_bytes_transferred,

    -- Performance metrics
    AVG(CAST(JSON_EXTRACT(metadata, '$.uploadDuration') AS UNSIGNED)) as avg_upload_time_ms

  FROM GRIDFS_FILES()
  WHERE upload_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 12 MONTH)
  GROUP BY 
    DATE_FORMAT(upload_date, '%Y-%m'),
    JSON_EXTRACT(metadata, '$.category'),
    JSON_EXTRACT(metadata, '$.mimeType')
),

category_summary AS (
  SELECT 
    category,

    -- Volume metrics
    SUM(file_count) as total_files,
    SUM(total_size_bytes) as category_total_size,
    ROUND(SUM(total_size_bytes) / 1024.0 / 1024.0 / 1024.0, 2) as category_total_gb,

    -- Access patterns
    SUM(total_access_count) as category_total_accesses,
    ROUND(AVG(avg_access_count), 2) as category_avg_access_per_file,

    -- Performance indicators
    ROUND(AVG(avg_upload_time_ms), 2) as category_avg_upload_time,

    -- Growth trends
    COUNT(DISTINCT upload_month) as active_months,

    -- Storage efficiency
    ROUND(AVG(avg_file_size) / 1024.0 / 1024.0, 2) as avg_file_size_mb,
    ROUND(SUM(total_bytes_transferred) / 1024.0 / 1024.0 / 1024.0, 2) as total_transfer_gb

  FROM file_analytics
  GROUP BY category
)

SELECT 
  category,
  total_files,
  category_total_gb,
  category_avg_access_per_file,
  avg_file_size_mb,
  total_transfer_gb,

  -- Storage cost estimation (example rates)
  ROUND(category_total_gb * 0.023, 2) as estimated_monthly_storage_cost_usd,
  ROUND(total_transfer_gb * 0.09, 2) as estimated_transfer_cost_usd,

  -- Performance assessment
  CASE 
    WHEN category_avg_upload_time < 1000 THEN 'Excellent'
    WHEN category_avg_upload_time < 5000 THEN 'Good'
    WHEN category_avg_upload_time < 15000 THEN 'Fair'
    ELSE 'Needs Optimization'
  END as upload_performance,

  -- Usage classification
  CASE 
    WHEN category_avg_access_per_file > 100 THEN 'High Usage'
    WHEN category_avg_access_per_file > 20 THEN 'Medium Usage'
    WHEN category_avg_access_per_file > 5 THEN 'Low Usage'
    ELSE 'Archived/Inactive'
  END as usage_pattern

FROM category_summary
ORDER BY category_total_gb DESC, category_avg_access_per_file DESC;

-- File streaming and download operations
SELECT 
  file_id,
  filename,
  file_size,

  -- Create streaming URLs with range support
  CONCAT('/api/files/stream/', file_id) as stream_url,
  CONCAT('/api/files/stream/', file_id, '?range=0-1048576') as preview_stream_url,
  CONCAT('/api/files/download/', file_id) as download_url,

  -- Content delivery optimization
  CASE 
    WHEN JSON_EXTRACT(metadata, '$.mimeType') LIKE 'video/%' THEN 'streaming'
    WHEN JSON_EXTRACT(metadata, '$.mimeType') LIKE 'audio/%' THEN 'streaming'
    WHEN JSON_EXTRACT(metadata, '$.mimeType') LIKE 'image/%' THEN 'direct'
    ELSE 'download'
  END as recommended_delivery_method,

  -- CDN configuration suggestions
  CASE 
    WHEN CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) > 1000 THEN 'edge-cache'
    WHEN CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) > 100 THEN 'regional-cache'
    ELSE 'origin-only'
  END as cdn_strategy,

  -- Access control
  JSON_EXTRACT(metadata, '$.isPublic') as is_public,
  JSON_EXTRACT(metadata, '$.accessPermissions') as access_permissions

FROM GRIDFS_FILES()
WHERE JSON_EXTRACT(metadata, '$.processingStatus') = 'ready'
ORDER BY CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) DESC;

-- File maintenance and cleanup operations
WITH file_maintenance AS (
  SELECT 
    file_id,
    filename,
    file_size,
    upload_date,

    -- Metadata analysis
    JSON_EXTRACT(metadata, '$.category') as category,
    JSON_EXTRACT(metadata, '$.retentionPolicy') as retention_policy,
    JSON_EXTRACT(metadata, '$.lastAccessedAt') as last_accessed,
    CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) as access_count,

    -- Age calculations
    DATEDIFF(CURRENT_DATE(), upload_date) as days_since_upload,
    DATEDIFF(CURRENT_DATE(), STR_TO_DATE(JSON_EXTRACT(metadata, '$.lastAccessedAt'), '%Y-%m-%d')) as days_since_access,

    -- Maintenance flags
    CASE 
      WHEN JSON_EXTRACT(metadata, '$.retentionPolicy') = 'business-7years' AND 
           DATEDIFF(CURRENT_DATE(), upload_date) > 2555 THEN 'DELETE'
      WHEN JSON_EXTRACT(metadata, '$.retentionPolicy') = 'business-3years' AND 
           DATEDIFF(CURRENT_DATE(), upload_date) > 1095 THEN 'DELETE'
      WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE(JSON_EXTRACT(metadata, '$.lastAccessedAt'), '%Y-%m-%d')) > 365
           AND CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) = 0 THEN 'ARCHIVE'
      WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE(JSON_EXTRACT(metadata, '$.lastAccessedAt'), '%Y-%m-%d')) > 180
           AND CAST(JSON_EXTRACT(metadata, '$.accessCount') AS UNSIGNED) < 5 THEN 'COLD_STORAGE'
      ELSE 'ACTIVE'
    END as maintenance_action

  FROM GRIDFS_FILES()
)

SELECT 
  maintenance_action,
  COUNT(*) as file_count,
  ROUND(SUM(file_size) / 1024.0 / 1024.0 / 1024.0, 2) as total_size_gb,

  -- Cost impact analysis
  ROUND((SUM(file_size) / 1024.0 / 1024.0 / 1024.0) * 0.023, 2) as current_monthly_cost_usd,

  -- Storage class optimization
  CASE maintenance_action
    WHEN 'COLD_STORAGE' THEN ROUND((SUM(file_size) / 1024.0 / 1024.0 / 1024.0) * 0.004, 2)
    WHEN 'ARCHIVE' THEN ROUND((SUM(file_size) / 1024.0 / 1024.0 / 1024.0) * 0.001, 2)
    WHEN 'DELETE' THEN 0
    ELSE ROUND((SUM(file_size) / 1024.0 / 1024.0 / 1024.0) * 0.023, 2)
  END as optimized_monthly_cost_usd,

  -- Sample files for review
  GROUP_CONCAT(
    CONCAT(filename, ' (', ROUND(file_size/1024/1024, 1), 'MB)')
    ORDER BY file_size DESC
    SEPARATOR '; '
  ) as sample_files

FROM file_maintenance
GROUP BY maintenance_action
ORDER BY total_size_gb DESC;

-- Real-time file system monitoring
CREATE VIEW file_system_health AS
SELECT 
  -- Current system status
  COUNT(*) as total_files,
  ROUND(SUM(file_size) / 1024.0 / 1024.0 / 1024.0, 2) as total_storage_gb,
  COUNT(CASE WHEN upload_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR) THEN 1 END) as files_uploaded_24h,
  COUNT(CASE WHEN STR_TO_DATE(JSON_EXTRACT(metadata, '$.lastAccessedAt'), '%Y-%m-%d %H:%i:%s') >= DATE_SUB(NOW(), INTERVAL 1 HOUR) THEN 1 END) as files_accessed_1h,

  -- Performance indicators
  AVG(CAST(JSON_EXTRACT(metadata, '$.uploadDuration') AS UNSIGNED)) as avg_upload_time_ms,
  COUNT(CASE WHEN JSON_EXTRACT(metadata, '$.processingStatus') = 'error' THEN 1 END) as files_with_errors,
  COUNT(CASE WHEN chunk_count != CEIL(file_size / 1048576.0) THEN 1 END) as files_with_integrity_issues,

  -- Storage distribution
  COUNT(CASE WHEN file_size > 1024*1024*1024 THEN 1 END) as large_files_1gb_plus,
  COUNT(CASE WHEN file_size BETWEEN 100*1024*1024 AND 1024*1024*1024 THEN 1 END) as medium_files_100mb_1gb,
  COUNT(CASE WHEN file_size < 100*1024*1024 THEN 1 END) as small_files_under_100mb,

  -- Health assessment
  CASE 
    WHEN COUNT(CASE WHEN JSON_EXTRACT(metadata, '$.processingStatus') = 'error' THEN 1 END) > 
         COUNT(*) * 0.05 THEN 'Critical - High Error Rate'
    WHEN AVG(CAST(JSON_EXTRACT(metadata, '$.uploadDuration') AS UNSIGNED)) > 30000 THEN 'Warning - Slow Uploads'
    WHEN COUNT(CASE WHEN chunk_count != CEIL(file_size / 1048576.0) THEN 1 END) > 0 THEN 'Warning - Integrity Issues'
    ELSE 'Healthy'
  END as system_health_status,

  NOW() as report_timestamp

FROM GRIDFS_FILES()
WHERE upload_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY);

-- QueryLeaf GridFS provides:
-- 1. SQL-familiar file upload and management operations
-- 2. Comprehensive file search and filtering capabilities
-- 3. Advanced analytics and usage pattern analysis
-- 4. Intelligent file lifecycle management and cleanup
-- 5. Real-time system health monitoring and alerting
-- 6. Cost optimization and storage class recommendations
-- 7. Integration with MongoDB's GridFS streaming capabilities
-- 8. Metadata-driven content management and organization
-- 9. Performance monitoring and optimization insights
-- 10. Enterprise-grade file operations with ACID guarantees

Best Practices for MongoDB GridFS

File Storage Strategy

Optimal GridFS configuration for different application types:

  1. Media Streaming Applications: Large chunk sizes for optimal streaming performance
  2. Document Management Systems: Metadata-rich storage with comprehensive indexing
  3. Content Distribution Networks: Integration with CDN and caching strategies
  4. Backup and Archival Systems: Compression and long-term storage optimization
  5. Real-time Applications: Fast upload/download with minimal latency
  6. Multi-tenant Systems: Secure isolation and access control patterns

Performance Optimization Guidelines

Essential considerations for production GridFS deployments:

  1. Chunk Size Optimization: Balance between storage efficiency and streaming performance
  2. Index Strategy: Create appropriate indexes on metadata fields for fast queries
  3. Replication Configuration: Optimize replica set configuration for file operations
  4. Connection Pooling: Configure connection pools for concurrent file operations
  5. Monitoring Integration: Implement comprehensive file operation monitoring
  6. Storage Management: Plan for growth and implement lifecycle management

Conclusion

MongoDB GridFS provides sophisticated large file storage and management capabilities that seamlessly integrate with MongoDB's document database features while supporting unlimited file sizes, intelligent streaming, and comprehensive metadata management. By implementing advanced file management patterns, streaming optimization, and automated analytics, applications can handle complex file storage requirements while maintaining high performance and operational efficiency.

Key GridFS benefits include:

  • Unlimited File Storage: No size limitations with automatic chunking and distribution
  • Seamless Integration: Native integration with MongoDB queries, indexes, and transactions
  • Intelligent Streaming: High-performance streaming with range request support
  • Comprehensive Metadata: Flexible, searchable metadata with rich query capabilities
  • High Availability: Automatic replication and distributed storage across replica sets
  • Advanced Analytics: Built-in analytics and reporting for file usage and performance

Whether you're building media streaming platforms, document management systems, content distribution networks, or file-intensive applications, MongoDB GridFS with QueryLeaf's familiar file operation interface provides the foundation for scalable, efficient large file management. This combination enables you to leverage advanced file storage capabilities while maintaining familiar database administration patterns and SQL-style file operations.

QueryLeaf Integration: QueryLeaf automatically translates SQL-familiar file operations into optimal MongoDB GridFS commands while providing comprehensive file management and analytics through SQL-style queries. Advanced file storage patterns, streaming optimization, and lifecycle management are seamlessly handled through familiar database administration interfaces, making sophisticated file storage both powerful and accessible.

The integration of intelligent file storage with SQL-style file operations makes MongoDB an ideal platform for applications requiring both scalable file management and familiar database administration patterns, ensuring your files remain both accessible and efficiently managed as they scale to meet demanding production requirements.

MongoDB Aggregation Framework for Real-Time Analytics Dashboards: Advanced Data Processing and Visualization Pipelines

Modern data-driven applications require sophisticated analytics capabilities that can process large volumes of data in real-time, generate insights across multiple dimensions, and power interactive dashboards that provide immediate business intelligence. Traditional analytics approaches often involve complex ETL processes, separate analytics databases, and batch processing systems that introduce significant latency between data creation and insight availability, limiting the ability to make real-time business decisions.

MongoDB's Aggregation Framework provides comprehensive real-time analytics capabilities through powerful data processing pipelines that enable complex calculations, multi-stage transformations, and advanced statistical operations directly within the database. Unlike traditional analytics systems that require data movement and separate processing infrastructure, MongoDB aggregation pipelines can process operational data immediately, providing real-time insights with minimal latency and infrastructure complexity.

The Traditional Analytics Challenge

Conventional approaches to real-time analytics and dashboard creation have significant limitations for modern data-driven applications:

-- Traditional PostgreSQL analytics - complex and resource-intensive approaches

-- Basic analytics table structure with limited real-time capabilities
CREATE TABLE sales_transactions (
    transaction_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    customer_id UUID NOT NULL,
    product_id UUID NOT NULL,
    transaction_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    quantity INTEGER NOT NULL,
    unit_price DECIMAL(10,2) NOT NULL,
    total_amount DECIMAL(10,2) NOT NULL,
    discount_amount DECIMAL(10,2) DEFAULT 0,
    tax_amount DECIMAL(10,2) NOT NULL,
    payment_method VARCHAR(50) NOT NULL,
    sales_channel VARCHAR(50) NOT NULL,
    region VARCHAR(100) NOT NULL,

    -- Manual aggregation tracking (limited granularity)
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Basic customer demographics table
CREATE TABLE customers (
    customer_id UUID PRIMARY KEY,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    email VARCHAR(200) UNIQUE NOT NULL,
    age INTEGER,
    gender VARCHAR(20),
    city VARCHAR(100),
    state VARCHAR(50),
    country VARCHAR(50),
    customer_segment VARCHAR(50),
    registration_date TIMESTAMP NOT NULL,
    lifetime_value DECIMAL(15,2) DEFAULT 0
);

-- Product catalog with basic attributes
CREATE TABLE products (
    product_id UUID PRIMARY KEY,
    product_name VARCHAR(200) NOT NULL,
    category VARCHAR(100) NOT NULL,
    subcategory VARCHAR(100),
    brand VARCHAR(100),
    unit_cost DECIMAL(10,2) NOT NULL,
    list_price DECIMAL(10,2) NOT NULL,
    margin_percent DECIMAL(5,2),
    stock_quantity INTEGER DEFAULT 0,
    supplier_id UUID
);

-- Pre-aggregated summary tables (manual maintenance required)
CREATE TABLE daily_sales_summary (
    summary_date DATE NOT NULL,
    region VARCHAR(100) NOT NULL,
    category VARCHAR(100) NOT NULL,
    total_transactions INTEGER DEFAULT 0,
    total_revenue DECIMAL(15,2) DEFAULT 0,
    total_units_sold INTEGER DEFAULT 0,
    unique_customers INTEGER DEFAULT 0,
    avg_transaction_value DECIMAL(10,2) DEFAULT 0,

    -- Manual timestamp tracking
    calculated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (summary_date, region, category)
);

-- Complex materialized view for real-time dashboard (limited refresh capabilities)
CREATE MATERIALIZED VIEW current_sales_dashboard AS
WITH hourly_metrics AS (
    SELECT 
        DATE_TRUNC('hour', st.transaction_date) as hour_bucket,
        st.region,
        p.category,
        p.brand,
        c.customer_segment,

        -- Basic aggregations (limited computational capability)
        COUNT(*) as transaction_count,
        COUNT(DISTINCT st.customer_id) as unique_customers,
        SUM(st.total_amount) as total_revenue,
        SUM(st.quantity) as total_units,
        AVG(st.total_amount) as avg_transaction_value,
        SUM(st.discount_amount) as total_discounts,

        -- Limited statistical calculations
        STDDEV(st.total_amount) as revenue_stddev,
        PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY st.total_amount) as median_transaction_value,

        -- Payment method breakdown (basic pivot)
        COUNT(*) FILTER (WHERE st.payment_method = 'credit_card') as credit_card_transactions,
        COUNT(*) FILTER (WHERE st.payment_method = 'debit_card') as debit_card_transactions,
        COUNT(*) FILTER (WHERE st.payment_method = 'cash') as cash_transactions,
        COUNT(*) FILTER (WHERE st.payment_method = 'digital_wallet') as digital_wallet_transactions

    FROM sales_transactions st
    JOIN customers c ON st.customer_id = c.customer_id
    JOIN products p ON st.product_id = p.product_id
    WHERE st.transaction_date >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
    GROUP BY 
        DATE_TRUNC('hour', st.transaction_date),
        st.region, p.category, p.brand, c.customer_segment
),

regional_performance AS (
    SELECT 
        hm.region,

        -- Regional aggregations (limited granularity)
        SUM(hm.transaction_count) as total_transactions,
        SUM(hm.total_revenue) as total_revenue,
        SUM(hm.unique_customers) as unique_customers,
        AVG(hm.avg_transaction_value) as avg_transaction_value,

        -- Simple ranking (no advanced analytics)
        RANK() OVER (ORDER BY SUM(hm.total_revenue) DESC) as revenue_rank,

        -- Basic percentage calculations
        SUM(hm.total_revenue) / SUM(SUM(hm.total_revenue)) OVER () * 100 as revenue_percentage,

        -- Limited trend analysis
        SUM(hm.total_revenue) FILTER (WHERE hm.hour_bucket >= CURRENT_TIMESTAMP - INTERVAL '12 hours') as revenue_last_12h,
        SUM(hm.total_revenue) FILTER (WHERE hm.hour_bucket < CURRENT_TIMESTAMP - INTERVAL '12 hours') as revenue_prev_12h

    FROM hourly_metrics hm
    GROUP BY hm.region
),

category_analysis AS (
    SELECT 
        hm.category,
        hm.brand,

        -- Category-level aggregations
        SUM(hm.transaction_count) as category_transactions,
        SUM(hm.total_revenue) as category_revenue,
        SUM(hm.total_units) as category_units,

        -- Limited cross-category analysis
        SUM(hm.total_revenue) / SUM(SUM(hm.total_revenue)) OVER () * 100 as category_revenue_share,
        DENSE_RANK() OVER (ORDER BY SUM(hm.total_revenue) DESC) as category_rank,

        -- Basic growth calculations (limited time series analysis)
        SUM(hm.total_revenue) FILTER (WHERE hm.hour_bucket >= CURRENT_TIMESTAMP - INTERVAL '6 hours') as recent_revenue,
        SUM(hm.total_revenue) FILTER (WHERE hm.hour_bucket < CURRENT_TIMESTAMP - INTERVAL '6 hours') as earlier_revenue

    FROM hourly_metrics hm
    GROUP BY hm.category, hm.brand
)

SELECT 
    CURRENT_TIMESTAMP as dashboard_last_updated,

    -- Overall metrics (basic calculations only)
    (SELECT SUM(total_transactions) FROM regional_performance) as total_transactions_24h,
    (SELECT SUM(total_revenue) FROM regional_performance) as total_revenue_24h,
    (SELECT SUM(unique_customers) FROM regional_performance) as unique_customers_24h,
    (SELECT AVG(avg_transaction_value) FROM regional_performance) as avg_transaction_value_24h,

    -- Regional performance (limited analysis depth)
    (SELECT JSON_AGG(
        JSON_BUILD_OBJECT(
            'region', region,
            'revenue', total_revenue,
            'transactions', total_transactions,
            'rank', revenue_rank,
            'percentage', ROUND(revenue_percentage, 2),
            'trend', CASE 
                WHEN revenue_last_12h > revenue_prev_12h THEN 'up'
                WHEN revenue_last_12h < revenue_prev_12h THEN 'down' 
                ELSE 'flat'
            END
        ) ORDER BY revenue_rank
    ) FROM regional_performance) as regional_data,

    -- Category analysis (basic breakdown only)
    (SELECT JSON_AGG(
        JSON_BUILD_OBJECT(
            'category', category,
            'brand', brand,
            'revenue', category_revenue,
            'units', category_units,
            'share', ROUND(category_revenue_share, 2),
            'rank', category_rank,
            'growth', CASE 
                WHEN recent_revenue > earlier_revenue THEN 'positive'
                WHEN recent_revenue < earlier_revenue THEN 'negative'
                ELSE 'neutral'
            END
        ) ORDER BY category_rank
    ) FROM category_analysis) as category_data,

    -- Payment method distribution (static breakdown)
    (SELECT JSON_BUILD_OBJECT(
        'credit_card', SUM(credit_card_transactions),
        'debit_card', SUM(debit_card_transactions), 
        'cash', SUM(cash_transactions),
        'digital_wallet', SUM(digital_wallet_transactions)
    ) FROM hourly_metrics) as payment_methods,

    -- Customer segment analysis (limited segmentation)
    (SELECT JSON_AGG(
        JSON_BUILD_OBJECT(
            'segment', customer_segment,
            'transactions', SUM(transaction_count),
            'revenue', SUM(total_revenue),
            'avg_value', AVG(avg_transaction_value)
        )
    ) FROM hourly_metrics GROUP BY customer_segment) as customer_segments;

-- Problems with traditional analytics approaches:
-- 1. Materialized views require manual refresh and don't support real-time updates
-- 2. Limited aggregation and statistical calculation capabilities
-- 3. Complex join operations impact performance with large datasets
-- 4. No support for advanced analytics like time series analysis or forecasting
-- 5. Difficult to handle nested data structures or dynamic schema requirements
-- 6. Pre-aggregation tables require significant maintenance and storage overhead
-- 7. Limited flexibility for ad-hoc analytics queries and dashboard customization
-- 8. No built-in support for complex data transformations or calculated metrics
-- 9. Poor scalability for high-volume real-time analytics workloads
-- 10. Complex query optimization and index management requirements

-- Manual refresh process (resource-intensive and not real-time)
REFRESH MATERIALIZED VIEW CONCURRENTLY current_sales_dashboard;

-- Attempt at real-time hourly summary calculation (performance bottleneck)
WITH real_time_hourly AS (
    SELECT 
        DATE_TRUNC('hour', CURRENT_TIMESTAMP) as current_hour,

        -- Current hour calculations (heavy resource usage)
        COUNT(*) as current_hour_transactions,
        SUM(total_amount) as current_hour_revenue,
        COUNT(DISTINCT customer_id) as current_hour_customers,
        AVG(total_amount) as current_hour_avg_value,

        -- Limited real-time comparisons
        COUNT(*) FILTER (WHERE transaction_date >= DATE_TRUNC('hour', CURRENT_TIMESTAMP)) as this_hour_so_far,
        COUNT(*) FILTER (WHERE transaction_date >= DATE_TRUNC('hour', CURRENT_TIMESTAMP - INTERVAL '1 hour')
                        AND transaction_date < DATE_TRUNC('hour', CURRENT_TIMESTAMP)) as previous_hour_full,

        -- Basic percentage calculations
        SUM(total_amount) FILTER (WHERE transaction_date >= DATE_TRUNC('hour', CURRENT_TIMESTAMP)) as revenue_this_hour,
        SUM(total_amount) FILTER (WHERE transaction_date >= DATE_TRUNC('hour', CURRENT_TIMESTAMP - INTERVAL '1 hour')
                                  AND transaction_date < DATE_TRUNC('hour', CURRENT_TIMESTAMP)) as revenue_previous_hour

    FROM sales_transactions
    WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '2 hours'
),

performance_indicators AS (
    SELECT 
        rth.*,

        -- Limited performance metrics
        CASE 
            WHEN revenue_previous_hour > 0 THEN
                ROUND(((revenue_this_hour - revenue_previous_hour) / revenue_previous_hour) * 100, 2)
            ELSE NULL
        END as revenue_change_percent,

        CASE 
            WHEN previous_hour_full > 0 THEN
                ROUND(((this_hour_so_far - previous_hour_full) / previous_hour_full::FLOAT) * 100, 2)
            ELSE NULL
        END as transaction_change_percent,

        -- Simple trend classification
        CASE 
            WHEN revenue_this_hour > revenue_previous_hour THEN 'increasing'
            WHEN revenue_this_hour < revenue_previous_hour THEN 'decreasing'
            ELSE 'stable'
        END as revenue_trend

    FROM real_time_hourly rth
)

SELECT 
    current_hour,
    current_hour_transactions,
    ROUND(current_hour_revenue::NUMERIC, 2) as current_hour_revenue,
    current_hour_customers,
    ROUND(current_hour_avg_value::NUMERIC, 2) as current_hour_avg_value,

    -- Trend indicators
    revenue_change_percent,
    transaction_change_percent,
    revenue_trend,

    -- Performance assessment (basic classification)
    CASE 
        WHEN revenue_change_percent > 20 THEN 'excellent'
        WHEN revenue_change_percent > 10 THEN 'good'
        WHEN revenue_change_percent > 0 THEN 'positive'
        WHEN revenue_change_percent > -10 THEN 'neutral'
        ELSE 'concerning'
    END as performance_status,

    CURRENT_TIMESTAMP as calculated_at

FROM performance_indicators;

-- Traditional limitations:
-- 1. No real-time dashboard updates - requires manual refresh or polling
-- 2. Limited analytical capabilities compared to specialized analytics databases
-- 3. Performance degrades significantly with large datasets and complex calculations
-- 4. Difficult to implement advanced analytics like cohort analysis or forecasting
-- 5. No support for nested document analysis or flexible schema structures
-- 6. Complex index management and query optimization requirements
-- 7. Limited ability to handle streaming data or event-driven analytics
-- 8. Poor integration with modern visualization tools and BI platforms
-- 9. Significant infrastructure and maintenance overhead for analytics workloads
-- 10. Inflexible aggregation patterns that don't adapt to changing business requirements

MongoDB provides sophisticated real-time analytics capabilities through its powerful Aggregation Framework:

// MongoDB Advanced Real-Time Analytics Dashboard System
const { MongoClient } = require('mongodb');

const client = new MongoClient('mongodb://localhost:27017/?replicaSet=rs0');
const db = client.db('realtime_analytics_system');

// Comprehensive MongoDB Analytics Dashboard Manager
class RealtimeAnalyticsDashboard {
  constructor(db, config = {}) {
    this.db = db;
    this.collections = {
      salesTransactions: db.collection('sales_transactions'),
      customers: db.collection('customers'),
      products: db.collection('products'),
      analyticsCache: db.collection('analytics_cache'),
      dashboardMetrics: db.collection('dashboard_metrics'),
      userSessions: db.collection('user_sessions')
    };

    // Advanced analytics configuration
    this.config = {
      // Real-time processing settings
      enableRealTimeUpdates: config.enableRealTimeUpdates !== false,
      updateInterval: config.updateInterval || 30000, // 30 seconds
      cacheExpiration: config.cacheExpiration || 300000, // 5 minutes

      // Performance optimization
      enableAggregationOptimization: config.enableAggregationOptimization !== false,
      useIndexes: config.useIndexes !== false,
      enableParallelProcessing: config.enableParallelProcessing !== false,
      maxConcurrentPipelines: config.maxConcurrentPipelines || 5,

      // Analytics features
      enableAdvancedMetrics: config.enableAdvancedMetrics !== false,
      enablePredictiveAnalytics: config.enablePredictiveAnalytics || false,
      enableCohortAnalysis: config.enableCohortAnalysis || false,
      enableAnomalyDetection: config.enableAnomalyDetection || false,

      // Dashboard customization
      timeWindows: config.timeWindows || ['1h', '6h', '24h', '7d', '30d'],
      metrics: config.metrics || ['revenue', 'transactions', 'customers', 'conversion'],
      dimensions: config.dimensions || ['region', 'category', 'channel', 'segment'],

      // Data retention
      rawDataRetention: config.rawDataRetention || 90, // days
      aggregatedDataRetention: config.aggregatedDataRetention || 365 // days
    };

    // Analytics state management
    this.dashboardState = {
      lastUpdate: null,
      activeConnections: 0,
      processingStats: {
        totalQueries: 0,
        avgResponseTime: 0,
        cacheHitRate: 0
      }
    };

    // Initialize analytics system
    this.initializeAnalyticsSystem();
  }

  async initializeAnalyticsSystem() {
    console.log('Initializing comprehensive MongoDB real-time analytics system...');

    try {
      // Setup analytics indexes for optimal performance
      await this.setupAnalyticsIndexes();

      // Initialize real-time data processing
      await this.setupRealTimeProcessing();

      // Setup analytics caching layer
      await this.setupAnalyticsCache();

      // Initialize dashboard metrics collection
      await this.initializeDashboardMetrics();

      // Setup performance monitoring
      await this.setupPerformanceMonitoring();

      console.log('Real-time analytics system initialized successfully');

    } catch (error) {
      console.error('Error initializing analytics system:', error);
      throw error;
    }
  }

  async setupAnalyticsIndexes() {
    console.log('Setting up analytics-optimized indexes...');

    try {
      // Sales transactions indexes for time-series analytics
      await this.collections.salesTransactions.createIndexes([
        { key: { transaction_date: 1, region: 1 }, background: true },
        { key: { transaction_date: 1, product_category: 1 }, background: true },
        { key: { customer_id: 1, transaction_date: 1 }, background: true },
        { key: { region: 1, sales_channel: 1, transaction_date: 1 }, background: true },
        { key: { product_id: 1, transaction_date: 1 }, background: true },
        { key: { payment_method: 1, transaction_date: 1 }, background: true }
      ]);

      // Customer analytics indexes
      await this.collections.customers.createIndexes([
        { key: { customer_segment: 1, registration_date: 1 }, background: true },
        { key: { region: 1, customer_segment: 1 }, background: true },
        { key: { lifetime_value: 1 }, background: true }
      ]);

      // Product catalog indexes
      await this.collections.products.createIndexes([
        { key: { category: 1, subcategory: 1 }, background: true },
        { key: { brand: 1, category: 1 }, background: true },
        { key: { margin_percent: 1 }, background: true }
      ]);

      console.log('Analytics indexes created successfully');

    } catch (error) {
      console.error('Error setting up analytics indexes:', error);
      throw error;
    }
  }

  async generateRealtimeSalesDashboard(timeWindow = '24h', filters = {}) {
    console.log(`Generating real-time sales dashboard for ${timeWindow} window...`);

    try {
      // Calculate time range based on window
      const timeRange = this.calculateTimeRange(timeWindow);

      // Build comprehensive aggregation pipeline for dashboard metrics
      const dashboardPipeline = [
        // Stage 1: Time-based filtering with optional additional filters
        {
          $match: {
            transaction_date: {
              $gte: timeRange.startDate,
              $lte: timeRange.endDate
            },
            ...this.buildDynamicFilters(filters)
          }
        },

        // Stage 2: Join with customer data for segmentation
        {
          $lookup: {
            from: 'customers',
            localField: 'customer_id',
            foreignField: '_id',
            as: 'customer_info'
          }
        },

        // Stage 3: Join with product data for category analysis
        {
          $lookup: {
            from: 'products',
            localField: 'product_id',
            foreignField: '_id',
            as: 'product_info'
          }
        },

        // Stage 4: Flatten joined data and add computed fields
        {
          $addFields: {
            customer: { $arrayElemAt: ['$customer_info', 0] },
            product: { $arrayElemAt: ['$product_info', 0] },
            transaction_hour: { $dateToString: { format: '%Y-%m-%d %H:00:00', date: '$transaction_date' } },
            transaction_day: { $dateToString: { format: '%Y-%m-%d', date: '$transaction_date' } },
            profit_margin: {
              $multiply: [
                { $subtract: ['$unit_price', '$product.unit_cost'] },
                '$quantity'
              ]
            },
            is_weekend: {
              $in: [{ $dayOfWeek: '$transaction_date' }, [1, 7]]
            },
            time_of_day: {
              $switch: {
                branches: [
                  { case: { $lt: [{ $hour: '$transaction_date' }, 6] }, then: 'night' },
                  { case: { $lt: [{ $hour: '$transaction_date' }, 12] }, then: 'morning' },
                  { case: { $lt: [{ $hour: '$transaction_date' }, 18] }, then: 'afternoon' },
                  { case: { $lt: [{ $hour: '$transaction_date' }, 22] }, then: 'evening' }
                ],
                default: 'night'
              }
            }
          }
        },

        // Stage 5: Advanced multi-dimensional aggregations
        {
          $facet: {
            // Overall metrics for the time period
            overallMetrics: [
              {
                $group: {
                  _id: null,
                  totalRevenue: { $sum: '$total_amount' },
                  totalTransactions: { $sum: 1 },
                  totalUnits: { $sum: '$quantity' },
                  uniqueCustomers: { $addToSet: '$customer_id' },
                  totalProfit: { $sum: '$profit_margin' },
                  avgTransactionValue: { $avg: '$total_amount' },
                  avgOrderSize: { $avg: '$quantity' },
                  totalDiscounts: { $sum: '$discount_amount' },
                  totalTax: { $sum: '$tax_amount' },

                  // Advanced statistical metrics
                  revenueStdDev: { $stdDevSamp: '$total_amount' },
                  transactionValuePercentiles: {
                    $push: '$total_amount'
                  }
                }
              },
              {
                $addFields: {
                  uniqueCustomerCount: { $size: '$uniqueCustomers' },
                  avgRevenuePerCustomer: {
                    $divide: ['$totalRevenue', { $size: '$uniqueCustomers' }]
                  },
                  profitMargin: {
                    $multiply: [
                      { $divide: ['$totalProfit', '$totalRevenue'] },
                      100
                    ]
                  },
                  discountRate: {
                    $multiply: [
                      { $divide: ['$totalDiscounts', '$totalRevenue'] },
                      100
                    ]
                  }
                }
              }
            ],

            // Time-based trend analysis (hourly breakdown)
            hourlyTrends: [
              {
                $group: {
                  _id: '$transaction_hour',
                  revenue: { $sum: '$total_amount' },
                  transactions: { $sum: 1 },
                  uniqueCustomers: { $addToSet: '$customer_id' },
                  avgTransactionValue: { $avg: '$total_amount' },
                  profit: { $sum: '$profit_margin' }
                }
              },
              {
                $addFields: {
                  uniqueCustomerCount: { $size: '$uniqueCustomers' },
                  hour: '$_id'
                }
              },
              {
                $sort: { _id: 1 }
              }
            ],

            // Regional performance analysis
            regionalPerformance: [
              {
                $group: {
                  _id: '$region',
                  revenue: { $sum: '$total_amount' },
                  transactions: { $sum: 1 },
                  uniqueCustomers: { $addToSet: '$customer_id' },
                  profit: { $sum: '$profit_margin' },
                  avgTransactionValue: { $avg: '$total_amount' },
                  topPaymentMethods: {
                    $push: '$payment_method'
                  }
                }
              },
              {
                $addFields: {
                  uniqueCustomerCount: { $size: '$uniqueCustomers' },
                  region: '$_id',
                  profitMargin: {
                    $multiply: [{ $divide: ['$profit', '$revenue'] }, 100]
                  }
                }
              },
              {
                $sort: { revenue: -1 }
              }
            ],

            // Product category analysis with advanced metrics
            categoryAnalysis: [
              {
                $group: {
                  _id: {
                    category: '$product.category',
                    subcategory: '$product.subcategory'
                  },
                  revenue: { $sum: '$total_amount' },
                  transactions: { $sum: 1 },
                  totalUnits: { $sum: '$quantity' },
                  profit: { $sum: '$profit_margin' },
                  avgUnitPrice: { $avg: '$unit_price' },
                  uniqueProducts: { $addToSet: '$product_id' },
                  brands: { $addToSet: '$product.brand' }
                }
              },
              {
                $addFields: {
                  category: '$_id.category',
                  subcategory: '$_id.subcategory',
                  uniqueProductCount: { $size: '$uniqueProducts' },
                  uniqueBrandCount: { $size: '$brands' },
                  profitMargin: {
                    $multiply: [{ $divide: ['$profit', '$revenue'] }, 100]
                  },
                  revenuePerProduct: {
                    $divide: ['$revenue', { $size: '$uniqueProducts' }]
                  }
                }
              },
              {
                $sort: { revenue: -1 }
              }
            ],

            // Customer segment performance
            customerSegmentAnalysis: [
              {
                $group: {
                  _id: '$customer.customer_segment',
                  revenue: { $sum: '$total_amount' },
                  transactions: { $sum: 1 },
                  uniqueCustomers: { $addToSet: '$customer_id' },
                  profit: { $sum: '$profit_margin' },
                  avgTransactionValue: { $avg: '$total_amount' },
                  avgAge: { $avg: '$customer.age' },
                  genderDistribution: { $push: '$customer.gender' }
                }
              },
              {
                $addFields: {
                  segment: '$_id',
                  uniqueCustomerCount: { $size: '$uniqueCustomers' },
                  revenuePerCustomer: {
                    $divide: ['$revenue', { $size: '$uniqueCustomers' }]
                  },
                  transactionsPerCustomer: {
                    $divide: ['$transactions', { $size: '$uniqueCustomers' }]
                  }
                }
              },
              {
                $sort: { revenuePerCustomer: -1 }
              }
            ],

            // Payment method and channel analysis
            paymentChannelAnalysis: [
              {
                $group: {
                  _id: {
                    paymentMethod: '$payment_method',
                    salesChannel: '$sales_channel'
                  },
                  revenue: { $sum: '$total_amount' },
                  transactions: { $sum: 1 },
                  avgTransactionValue: { $avg: '$total_amount' },
                  profit: { $sum: '$profit_margin' }
                }
              },
              {
                $addFields: {
                  paymentMethod: '$_id.paymentMethod',
                  salesChannel: '$_id.salesChannel'
                }
              },
              {
                $sort: { revenue: -1 }
              }
            ],

            // Time-of-day and weekend analysis
            temporalAnalysis: [
              {
                $group: {
                  _id: {
                    timeOfDay: '$time_of_day',
                    isWeekend: '$is_weekend'
                  },
                  revenue: { $sum: '$total_amount' },
                  transactions: { $sum: 1 },
                  avgTransactionValue: { $avg: '$total_amount' },
                  uniqueCustomers: { $addToSet: '$customer_id' }
                }
              },
              {
                $addFields: {
                  timeOfDay: '$_id.timeOfDay',
                  isWeekend: '$_id.isWeekend',
                  uniqueCustomerCount: { $size: '$uniqueCustomers' }
                }
              },
              {
                $sort: { revenue: -1 }
              }
            ]
          }
        }
      ];

      // Execute the aggregation pipeline
      const dashboardResults = await this.collections.salesTransactions
        .aggregate(dashboardPipeline, {
          allowDiskUse: true,
          hint: { transaction_date: 1, region: 1 }
        })
        .toArray();

      // Process and enrich the results
      const enrichedResults = await this.enrichDashboardResults(dashboardResults[0], timeWindow);

      // Cache the results for performance
      await this.cacheDashboardResults(enrichedResults, timeWindow, filters);

      // Update dashboard metrics
      await this.updateDashboardMetrics(enrichedResults);

      return enrichedResults;

    } catch (error) {
      console.error('Error generating real-time sales dashboard:', error);
      throw error;
    }
  }

  async enrichDashboardResults(results, timeWindow) {
    console.log('Enriching dashboard results with advanced analytics...');

    try {
      const overallMetrics = results.overallMetrics[0] || {};

      // Calculate percentiles for transaction values
      if (overallMetrics.transactionValuePercentiles) {
        const sortedValues = overallMetrics.transactionValuePercentiles.sort((a, b) => a - b);
        const length = sortedValues.length;

        overallMetrics.percentiles = {
          p25: this.calculatePercentile(sortedValues, 25),
          p50: this.calculatePercentile(sortedValues, 50),
          p75: this.calculatePercentile(sortedValues, 75),
          p90: this.calculatePercentile(sortedValues, 90),
          p95: this.calculatePercentile(sortedValues, 95)
        };

        delete overallMetrics.transactionValuePercentiles; // Remove raw data
      }

      // Add growth calculations (comparing with previous period)
      const previousPeriodMetrics = await this.getPreviousPeriodMetrics(timeWindow);
      if (previousPeriodMetrics) {
        overallMetrics.growth = {
          revenueGrowth: this.calculateGrowthRate(overallMetrics.totalRevenue, previousPeriodMetrics.totalRevenue),
          transactionGrowth: this.calculateGrowthRate(overallMetrics.totalTransactions, previousPeriodMetrics.totalTransactions),
          customerGrowth: this.calculateGrowthRate(overallMetrics.uniqueCustomerCount, previousPeriodMetrics.uniqueCustomerCount),
          avgValueGrowth: this.calculateGrowthRate(overallMetrics.avgTransactionValue, previousPeriodMetrics.avgTransactionValue)
        };
      }

      // Add revenue distribution analysis
      if (results.regionalPerformance) {
        const totalRevenue = overallMetrics.totalRevenue || 0;
        results.regionalPerformance = results.regionalPerformance.map(region => ({
          ...region,
          revenueShare: (region.revenue / totalRevenue * 100).toFixed(2),
          customerDensity: (region.uniqueCustomerCount / region.transactions * 100).toFixed(2)
        }));
      }

      // Add category performance rankings
      if (results.categoryAnalysis) {
        results.categoryAnalysis = results.categoryAnalysis.map((category, index) => ({
          ...category,
          rank: index + 1,
          performanceScore: this.calculateCategoryPerformanceScore(category)
        }));
      }

      // Add temporal insights
      if (results.temporalAnalysis) {
        results.temporalAnalysis = results.temporalAnalysis.map(period => ({
          ...period,
          efficiency: (period.revenue / period.transactions).toFixed(2),
          customerEngagement: (period.uniqueCustomerCount / period.transactions * 100).toFixed(2)
        }));
      }

      // Add dashboard metadata
      const enrichedResults = {
        ...results,
        metadata: {
          timeWindow: timeWindow,
          generatedAt: new Date(),
          dataFreshness: this.calculateDataFreshness(),
          performanceMetrics: {
            queryExecutionTime: Date.now() - this.queryStartTime,
            dataPoints: overallMetrics.totalTransactions,
            cacheStatus: 'fresh'
          }
        },
        overallMetrics: overallMetrics
      };

      return enrichedResults;

    } catch (error) {
      console.error('Error enriching dashboard results:', error);
      throw error;
    }
  }

  calculatePercentile(sortedArray, percentile) {
    const index = (percentile / 100) * (sortedArray.length - 1);
    const lower = Math.floor(index);
    const upper = Math.ceil(index);
    const weight = index % 1;

    return (sortedArray[lower] * (1 - weight) + sortedArray[upper] * weight).toFixed(2);
  }

  calculateGrowthRate(current, previous) {
    if (!previous || previous === 0) return null;
    return (((current - previous) / previous) * 100).toFixed(2);
  }

  calculateCategoryPerformanceScore(category) {
    // Weighted scoring based on revenue, profit margin, and transaction volume
    const revenueScore = Math.min(category.revenue / 10000, 100); // Scale revenue
    const profitScore = Math.max(0, Math.min(category.profitMargin || 0, 100));
    const volumeScore = Math.min(category.transactions / 100, 100);

    return ((revenueScore * 0.5) + (profitScore * 0.3) + (volumeScore * 0.2)).toFixed(2);
  }

  buildDynamicFilters(filters) {
    const mongoFilters = {};

    if (filters.regions && filters.regions.length > 0) {
      mongoFilters.region = { $in: filters.regions };
    }

    if (filters.categories && filters.categories.length > 0) {
      mongoFilters.product_category = { $in: filters.categories };
    }

    if (filters.paymentMethods && filters.paymentMethods.length > 0) {
      mongoFilters.payment_method = { $in: filters.paymentMethods };
    }

    if (filters.minAmount || filters.maxAmount) {
      mongoFilters.total_amount = {};
      if (filters.minAmount) mongoFilters.total_amount.$gte = filters.minAmount;
      if (filters.maxAmount) mongoFilters.total_amount.$lte = filters.maxAmount;
    }

    return mongoFilters;
  }

  calculateTimeRange(timeWindow) {
    const endDate = new Date();
    let startDate = new Date();

    switch (timeWindow) {
      case '1h':
        startDate.setHours(endDate.getHours() - 1);
        break;
      case '6h':
        startDate.setHours(endDate.getHours() - 6);
        break;
      case '24h':
        startDate.setDate(endDate.getDate() - 1);
        break;
      case '7d':
        startDate.setDate(endDate.getDate() - 7);
        break;
      case '30d':
        startDate.setDate(endDate.getDate() - 30);
        break;
      default:
        startDate.setDate(endDate.getDate() - 1);
    }

    return { startDate, endDate };
  }

  async generateCustomerLifetimeValueAnalysis() {
    console.log('Generating advanced customer lifetime value analysis...');

    try {
      const clvAnalysisPipeline = [
        // Stage 1: Join transactions with customer data
        {
          $lookup: {
            from: 'customers',
            localField: 'customer_id',
            foreignField: '_id',
            as: 'customer'
          }
        },

        // Stage 2: Flatten customer data
        {
          $addFields: {
            customer: { $arrayElemAt: ['$customer', 0] }
          }
        },

        // Stage 3: Calculate customer metrics
        {
          $group: {
            _id: '$customer_id',
            customerInfo: { $first: '$customer' },
            firstPurchase: { $min: '$transaction_date' },
            lastPurchase: { $max: '$transaction_date' },
            totalRevenue: { $sum: '$total_amount' },
            totalProfit: { 
              $sum: { 
                $multiply: [
                  { $subtract: ['$unit_price', { $ifNull: ['$unit_cost', 0] }] },
                  '$quantity'
                ]
              }
            },
            totalTransactions: { $sum: 1 },
            totalUnits: { $sum: '$quantity' },
            avgOrderValue: { $avg: '$total_amount' },
            purchaseFrequency: { $sum: 1 },
            categories: { $addToSet: '$product_category' },
            paymentMethods: { $push: '$payment_method' },
            channels: { $addToSet: '$sales_channel' }
          }
        },

        // Stage 4: Calculate advanced CLV metrics
        {
          $addFields: {
            customerLifespanDays: {
              $divide: [
                { $subtract: ['$lastPurchase', '$firstPurchase'] },
                1000 * 60 * 60 * 24
              ]
            },
            avgDaysBetweenPurchases: {
              $cond: {
                if: { $gt: ['$totalTransactions', 1] },
                then: {
                  $divide: [
                    { $divide: [
                      { $subtract: ['$lastPurchase', '$firstPurchase'] },
                      1000 * 60 * 60 * 24
                    ]},
                    { $subtract: ['$totalTransactions', 1] }
                  ]
                },
                else: null
              }
            },
            categoryDiversity: { $size: '$categories' },
            channelDiversity: { $size: '$channels' },
            profitMargin: {
              $multiply: [
                { $divide: ['$totalProfit', '$totalRevenue'] },
                100
              ]
            }
          }
        },

        // Stage 5: Calculate predicted CLV (simplified model)
        {
          $addFields: {
            predictedMonthlyValue: {
              $cond: {
                if: { $and: [
                  { $gt: ['$avgDaysBetweenPurchases', 0] },
                  { $lte: ['$avgDaysBetweenPurchases', 365] }
                ]},
                then: {
                  $multiply: [
                    '$avgOrderValue',
                    { $divide: [30, '$avgDaysBetweenPurchases'] }
                  ]
                },
                else: 0
              }
            },
            predictedAnnualValue: {
              $cond: {
                if: { $and: [
                  { $gt: ['$avgDaysBetweenPurchases', 0] },
                  { $lte: ['$avgDaysBetweenPurchases', 365] }
                ]},
                then: {
                  $multiply: [
                    '$avgOrderValue',
                    { $divide: [365, '$avgDaysBetweenPurchases'] }
                  ]
                },
                else: '$totalRevenue'
              }
            }
          }
        },

        // Stage 6: Customer segmentation
        {
          $addFields: {
            valueSegment: {
              $switch: {
                branches: [
                  { case: { $gte: ['$totalRevenue', 5000] }, then: 'high_value' },
                  { case: { $gte: ['$totalRevenue', 1000] }, then: 'medium_value' },
                  { case: { $gte: ['$totalRevenue', 100] }, then: 'low_value' }
                ],
                default: 'minimal_value'
              }
            },
            frequencySegment: {
              $switch: {
                branches: [
                  { case: { $gte: ['$totalTransactions', 20] }, then: 'very_frequent' },
                  { case: { $gte: ['$totalTransactions', 10] }, then: 'frequent' },
                  { case: { $gte: ['$totalTransactions', 5] }, then: 'occasional' }
                ],
                default: 'rare'
              }
            },
            recencySegment: {
              $switch: {
                branches: [
                  { 
                    case: { 
                      $gte: [
                        '$lastPurchase',
                        { $subtract: [new Date(), 30 * 24 * 60 * 60 * 1000] }
                      ]
                    },
                    then: 'recent'
                  },
                  {
                    case: {
                      $gte: [
                        '$lastPurchase',
                        { $subtract: [new Date(), 90 * 24 * 60 * 60 * 1000] }
                      ]
                    },
                    then: 'moderate'
                  }
                ],
                default: 'dormant'
              }
            }
          }
        },

        // Stage 7: Final CLV calculation and risk assessment
        {
          $addFields: {
            rfmScore: {
              $add: [
                {
                  $switch: {
                    branches: [
                      { case: { $eq: ['$recencySegment', 'recent'] }, then: 4 },
                      { case: { $eq: ['$recencySegment', 'moderate'] }, then: 2 }
                    ],
                    default: 1
                  }
                },
                {
                  $switch: {
                    branches: [
                      { case: { $eq: ['$frequencySegment', 'very_frequent'] }, then: 4 },
                      { case: { $eq: ['$frequencySegment', 'frequent'] }, then: 3 },
                      { case: { $eq: ['$frequencySegment', 'occasional'] }, then: 2 }
                    ],
                    default: 1
                  }
                },
                {
                  $switch: {
                    branches: [
                      { case: { $eq: ['$valueSegment', 'high_value'] }, then: 4 },
                      { case: { $eq: ['$valueSegment', 'medium_value'] }, then: 3 },
                      { case: { $eq: ['$valueSegment', 'low_value'] }, then: 2 }
                    ],
                    default: 1
                  }
                }
              ]
            },
            churnRisk: {
              $switch: {
                branches: [
                  {
                    case: {
                      $and: [
                        { $eq: ['$recencySegment', 'dormant'] },
                        { $lt: ['$avgDaysBetweenPurchases', 60] }
                      ]
                    },
                    then: 'high'
                  },
                  {
                    case: {
                      $and: [
                        { $eq: ['$recencySegment', 'moderate'] },
                        { $gt: ['$avgDaysBetweenPurchases', 30] }
                      ]
                    },
                    then: 'medium'
                  }
                ],
                default: 'low'
              }
            }
          }
        },

        // Stage 8: Sort by predicted annual value
        {
          $sort: { predictedAnnualValue: -1, totalRevenue: -1 }
        }
      ];

      const clvResults = await this.collections.salesTransactions
        .aggregate(clvAnalysisPipeline, { allowDiskUse: true })
        .toArray();

      return {
        customerAnalysis: clvResults,
        summary: await this.generateCLVSummary(clvResults),
        generatedAt: new Date()
      };

    } catch (error) {
      console.error('Error generating CLV analysis:', error);
      throw error;
    }
  }

  async generateCLVSummary(clvResults) {
    const totalCustomers = clvResults.length;
    const totalValue = clvResults.reduce((sum, customer) => sum + customer.totalRevenue, 0);
    const totalPredictedValue = clvResults.reduce((sum, customer) => sum + (customer.predictedAnnualValue || 0), 0);

    return {
      totalCustomers,
      totalHistoricalValue: totalValue,
      totalPredictedAnnualValue: totalPredictedValue,
      averageCustomerValue: totalValue / totalCustomers,
      averagePredictedValue: totalPredictedValue / totalCustomers,
      segmentBreakdown: {
        highValue: clvResults.filter(c => c.valueSegment === 'high_value').length,
        mediumValue: clvResults.filter(c => c.valueSegment === 'medium_value').length,
        lowValue: clvResults.filter(c => c.valueSegment === 'low_value').length,
        minimalValue: clvResults.filter(c => c.valueSegment === 'minimal_value').length
      },
      churnRiskDistribution: {
        high: clvResults.filter(c => c.churnRisk === 'high').length,
        medium: clvResults.filter(c => c.churnRisk === 'medium').length,
        low: clvResults.filter(c => c.churnRisk === 'low').length
      },
      topPerformers: clvResults.slice(0, 10).map(customer => ({
        customerId: customer._id,
        totalRevenue: customer.totalRevenue,
        predictedAnnualValue: customer.predictedAnnualValue,
        rfmScore: customer.rfmScore,
        segment: customer.valueSegment
      }))
    };
  }

  async cacheDashboardResults(results, timeWindow, filters) {
    try {
      const cacheKey = `dashboard_${timeWindow}_${JSON.stringify(filters)}`;

      await this.collections.analyticsCache.replaceOne(
        { cacheKey },
        {
          cacheKey,
          results,
          createdAt: new Date(),
          expiresAt: new Date(Date.now() + this.config.cacheExpiration)
        },
        { upsert: true }
      );
    } catch (error) {
      console.warn('Error caching dashboard results:', error.message);
    }
  }

  async getPreviousPeriodMetrics(timeWindow) {
    try {
      // Calculate previous period time range
      const previousTimeRange = this.calculatePreviousPeriodRange(timeWindow);

      const previousMetrics = await this.collections.salesTransactions.aggregate([
        {
          $match: {
            transaction_date: {
              $gte: previousTimeRange.startDate,
              $lte: previousTimeRange.endDate
            }
          }
        },
        {
          $group: {
            _id: null,
            totalRevenue: { $sum: '$total_amount' },
            totalTransactions: { $sum: 1 },
            uniqueCustomers: { $addToSet: '$customer_id' },
            avgTransactionValue: { $avg: '$total_amount' }
          }
        },
        {
          $addFields: {
            uniqueCustomerCount: { $size: '$uniqueCustomers' }
          }
        }
      ]).toArray();

      return previousMetrics[0] || null;

    } catch (error) {
      console.warn('Error getting previous period metrics:', error.message);
      return null;
    }
  }

  calculatePreviousPeriodRange(timeWindow) {
    const currentEndDate = new Date();
    let currentStartDate = new Date();

    // Calculate current period duration
    switch (timeWindow) {
      case '1h':
        currentStartDate.setHours(currentEndDate.getHours() - 1);
        break;
      case '6h':
        currentStartDate.setHours(currentEndDate.getHours() - 6);
        break;
      case '24h':
        currentStartDate.setDate(currentEndDate.getDate() - 1);
        break;
      case '7d':
        currentStartDate.setDate(currentEndDate.getDate() - 7);
        break;
      case '30d':
        currentStartDate.setDate(currentEndDate.getDate() - 30);
        break;
      default:
        currentStartDate.setDate(currentEndDate.getDate() - 1);
    }

    // Calculate previous period (same duration, preceding the current period)
    const periodDuration = currentEndDate.getTime() - currentStartDate.getTime();
    const previousEndDate = new Date(currentStartDate.getTime());
    const previousStartDate = new Date(currentStartDate.getTime() - periodDuration);

    return { startDate: previousStartDate, endDate: previousEndDate };
  }

  calculateDataFreshness() {
    // Calculate how fresh the data is based on the latest transaction
    const now = new Date();
    // This would typically query for the latest transaction timestamp
    // For demo purposes, assuming data is fresh within the last 5 minutes
    return 'fresh'; // 'fresh', 'stale', 'outdated'
  }

  async updateDashboardMetrics(results) {
    try {
      await this.collections.dashboardMetrics.insertOne({
        timestamp: new Date(),
        metrics: {
          totalRevenue: results.overallMetrics?.totalRevenue || 0,
          totalTransactions: results.overallMetrics?.totalTransactions || 0,
          uniqueCustomers: results.overallMetrics?.uniqueCustomerCount || 0,
          avgTransactionValue: results.overallMetrics?.avgTransactionValue || 0
        },
        performance: results.metadata?.performanceMetrics || {}
      });

      // Cleanup old metrics (keep last 1000 entries)
      const totalCount = await this.collections.dashboardMetrics.countDocuments();
      if (totalCount > 1000) {
        const oldestRecords = await this.collections.dashboardMetrics
          .find()
          .sort({ timestamp: 1 })
          .limit(totalCount - 1000)
          .toArray();

        const idsToDelete = oldestRecords.map(record => record._id);
        await this.collections.dashboardMetrics.deleteMany({
          _id: { $in: idsToDelete }
        });
      }
    } catch (error) {
      console.warn('Error updating dashboard metrics:', error.message);
    }
  }

  async setupRealTimeProcessing() {
    if (!this.config.enableRealTimeUpdates) return;

    console.log('Setting up real-time dashboard processing...');

    // Setup interval for dashboard updates
    setInterval(async () => {
      try {
        // Generate fresh dashboard data
        const dashboardData = await this.generateRealtimeSalesDashboard('1h');

        // Emit real-time updates (in a real implementation, this would push to connected clients)
        this.emit('dashboardUpdate', {
          timestamp: new Date(),
          data: dashboardData
        });

        this.dashboardState.lastUpdate = new Date();

      } catch (error) {
        console.error('Error in real-time processing:', error);
      }
    }, this.config.updateInterval);
  }

  async setupAnalyticsCache() {
    console.log('Setting up analytics caching layer...');

    try {
      // Create TTL index for cache expiration
      await this.collections.analyticsCache.createIndex(
        { expiresAt: 1 },
        { expireAfterSeconds: 0, background: true }
      );

      console.log('Analytics cache configured successfully');

    } catch (error) {
      console.error('Error setting up analytics cache:', error);
      throw error;
    }
  }

  async setupPerformanceMonitoring() {
    console.log('Setting up performance monitoring...');

    setInterval(async () => {
      try {
        // Monitor query performance
        const queryStats = await this.db.stats();

        // Update processing statistics
        this.dashboardState.processingStats.totalQueries++;

        // Log performance metrics
        console.log('Dashboard performance metrics:', {
          activeConnections: this.dashboardState.activeConnections,
          avgResponseTime: this.dashboardState.processingStats.avgResponseTime,
          cacheHitRate: this.dashboardState.processingStats.cacheHitRate,
          dbStats: {
            collections: queryStats.collections,
            dataSize: queryStats.dataSize,
            indexSize: queryStats.indexSize
          }
        });

      } catch (error) {
        console.warn('Error in performance monitoring:', error);
      }
    }, 60000); // Every minute
  }
}

// Benefits of MongoDB Advanced Real-Time Analytics:
// - Real-time dashboard updates with minimal latency through change streams integration
// - Complex multi-dimensional aggregations with advanced statistical calculations
// - Flexible data transformation and enrichment during query execution
// - Sophisticated customer segmentation and lifetime value analysis
// - Built-in performance optimization with intelligent caching strategies
// - Scalable architecture supporting high-volume analytics workloads
// - Native MongoDB aggregation framework providing SQL-compatible analytics
// - Advanced temporal analysis with time-series data processing capabilities
// - Comprehensive business intelligence with predictive analytics support
// - SQL-familiar analytics operations through QueryLeaf integration

module.exports = {
  RealtimeAnalyticsDashboard
};

Understanding MongoDB Analytics Architecture

Advanced Aggregation Pipeline Design and Performance Optimization

Implement sophisticated analytics patterns for production MongoDB deployments:

// Production-ready MongoDB analytics with enterprise-grade optimization
class EnterpriseAnalyticsPlatform extends RealtimeAnalyticsDashboard {
  constructor(db, enterpriseConfig) {
    super(db, enterpriseConfig);

    this.enterpriseConfig = {
      ...enterpriseConfig,
      enableDistributedAnalytics: true,
      enableMachineLearning: true,
      enablePredictiveModeling: true,
      enableDataGovernance: true,
      enableComplianceReporting: true,
      enableAdvancedVisualization: true
    };

    this.setupEnterpriseFeatures();
    this.initializeMLPipelines();
    this.setupDataGovernance();
  }

  async implementAdvancedTimeSeriesAnalytics() {
    console.log('Implementing advanced time series analytics with forecasting...');

    const timeSeriesConfig = {
      // Time series aggregation strategies
      temporalAggregation: {
        enableSeasonalityDetection: true,
        enableTrendAnalysis: true,
        enableAnomalyDetection: true,
        forecastHorizon: 30 // days
      },

      // Statistical modeling
      statisticalModeling: {
        enableMovingAverages: true,
        enableExponentialSmoothing: true,
        enableRegressionAnalysis: true,
        confidenceIntervals: true
      },

      // Performance optimization
      performanceOptimization: {
        enableTimeSeriesCollections: true,
        optimizedIndexes: true,
        compressionStrategies: true,
        partitioningSchemes: true
      }
    };

    return await this.deployTimeSeriesAnalytics(timeSeriesConfig);
  }

  async setupAdvancedMLPipelines() {
    console.log('Setting up machine learning pipelines for predictive analytics...');

    const mlPipelineConfig = {
      // Customer behavior prediction
      customerBehaviorML: {
        churnPredictionModel: true,
        clvPredictionModel: true,
        recommendationEngine: true,
        segmentationOptimization: true
      },

      // Sales forecasting
      salesForecastingML: {
        demandForecasting: true,
        inventoryOptimization: true,
        priceOptimization: true,
        seasonalityModeling: true
      },

      // Real-time decision making
      realTimeDecisionEngine: {
        dynamicPricing: true,
        inventoryAlerts: true,
        customerTargeting: true,
        fraudDetection: true
      }
    };

    return await this.implementMLPipelines(mlPipelineConfig);
  }
}

SQL-Style Analytics with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB analytics and dashboard operations:

-- QueryLeaf advanced real-time analytics with SQL-familiar syntax for MongoDB

-- Configure comprehensive analytics dashboard with real-time updates
CONFIGURE ANALYTICS_DASHBOARD
SET real_time_updates = true,
    update_interval_seconds = 30,
    cache_expiration_minutes = 5,
    enable_predictive_analytics = true,
    enable_advanced_metrics = true,
    enable_cohort_analysis = true,
    time_windows = ['1h', '6h', '24h', '7d', '30d'],
    dimensions = ['region', 'category', 'channel', 'segment'];

-- Advanced real-time sales dashboard with comprehensive metrics and analytics
WITH sales_analytics AS (
  -- Primary transaction data with enriched customer and product information
  SELECT 
    st.transaction_id,
    st.transaction_date,
    st.customer_id,
    st.product_id,
    st.total_amount,
    st.quantity,
    st.unit_price,
    st.discount_amount,
    st.tax_amount,
    st.payment_method,
    st.sales_channel,
    st.region,

    -- Customer enrichment
    c.customer_segment,
    c.age,
    c.gender,
    c.city,
    c.state,
    c.registration_date,
    c.lifetime_value,

    -- Product enrichment
    p.category,
    p.subcategory,
    p.brand,
    p.unit_cost,
    p.list_price,
    p.margin_percent,

    -- Calculated fields for analytics
    (st.unit_price - p.unit_cost) * st.quantity as profit_margin,
    st.total_amount - st.discount_amount - st.tax_amount as net_revenue,

    -- Time-based dimensions
    DATE_TRUNC('hour', st.transaction_date) as transaction_hour,
    DATE_TRUNC('day', st.transaction_date) as transaction_day,
    EXTRACT(hour FROM st.transaction_date) as hour_of_day,
    EXTRACT(dow FROM st.transaction_date) as day_of_week,
    EXTRACT(dow FROM st.transaction_date) IN (0, 6) as is_weekend,

    -- Time categorization
    CASE 
      WHEN EXTRACT(hour FROM st.transaction_date) BETWEEN 6 AND 11 THEN 'morning'
      WHEN EXTRACT(hour FROM st.transaction_date) BETWEEN 12 AND 17 THEN 'afternoon'  
      WHEN EXTRACT(hour FROM st.transaction_date) BETWEEN 18 AND 21 THEN 'evening'
      ELSE 'night'
    END as time_of_day_category,

    -- Customer lifecycle stage
    CASE 
      WHEN st.transaction_date - c.registration_date <= INTERVAL '30 days' THEN 'new_customer'
      WHEN st.transaction_date - c.registration_date <= INTERVAL '365 days' THEN 'established_customer'
      ELSE 'loyal_customer'
    END as customer_lifecycle_stage

  FROM sales_transactions st
  JOIN customers c ON st.customer_id = c.customer_id
  JOIN products p ON st.product_id = p.product_id
  WHERE st.transaction_date >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
),

-- Overall metrics with advanced statistical calculations
overall_metrics AS (
  SELECT 
    -- Basic volume metrics
    COUNT(*) as total_transactions,
    COUNT(DISTINCT customer_id) as unique_customers,
    SUM(total_amount) as total_revenue,
    SUM(net_revenue) as total_net_revenue,
    SUM(quantity) as total_units_sold,
    SUM(profit_margin) as total_profit,
    SUM(discount_amount) as total_discounts,
    SUM(tax_amount) as total_tax,

    -- Advanced statistical metrics
    AVG(total_amount) as avg_transaction_value,
    STDDEV(total_amount) as transaction_value_stddev,
    PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY total_amount) as q1_transaction_value,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY total_amount) as median_transaction_value,
    PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY total_amount) as q3_transaction_value,
    PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY total_amount) as p90_transaction_value,
    PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY total_amount) as p95_transaction_value,

    -- Derived metrics
    AVG(quantity) as avg_order_size,
    AVG(profit_margin) as avg_profit_per_transaction,
    AVG(total_amount / NULLIF(quantity, 0)) as avg_price_per_unit,

    -- Efficiency metrics
    SUM(total_revenue) / COUNT(DISTINCT customer_id) as revenue_per_customer,
    COUNT(*) / COUNT(DISTINCT customer_id) as transactions_per_customer,
    SUM(profit_margin) / SUM(total_revenue) * 100 as overall_profit_margin_percent,
    SUM(discount_amount) / SUM(total_revenue) * 100 as overall_discount_rate_percent,

    -- Time-based metrics
    MIN(transaction_date) as earliest_transaction,
    MAX(transaction_date) as latest_transaction,
    COUNT(DISTINCT transaction_hour) as active_hours,
    COUNT(DISTINCT transaction_day) as active_days

  FROM sales_analytics
),

-- Temporal trend analysis with pattern detection
temporal_trends AS (
  SELECT 
    transaction_hour,

    -- Hourly volume metrics
    COUNT(*) as hourly_transactions,
    COUNT(DISTINCT customer_id) as hourly_unique_customers,
    SUM(total_amount) as hourly_revenue,
    SUM(profit_margin) as hourly_profit,
    AVG(total_amount) as hourly_avg_transaction_value,
    SUM(quantity) as hourly_units_sold,

    -- Hour-over-hour growth calculations
    LAG(SUM(total_amount)) OVER (ORDER BY transaction_hour) as prev_hour_revenue,
    LAG(COUNT(*)) OVER (ORDER BY transaction_hour) as prev_hour_transactions,

    -- Moving averages for trend smoothing
    AVG(SUM(total_amount)) OVER (
      ORDER BY transaction_hour 
      ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
    ) as revenue_3h_moving_avg,

    AVG(COUNT(*)) OVER (
      ORDER BY transaction_hour 
      ROWS BETWEEN 2 PRECEDING AND CURRENT ROW  
    ) as transactions_3h_moving_avg,

    -- Peak detection
    RANK() OVER (ORDER BY SUM(total_amount) DESC) as revenue_rank,
    RANK() OVER (ORDER BY COUNT(*) DESC) as transaction_rank,

    -- Performance classification
    CASE 
      WHEN SUM(total_amount) > AVG(SUM(total_amount)) OVER () * 1.5 THEN 'peak'
      WHEN SUM(total_amount) > AVG(SUM(total_amount)) OVER () THEN 'above_average'
      WHEN SUM(total_amount) > AVG(SUM(total_amount)) OVER () * 0.5 THEN 'below_average'
      ELSE 'low'
    END as performance_tier

  FROM sales_analytics
  GROUP BY transaction_hour
  ORDER BY transaction_hour
),

-- Regional performance analysis with competitive ranking
regional_performance AS (
  SELECT 
    region,

    -- Regional volume metrics
    COUNT(*) as region_transactions,
    COUNT(DISTINCT customer_id) as region_unique_customers,
    COUNT(DISTINCT product_id) as region_unique_products,
    SUM(total_amount) as region_revenue,
    SUM(profit_margin) as region_profit,
    SUM(quantity) as region_units_sold,

    -- Regional efficiency metrics
    AVG(total_amount) as region_avg_transaction_value,
    SUM(total_amount) / COUNT(DISTINCT customer_id) as region_revenue_per_customer,
    COUNT(*) / COUNT(DISTINCT customer_id) as region_frequency_per_customer,
    SUM(profit_margin) / SUM(total_amount) * 100 as region_profit_margin_percent,

    -- Market share calculations
    SUM(total_amount) / SUM(SUM(total_amount)) OVER () * 100 as region_revenue_share,
    COUNT(*) / SUM(COUNT(*)) OVER () * 100 as region_transaction_share,
    COUNT(DISTINCT customer_id) / SUM(COUNT(DISTINCT customer_id)) OVER () * 100 as region_customer_share,

    -- Regional ranking
    RANK() OVER (ORDER BY SUM(total_amount) DESC) as revenue_rank,
    RANK() OVER (ORDER BY SUM(profit_margin) DESC) as profit_rank,
    RANK() OVER (ORDER BY COUNT(DISTINCT customer_id) DESC) as customer_base_rank,

    -- Customer density and engagement
    COUNT(DISTINCT customer_id) / COUNT(*) * 100 as customer_density_percent,
    AVG(
      CASE WHEN customer_lifecycle_stage = 'new_customer' THEN 1 ELSE 0 END
    ) * 100 as new_customer_percent,

    -- Channel and payment preferences
    MODE() WITHIN GROUP (ORDER BY sales_channel) as dominant_sales_channel,
    MODE() WITHIN GROUP (ORDER BY payment_method) as dominant_payment_method,

    -- Performance indicators
    CASE 
      WHEN SUM(total_amount) / SUM(SUM(total_amount)) OVER () > 0.2 THEN 'market_leader'
      WHEN SUM(total_amount) / SUM(SUM(total_amount)) OVER () > 0.1 THEN 'major_market'
      WHEN SUM(total_amount) / SUM(SUM(total_amount)) OVER () > 0.05 THEN 'secondary_market'
      ELSE 'emerging_market'
    END as market_position

  FROM sales_analytics
  GROUP BY region
),

-- Advanced product category analysis with profitability insights
category_analysis AS (
  SELECT 
    category,
    subcategory,
    brand,

    -- Category performance metrics
    COUNT(*) as category_transactions,
    COUNT(DISTINCT customer_id) as category_customers,
    COUNT(DISTINCT product_id) as category_products,
    SUM(total_amount) as category_revenue,
    SUM(profit_margin) as category_profit,
    SUM(quantity) as category_units,

    -- Category efficiency and profitability
    AVG(total_amount) as category_avg_transaction_value,
    AVG(profit_margin) as category_avg_profit_per_transaction,
    SUM(profit_margin) / SUM(total_amount) * 100 as category_profit_margin_percent,
    AVG(unit_price) as category_avg_unit_price,
    AVG(margin_percent) as category_avg_product_margin,

    -- Market positioning
    SUM(total_amount) / SUM(SUM(total_amount)) OVER () * 100 as category_revenue_share,
    COUNT(*) / SUM(COUNT(*)) OVER () * 100 as category_transaction_share,

    -- Category rankings
    RANK() OVER (ORDER BY SUM(total_amount) DESC) as revenue_rank,
    RANK() OVER (ORDER BY SUM(profit_margin) DESC) as profit_rank,
    RANK() OVER (ORDER BY COUNT(*) DESC) as volume_rank,
    RANK() OVER (ORDER BY SUM(profit_margin) / SUM(total_amount) DESC) as margin_rank,

    -- Customer engagement
    COUNT(DISTINCT customer_id) / COUNT(*) * 100 as customer_diversity_percent,
    SUM(total_amount) / COUNT(DISTINCT customer_id) as revenue_per_customer,
    COUNT(*) / COUNT(DISTINCT customer_id) as repeat_purchase_rate,

    -- Product performance distribution
    AVG(list_price) as category_avg_list_price,
    AVG(unit_cost) as category_avg_unit_cost,
    STDDEV(unit_price) as category_price_variance,

    -- Growth and trend indicators
    SUM(total_amount) FILTER (WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '6 hours') as recent_6h_revenue,
    SUM(total_amount) FILTER (WHERE transaction_date < CURRENT_TIMESTAMP - INTERVAL '6 hours') as earlier_18h_revenue,

    -- Performance classification
    CASE 
      WHEN SUM(profit_margin) / SUM(total_amount) > 0.3 THEN 'high_margin'
      WHEN SUM(profit_margin) / SUM(total_amount) > 0.15 THEN 'medium_margin'
      ELSE 'low_margin'
    END as profitability_tier,

    CASE 
      WHEN SUM(total_amount) / SUM(SUM(total_amount)) OVER () > 0.15 THEN 'star_category'
      WHEN SUM(total_amount) / SUM(SUM(total_amount)) OVER () > 0.05 THEN 'growth_category'
      ELSE 'niche_category'
    END as strategic_category

  FROM sales_analytics
  GROUP BY category, subcategory, brand
),

-- Customer segmentation analysis with behavioral insights
customer_segment_analysis AS (
  SELECT 
    customer_segment,
    customer_lifecycle_stage,

    -- Segment volume metrics
    COUNT(*) as segment_transactions,
    COUNT(DISTINCT customer_id) as segment_customers,
    SUM(total_amount) as segment_revenue,
    SUM(profit_margin) as segment_profit,
    SUM(quantity) as segment_units,

    -- Segment behavior analysis
    AVG(total_amount) as segment_avg_transaction_value,
    SUM(total_amount) / COUNT(DISTINCT customer_id) as segment_revenue_per_customer,
    COUNT(*) / COUNT(DISTINCT customer_id) as segment_transactions_per_customer,
    AVG(age) as segment_avg_age,

    -- Demographic breakdown
    AVG(CASE WHEN gender = 'male' THEN 1 ELSE 0 END) * 100 as male_percentage,
    AVG(CASE WHEN gender = 'female' THEN 1 ELSE 0 END) * 100 as female_percentage,
    COUNT(DISTINCT city) as cities_represented,
    COUNT(DISTINCT state) as states_represented,

    -- Channel preferences  
    AVG(CASE WHEN sales_channel = 'online' THEN 1 ELSE 0 END) * 100 as online_preference_percent,
    AVG(CASE WHEN sales_channel = 'retail' THEN 1 ELSE 0 END) * 100 as retail_preference_percent,
    AVG(CASE WHEN sales_channel = 'mobile' THEN 1 ELSE 0 END) * 100 as mobile_preference_percent,

    -- Payment behavior
    AVG(CASE WHEN payment_method = 'credit_card' THEN 1 ELSE 0 END) * 100 as credit_card_usage_percent,
    AVG(CASE WHEN payment_method = 'digital_wallet' THEN 1 ELSE 0 END) * 100 as digital_wallet_usage_percent,

    -- Temporal behavior
    AVG(CASE WHEN is_weekend THEN 1 ELSE 0 END) * 100 as weekend_shopping_percent,
    MODE() WITHIN GROUP (ORDER BY time_of_day_category) as preferred_shopping_time,

    -- Value and profitability
    SUM(profit_margin) / SUM(total_amount) * 100 as segment_profit_margin_percent,
    AVG(lifetime_value) as segment_avg_lifetime_value,

    -- Segment rankings
    RANK() OVER (ORDER BY SUM(total_amount) DESC) as revenue_rank,
    RANK() OVER (ORDER BY SUM(total_amount) / COUNT(DISTINCT customer_id) DESC) as value_per_customer_rank,
    RANK() OVER (ORDER BY COUNT(*) / COUNT(DISTINCT customer_id) DESC) as engagement_rank,

    -- Segment classification
    CASE 
      WHEN SUM(total_amount) / COUNT(DISTINCT customer_id) > 1000 THEN 'high_value_segment'
      WHEN SUM(total_amount) / COUNT(DISTINCT customer_id) > 500 THEN 'medium_value_segment'
      ELSE 'opportunity_segment'
    END as value_classification

  FROM sales_analytics
  GROUP BY customer_segment, customer_lifecycle_stage
),

-- Payment method and channel effectiveness analysis  
channel_payment_analysis AS (
  SELECT 
    sales_channel,
    payment_method,

    -- Channel-payment combination metrics
    COUNT(*) as combination_transactions,
    SUM(total_amount) as combination_revenue,
    AVG(total_amount) as combination_avg_value,
    SUM(profit_margin) as combination_profit,
    COUNT(DISTINCT customer_id) as combination_customers,

    -- Effectiveness metrics
    SUM(total_amount) / COUNT(DISTINCT customer_id) as revenue_per_customer,
    COUNT(*) / COUNT(DISTINCT customer_id) as transactions_per_customer,
    SUM(profit_margin) / SUM(total_amount) * 100 as combination_profit_margin,

    -- Market share within channel
    SUM(total_amount) / SUM(SUM(total_amount)) OVER (PARTITION BY sales_channel) * 100 as payment_share_in_channel,

    -- Market share within payment method
    SUM(total_amount) / SUM(SUM(total_amount)) OVER (PARTITION BY payment_method) * 100 as channel_share_in_payment,

    -- Overall market share
    SUM(total_amount) / SUM(SUM(total_amount)) OVER () * 100 as overall_market_share,

    -- Customer behavior insights
    AVG(age) as combination_avg_customer_age,
    AVG(CASE WHEN customer_segment = 'premium' THEN 1 ELSE 0 END) * 100 as premium_customer_percent,
    AVG(CASE WHEN is_weekend THEN 1 ELSE 0 END) * 100 as weekend_usage_percent,

    -- Performance ranking
    RANK() OVER (ORDER BY SUM(total_amount) DESC) as revenue_rank,
    RANK() OVER (ORDER BY COUNT(*) DESC) as volume_rank,
    RANK() OVER (ORDER BY SUM(profit_margin) / SUM(total_amount) DESC) as profitability_rank

  FROM sales_analytics  
  GROUP BY sales_channel, payment_method
),

-- Advanced growth and trend analysis
growth_trend_analysis AS (
  SELECT 
    -- Current period metrics (last 6 hours)
    SUM(total_amount) FILTER (WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '6 hours') as current_6h_revenue,
    COUNT(*) FILTER (WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '6 hours') as current_6h_transactions,
    COUNT(DISTINCT customer_id) FILTER (WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '6 hours') as current_6h_customers,

    -- Previous period metrics (6-12 hours ago)
    SUM(total_amount) FILTER (
      WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '12 hours' 
      AND transaction_date < CURRENT_TIMESTAMP - INTERVAL '6 hours'
    ) as previous_6h_revenue,
    COUNT(*) FILTER (
      WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '12 hours' 
      AND transaction_date < CURRENT_TIMESTAMP - INTERVAL '6 hours'
    ) as previous_6h_transactions,
    COUNT(DISTINCT customer_id) FILTER (
      WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '12 hours' 
      AND transaction_date < CURRENT_TIMESTAMP - INTERVAL '6 hours'
    ) as previous_6h_customers,

    -- Earlier period metrics (12-18 hours ago) for trend detection
    SUM(total_amount) FILTER (
      WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '18 hours' 
      AND transaction_date < CURRENT_TIMESTAMP - INTERVAL '12 hours'
    ) as earlier_6h_revenue,
    COUNT(*) FILTER (
      WHERE transaction_date >= CURRENT_TIMESTAMP - INTERVAL '18 hours' 
      AND transaction_date < CURRENT_TIMESTAMP - INTERVAL '12 hours'
    ) as earlier_6h_transactions,

    -- Peak analysis
    MAX(total_amount) as peak_transaction_value,
    MIN(total_amount) as min_transaction_value,
    MODE() WITHIN GROUP (ORDER BY EXTRACT(hour FROM transaction_date)) as peak_hour,
    MODE() WITHIN GROUP (ORDER BY region) as dominant_region,
    MODE() WITHIN GROUP (ORDER BY category) as dominant_category

  FROM sales_analytics
)

-- Final dashboard results with comprehensive analytics
SELECT 
  CURRENT_TIMESTAMP as dashboard_generated_at,

  -- Overall performance summary
  JSON_OBJECT(
    'total_transactions', om.total_transactions,
    'total_revenue', ROUND(om.total_revenue::NUMERIC, 2),
    'total_net_revenue', ROUND(om.total_net_revenue::NUMERIC, 2), 
    'total_profit', ROUND(om.total_profit::NUMERIC, 2),
    'unique_customers', om.unique_customers,
    'avg_transaction_value', ROUND(om.avg_transaction_value::NUMERIC, 2),
    'median_transaction_value', ROUND(om.median_transaction_value::NUMERIC, 2),
    'profit_margin_percent', ROUND((om.total_profit / om.total_revenue * 100)::NUMERIC, 2),
    'discount_rate_percent', ROUND((om.total_discounts / om.total_revenue * 100)::NUMERIC, 2),
    'revenue_per_customer', ROUND(om.revenue_per_customer::NUMERIC, 2),
    'transactions_per_customer', ROUND(om.transactions_per_customer::NUMERIC, 2)
  ) as overall_metrics,

  -- Temporal trends with growth indicators
  (SELECT JSON_AGG(
    JSON_OBJECT(
      'hour', transaction_hour,
      'revenue', ROUND(hourly_revenue::NUMERIC, 2),
      'transactions', hourly_transactions,
      'customers', hourly_unique_customers,
      'avg_value', ROUND(hourly_avg_transaction_value::NUMERIC, 2),
      'units_sold', hourly_units_sold,
      'growth_rate_revenue', 
        CASE 
          WHEN prev_hour_revenue > 0 THEN
            ROUND(((hourly_revenue - prev_hour_revenue) / prev_hour_revenue * 100)::NUMERIC, 2)
          ELSE NULL
        END,
      'growth_rate_transactions',
        CASE 
          WHEN prev_hour_transactions > 0 THEN  
            ROUND(((hourly_transactions - prev_hour_transactions) / prev_hour_transactions::FLOAT * 100)::NUMERIC, 2)
          ELSE NULL
        END,
      'revenue_3h_moving_avg', ROUND(revenue_3h_moving_avg::NUMERIC, 2),
      'performance_tier', performance_tier,
      'revenue_rank', revenue_rank
    ) ORDER BY transaction_hour
  ) FROM temporal_trends) as hourly_trends,

  -- Regional performance with competitive analysis  
  (SELECT JSON_AGG(
    JSON_OBJECT(
      'region', region,
      'revenue', ROUND(region_revenue::NUMERIC, 2),
      'revenue_share', ROUND(region_revenue_share::NUMERIC, 2),
      'transactions', region_transactions,
      'customers', region_unique_customers,
      'products', region_unique_products,
      'avg_transaction_value', ROUND(region_avg_transaction_value::NUMERIC, 2),
      'revenue_per_customer', ROUND(region_revenue_per_customer::NUMERIC, 2),
      'profit_margin_percent', ROUND(region_profit_margin_percent::NUMERIC, 2),
      'revenue_rank', revenue_rank,
      'profit_rank', profit_rank,
      'customer_base_rank', customer_base_rank,
      'market_position', market_position,
      'dominant_channel', dominant_sales_channel,
      'dominant_payment', dominant_payment_method,
      'customer_density_percent', ROUND(customer_density_percent::NUMERIC, 2),
      'new_customer_percent', ROUND(new_customer_percent::NUMERIC, 2)
    ) ORDER BY revenue_rank
  ) FROM regional_performance) as regional_analysis,

  -- Category analysis with profitability insights
  (SELECT JSON_AGG(
    JSON_OBJECT(
      'category', category,
      'subcategory', subcategory,
      'brand', brand,
      'revenue', ROUND(category_revenue::NUMERIC, 2),
      'revenue_share', ROUND(category_revenue_share::NUMERIC, 2),
      'transactions', category_transactions,
      'customers', category_customers,
      'products', category_products,
      'profit_margin_percent', ROUND(category_profit_margin_percent::NUMERIC, 2),
      'avg_transaction_value', ROUND(category_avg_transaction_value::NUMERIC, 2),
      'revenue_per_customer', ROUND(revenue_per_customer::NUMERIC, 2),
      'revenue_rank', revenue_rank,
      'profit_rank', profit_rank,
      'margin_rank', margin_rank,
      'profitability_tier', profitability_tier,
      'strategic_category', strategic_category,
      'growth_indicator',
        CASE 
          WHEN earlier_18h_revenue > 0 THEN
            CASE 
              WHEN recent_6h_revenue > earlier_18h_revenue THEN 'growing'
              WHEN recent_6h_revenue < earlier_18h_revenue THEN 'declining'
              ELSE 'stable'
            END
          ELSE 'insufficient_data'
        END
    ) ORDER BY revenue_rank
  ) FROM category_analysis) as category_performance,

  -- Customer segment insights
  (SELECT JSON_AGG(
    JSON_OBJECT(
      'segment', customer_segment,
      'lifecycle_stage', customer_lifecycle_stage,
      'revenue', ROUND(segment_revenue::NUMERIC, 2),
      'customers', segment_customers,
      'transactions', segment_transactions,
      'revenue_per_customer', ROUND(segment_revenue_per_customer::NUMERIC, 2),
      'transactions_per_customer', ROUND(segment_transactions_per_customer::NUMERIC, 2),
      'avg_age', ROUND(segment_avg_age::NUMERIC, 1),
      'avg_lifetime_value', ROUND(segment_avg_lifetime_value::NUMERIC, 2),
      'profit_margin_percent', ROUND(segment_profit_margin_percent::NUMERIC, 2),
      'male_percentage', ROUND(male_percentage::NUMERIC, 1),
      'female_percentage', ROUND(female_percentage::NUMERIC, 1),
      'online_preference_percent', ROUND(online_preference_percent::NUMERIC, 1),
      'weekend_shopping_percent', ROUND(weekend_shopping_percent::NUMERIC, 1),
      'preferred_shopping_time', preferred_shopping_time,
      'value_classification', value_classification,
      'revenue_rank', revenue_rank
    ) ORDER BY revenue_rank
  ) FROM customer_segment_analysis) as segment_analysis,

  -- Channel and payment method effectiveness
  (SELECT JSON_AGG(
    JSON_OBJECT(
      'channel', sales_channel,
      'payment_method', payment_method,
      'revenue', ROUND(combination_revenue::NUMERIC, 2),
      'transactions', combination_transactions,
      'customers', combination_customers,
      'avg_transaction_value', ROUND(combination_avg_value::NUMERIC, 2),
      'revenue_per_customer', ROUND(revenue_per_customer::NUMERIC, 2),
      'profit_margin_percent', ROUND(combination_profit_margin::NUMERIC, 2),
      'overall_market_share', ROUND(overall_market_share::NUMERIC, 2),
      'payment_share_in_channel', ROUND(payment_share_in_channel::NUMERIC, 2),
      'channel_share_in_payment', ROUND(channel_share_in_payment::NUMERIC, 2),
      'premium_customer_percent', ROUND(premium_customer_percent::NUMERIC, 1),
      'weekend_usage_percent', ROUND(weekend_usage_percent::NUMERIC, 1),
      'revenue_rank', revenue_rank,
      'profitability_rank', profitability_rank
    ) ORDER BY revenue_rank
  ) FROM channel_payment_analysis) as channel_payment_effectiveness,

  -- Growth trends and momentum indicators
  (SELECT JSON_OBJECT(
    'current_6h_revenue', ROUND(current_6h_revenue::NUMERIC, 2),
    'current_6h_transactions', current_6h_transactions,
    'current_6h_customers', current_6h_customers,
    'previous_6h_revenue', ROUND(previous_6h_revenue::NUMERIC, 2),
    'previous_6h_transactions', previous_6h_transactions,
    'previous_6h_customers', previous_6h_customers,
    'revenue_growth_rate',
      CASE 
        WHEN previous_6h_revenue > 0 THEN
          ROUND(((current_6h_revenue - previous_6h_revenue) / previous_6h_revenue * 100)::NUMERIC, 2)
        ELSE NULL
      END,
    'transaction_growth_rate',
      CASE 
        WHEN previous_6h_transactions > 0 THEN
          ROUND(((current_6h_transactions - previous_6h_transactions) / previous_6h_transactions::FLOAT * 100)::NUMERIC, 2)
        ELSE NULL
      END,
    'customer_growth_rate',
      CASE 
        WHEN previous_6h_customers > 0 THEN
          ROUND(((current_6h_customers - previous_6h_customers) / previous_6h_customers::FLOAT * 100)::NUMERIC, 2)
        ELSE NULL
      END,
    'momentum_indicator',
      CASE 
        WHEN previous_6h_revenue > 0 AND earlier_6h_revenue > 0 THEN
          CASE 
            WHEN current_6h_revenue > previous_6h_revenue AND previous_6h_revenue > earlier_6h_revenue THEN 'accelerating'
            WHEN current_6h_revenue > previous_6h_revenue AND previous_6h_revenue <= earlier_6h_revenue THEN 'recovering'
            WHEN current_6h_revenue <= previous_6h_revenue AND previous_6h_revenue > earlier_6h_revenue THEN 'slowing'
            WHEN current_6h_revenue <= previous_6h_revenue AND previous_6h_revenue <= earlier_6h_revenue THEN 'declining'
            ELSE 'stable'
          END
        ELSE 'insufficient_data'
      END,
    'peak_transaction_value', ROUND(peak_transaction_value::NUMERIC, 2),
    'min_transaction_value', ROUND(min_transaction_value::NUMERIC, 2),
    'peak_hour', peak_hour,
    'dominant_region', dominant_region,
    'dominant_category', dominant_category
  ) FROM growth_trend_analysis) as growth_trends,

  -- Dashboard metadata and performance indicators
  JSON_OBJECT(
    'data_freshness_minutes', 
      EXTRACT(MINUTES FROM CURRENT_TIMESTAMP - (SELECT MAX(transaction_date) FROM sales_analytics)),
    'analysis_time_window', '24 hours',
    'total_data_points', (SELECT total_transactions FROM overall_metrics),
    'analysis_depth', 'comprehensive',
    'last_updated', CURRENT_TIMESTAMP,
    'performance_indicators', JSON_OBJECT(
      'query_complexity', 'high',
      'data_completeness', 'complete',
      'analytical_accuracy', 'high',
      'real_time_capability', true
    )
  ) as dashboard_metadata

FROM overall_metrics om
CROSS JOIN growth_trend_analysis gta;

-- Advanced customer lifetime value analysis with SQL aggregations
WITH customer_transaction_history AS (
  SELECT 
    st.customer_id,
    c.customer_segment,
    c.registration_date,
    c.age,
    c.gender,
    c.city,
    c.state,

    -- Transaction aggregations
    MIN(st.transaction_date) as first_purchase_date,
    MAX(st.transaction_date) as last_purchase_date,
    COUNT(*) as total_transactions,
    SUM(st.total_amount) as lifetime_revenue,
    AVG(st.total_amount) as avg_transaction_value,
    SUM(st.quantity) as total_units_purchased,
    SUM(st.profit_margin) as lifetime_profit,

    -- Temporal behavior
    COUNT(DISTINCT DATE_TRUNC('month', st.transaction_date)) as active_months,
    COUNT(DISTINCT p.category) as categories_purchased,
    COUNT(DISTINCT st.sales_channel) as channels_used,
    COUNT(DISTINCT st.payment_method) as payment_methods_used,

    -- Calculated metrics
    EXTRACT(DAYS FROM MAX(st.transaction_date) - MIN(st.transaction_date)) as customer_lifespan_days,
    AVG(EXTRACT(DAYS FROM st.transaction_date - LAG(st.transaction_date) OVER (
      PARTITION BY st.customer_id ORDER BY st.transaction_date
    ))) as avg_days_between_purchases

  FROM sales_transactions st
  JOIN customers c ON st.customer_id = c.customer_id
  JOIN products p ON st.product_id = p.product_id
  WHERE st.transaction_date >= CURRENT_TIMESTAMP - INTERVAL '365 days'
  GROUP BY st.customer_id, c.customer_segment, c.registration_date, c.age, c.gender, c.city, c.state
),

customer_segmentation_and_prediction AS (
  SELECT 
    cth.*,

    -- CLV calculations
    CASE 
      WHEN avg_days_between_purchases > 0 AND avg_days_between_purchases <= 365 THEN
        avg_transaction_value * (365 / avg_days_between_purchases)
      ELSE lifetime_revenue
    END as predicted_annual_value,

    CASE 
      WHEN avg_days_between_purchases > 0 AND avg_days_between_purchases <= 365 THEN
        avg_transaction_value * (30 / avg_days_between_purchases)
      ELSE lifetime_revenue / GREATEST(active_months, 1)
    END as predicted_monthly_value,

    -- RFM scoring
    NTILE(5) OVER (ORDER BY last_purchase_date DESC) as recency_score,
    NTILE(5) OVER (ORDER BY total_transactions DESC) as frequency_score, 
    NTILE(5) OVER (ORDER BY lifetime_revenue DESC) as monetary_score,

    -- Customer lifecycle classification
    CASE 
      WHEN last_purchase_date >= CURRENT_TIMESTAMP - INTERVAL '30 days' THEN 'active'
      WHEN last_purchase_date >= CURRENT_TIMESTAMP - INTERVAL '90 days' THEN 'at_risk'
      WHEN last_purchase_date >= CURRENT_TIMESTAMP - INTERVAL '180 days' THEN 'dormant'
      ELSE 'churned'
    END as lifecycle_status,

    -- Value segmentation
    CASE 
      WHEN lifetime_revenue >= 5000 THEN 'vip'
      WHEN lifetime_revenue >= 1000 THEN 'high_value'
      WHEN lifetime_revenue >= 500 THEN 'medium_value'
      WHEN lifetime_revenue >= 100 THEN 'low_value'
      ELSE 'minimal_value'
    END as value_segment,

    -- Engagement classification
    CASE 
      WHEN total_transactions >= 20 THEN 'highly_engaged'
      WHEN total_transactions >= 10 THEN 'engaged'
      WHEN total_transactions >= 5 THEN 'moderately_engaged'
      ELSE 'low_engagement'
    END as engagement_level,

    -- Churn risk assessment
    CASE 
      WHEN last_purchase_date < CURRENT_TIMESTAMP - INTERVAL '90 days' AND avg_days_between_purchases < 60 THEN 'high_risk'
      WHEN last_purchase_date < CURRENT_TIMESTAMP - INTERVAL '60 days' AND avg_days_between_purchases < 45 THEN 'medium_risk'
      WHEN last_purchase_date < CURRENT_TIMESTAMP - INTERVAL '30 days' AND total_transactions > 5 THEN 'low_risk'
      ELSE 'minimal_risk'
    END as churn_risk

  FROM customer_transaction_history cth
)

SELECT 
  -- Customer lifetime value summary
  JSON_OBJECT(
    'total_customers_analyzed', COUNT(*),
    'total_historical_revenue', SUM(lifetime_revenue),
    'total_predicted_annual_revenue', SUM(predicted_annual_value),
    'avg_customer_lifetime_value', AVG(lifetime_revenue),
    'avg_predicted_annual_value', AVG(predicted_annual_value),
    'avg_customer_lifespan_days', AVG(customer_lifespan_days),
    'avg_purchase_frequency_days', AVG(avg_days_between_purchases)
  ) as clv_summary,

  -- Value segment distribution
  (SELECT JSON_OBJECT_AGG(
    value_segment,
    JSON_OBJECT(
      'customer_count', COUNT(*),
      'total_revenue', SUM(lifetime_revenue),
      'avg_revenue_per_customer', AVG(lifetime_revenue),
      'avg_predicted_annual_value', AVG(predicted_annual_value),
      'avg_transactions', AVG(total_transactions),
      'revenue_share_percent', ROUND(SUM(lifetime_revenue) / SUM(SUM(lifetime_revenue)) OVER () * 100, 2)
    )
  ) FROM customer_segmentation_and_prediction GROUP BY value_segment) as value_segments,

  -- Lifecycle status analysis
  (SELECT JSON_OBJECT_AGG(
    lifecycle_status,
    JSON_OBJECT(
      'customer_count', COUNT(*),
      'total_revenue', SUM(lifetime_revenue),
      'avg_revenue_per_customer', AVG(lifetime_revenue),
      'avg_recency_score', AVG(recency_score),
      'avg_frequency_score', AVG(frequency_score),
      'avg_monetary_score', AVG(monetary_score)
    )
  ) FROM customer_segmentation_and_prediction GROUP BY lifecycle_status) as lifecycle_analysis,

  -- Churn risk assessment
  (SELECT JSON_OBJECT_AGG(
    churn_risk,
    JSON_OBJECT(
      'customer_count', COUNT(*),
      'at_risk_revenue', SUM(lifetime_revenue),
      'avg_predicted_annual_loss', AVG(predicted_annual_value),
      'high_value_customers_at_risk', COUNT(*) FILTER (WHERE value_segment IN ('vip', 'high_value'))
    )
  ) FROM customer_segmentation_and_prediction GROUP BY churn_risk) as churn_risk_analysis,

  -- Top performers
  (SELECT JSON_AGG(
    JSON_OBJECT(
      'customer_id', customer_id,
      'lifetime_revenue', ROUND(lifetime_revenue::NUMERIC, 2),
      'predicted_annual_value', ROUND(predicted_annual_value::NUMERIC, 2),
      'total_transactions', total_transactions,
      'customer_lifespan_days', customer_lifespan_days,
      'avg_transaction_value', ROUND(avg_transaction_value::NUMERIC, 2),
      'value_segment', value_segment,
      'engagement_level', engagement_level,
      'rfm_combined_score', recency_score + frequency_score + monetary_score,
      'churn_risk', churn_risk
    ) ORDER BY lifetime_revenue DESC LIMIT 20
  )) as top_customers,

  CURRENT_TIMESTAMP as analysis_generated_at

FROM customer_segmentation_and_prediction;

-- Real-time analytics performance monitoring
CREATE VIEW analytics_performance_dashboard AS
WITH performance_metrics AS (
  SELECT 
    -- Query performance indicators
    COUNT(*) as total_dashboard_queries_24h,
    AVG(query_duration_ms) as avg_query_duration_ms,
    PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY query_duration_ms) as p95_query_duration_ms,
    MAX(query_duration_ms) as max_query_duration_ms,

    -- Data freshness metrics
    AVG(EXTRACT(MINUTES FROM query_timestamp - data_timestamp)) as avg_data_age_minutes,
    MAX(EXTRACT(MINUTES FROM query_timestamp - data_timestamp)) as max_data_age_minutes,

    -- Cache performance
    COUNT(*) FILTER (WHERE cache_hit = true) as cache_hits,
    COUNT(*) FILTER (WHERE cache_hit = false) as cache_misses,

    -- Resource utilization
    AVG(memory_usage_mb) as avg_memory_usage_mb,
    MAX(memory_usage_mb) as peak_memory_usage_mb,
    AVG(cpu_utilization_percent) as avg_cpu_utilization,

    -- Error rates
    COUNT(*) FILTER (WHERE query_status = 'error') as query_errors,
    COUNT(*) FILTER (WHERE query_status = 'timeout') as query_timeouts

  FROM analytics_query_log
  WHERE query_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
)

SELECT 
  CURRENT_TIMESTAMP as dashboard_time,

  -- Performance indicators
  total_dashboard_queries_24h,
  ROUND(avg_query_duration_ms::NUMERIC, 2) as avg_response_time_ms,
  ROUND(p95_query_duration_ms::NUMERIC, 2) as p95_response_time_ms,
  ROUND(max_query_duration_ms::NUMERIC, 2) as max_response_time_ms,

  -- Data quality indicators
  ROUND(avg_data_age_minutes::NUMERIC, 2) as avg_data_freshness_minutes,
  ROUND(max_data_age_minutes::NUMERIC, 2) as max_data_age_minutes,

  -- Cache effectiveness
  cache_hits,
  cache_misses,
  CASE 
    WHEN (cache_hits + cache_misses) > 0 THEN
      ROUND((cache_hits::FLOAT / (cache_hits + cache_misses) * 100)::NUMERIC, 2)
    ELSE 0
  END as cache_hit_rate_percent,

  -- System resource utilization
  ROUND(avg_memory_usage_mb::NUMERIC, 2) as avg_memory_mb,
  ROUND(peak_memory_usage_mb::NUMERIC, 2) as peak_memory_mb,
  ROUND(avg_cpu_utilization::NUMERIC, 2) as avg_cpu_percent,

  -- Reliability indicators
  query_errors,
  query_timeouts,
  CASE 
    WHEN total_dashboard_queries_24h > 0 THEN
      ROUND(((total_dashboard_queries_24h - query_errors - query_timeouts)::FLOAT / total_dashboard_queries_24h * 100)::NUMERIC, 2)
    ELSE 100
  END as success_rate_percent,

  -- Health status
  CASE 
    WHEN avg_query_duration_ms > 5000 OR (query_errors + query_timeouts) > total_dashboard_queries_24h * 0.05 THEN 'critical'
    WHEN avg_query_duration_ms > 2000 OR (query_errors + query_timeouts) > total_dashboard_queries_24h * 0.02 THEN 'warning'
    ELSE 'healthy'
  END as system_health,

  -- Performance recommendations
  ARRAY[
    CASE WHEN avg_query_duration_ms > 3000 THEN 'Consider query optimization or caching improvements' END,
    CASE WHEN cache_hit_rate_percent < 70 THEN 'Cache hit rate is low - review caching strategy' END,
    CASE WHEN avg_data_age_minutes > 10 THEN 'Data freshness may impact real-time insights' END,
    CASE WHEN peak_memory_usage_mb > 1000 THEN 'High memory usage detected - consider resource scaling' END
  ]::TEXT[] as recommendations

FROM performance_metrics;

-- QueryLeaf provides comprehensive MongoDB analytics capabilities:
-- 1. SQL-familiar syntax for complex aggregation pipelines and dashboard queries
-- 2. Advanced real-time analytics with multi-dimensional data processing
-- 3. Customer lifetime value analysis with predictive modeling capabilities
-- 4. Sophisticated segmentation and behavioral analysis through SQL constructs
-- 5. Real-time performance monitoring with comprehensive health indicators
-- 6. Advanced temporal trend analysis with growth rate calculations
-- 7. Production-ready analytics operations with caching and optimization
-- 8. Integration with MongoDB's native aggregation framework optimizations
-- 9. Comprehensive business intelligence with statistical analysis support
-- 10. Enterprise-grade analytics dashboards accessible through familiar SQL patterns

Best Practices for Production Analytics Implementation

Analytics Pipeline Design and Optimization

Essential principles for effective MongoDB analytics dashboard deployment:

  1. Data Modeling Strategy: Design analytics-optimized schemas with appropriate indexing strategies for time-series and dimensional queries
  2. Aggregation Optimization: Implement efficient aggregation pipelines with proper stage ordering and memory-conscious operations
  3. Caching Architecture: Deploy intelligent caching layers that balance data freshness with query performance requirements
  4. Real-Time Processing: Configure change stream integration for live dashboard updates without performance degradation
  5. Scalability Design: Architect analytics systems that can handle growing data volumes and increasing concurrent user loads
  6. Performance Monitoring: Implement comprehensive monitoring that tracks query performance, resource utilization, and user experience metrics

Enterprise Analytics Deployment

Optimize analytics platforms for production enterprise environments:

  1. Distributed Processing: Implement distributed analytics processing that can leverage MongoDB's sharding capabilities for massive datasets
  2. Security Integration: Ensure analytics operations meet enterprise security requirements with proper access controls and data governance
  3. Compliance Framework: Design analytics systems that support regulatory requirements for data retention, audit trails, and reporting
  4. Operational Integration: Integrate analytics platforms with existing monitoring, alerting, and business intelligence infrastructure
  5. Multi-Tenant Architecture: Support multiple business units and use cases with scalable, isolated analytics environments
  6. Cost Optimization: Monitor and optimize analytics resource usage and processing costs for efficient operations

Conclusion

MongoDB's Aggregation Framework provides sophisticated real-time analytics capabilities that enable powerful dashboard creation, complex data processing, and comprehensive business intelligence without the complexity and infrastructure overhead of traditional analytics platforms. Native aggregation operations offer scalable, efficient, and flexible data processing directly within the operational database.

Key MongoDB Analytics benefits include:

  • Real-Time Processing: Immediate insight generation from operational data without ETL delays or separate analytics infrastructure
  • Advanced Aggregations: Sophisticated multi-stage data processing with statistical calculations, temporal analysis, and predictive modeling
  • Flexible Analytics: Dynamic dashboard creation with customizable metrics, dimensions, and filtering capabilities
  • Scalable Architecture: Native MongoDB integration that scales efficiently with data growth and analytical complexity
  • Performance Optimization: Built-in optimization features with intelligent caching, indexing, and query planning
  • SQL Accessibility: Familiar SQL-style analytics operations through QueryLeaf for accessible business intelligence development

Whether you're building executive dashboards, operational analytics, customer insights platforms, or real-time monitoring systems, MongoDB aggregation with QueryLeaf's familiar SQL interface provides the foundation for powerful, scalable, and efficient analytics solutions.

QueryLeaf Integration: QueryLeaf automatically optimizes MongoDB aggregation pipelines while providing SQL-familiar syntax for complex analytics operations. Advanced dashboard creation, customer segmentation, and predictive analytics are seamlessly handled through familiar SQL constructs, making sophisticated business intelligence accessible to SQL-oriented development teams without requiring deep MongoDB aggregation expertise.

The combination of MongoDB's robust aggregation capabilities with SQL-style analytics operations makes it an ideal platform for applications requiring both real-time operational data processing and familiar business intelligence patterns, ensuring your analytics solutions can deliver immediate insights while maintaining development team productivity and system performance.

MongoDB Transaction Error Handling and Recovery Patterns: Building Resilient Applications with Advanced Error Management and Automatic Retry Strategies

Production MongoDB applications require sophisticated error handling and recovery mechanisms that can gracefully manage transaction failures, network interruptions, server unavailability, and resource constraints while maintaining data consistency and application reliability. Traditional database error handling approaches often lack the nuanced understanding of distributed system challenges, leading to incomplete transactions, data inconsistencies, and poor user experiences when dealing with complex failure scenarios.

MongoDB provides comprehensive transaction error handling capabilities through intelligent retry mechanisms, detailed error classification, and sophisticated recovery patterns that enable applications to maintain consistency and reliability even in the face of network partitions, replica set failovers, and resource contention. Unlike traditional databases that provide basic error codes and limited retry logic, MongoDB transactions integrate advanced error detection with automatic recovery strategies and detailed diagnostic information.

The Traditional Transaction Error Handling Challenge

Conventional approaches to database transaction error management in enterprise applications face significant limitations in resilience and recovery capabilities:

-- Traditional PostgreSQL transaction error handling - basic error management with limited recovery options

-- Simple transaction error tracking table
CREATE TABLE transaction_error_log (
    error_id SERIAL PRIMARY KEY,
    transaction_id UUID,
    connection_id VARCHAR(100),

    -- Basic error information
    error_code VARCHAR(20),
    error_message TEXT,
    error_category VARCHAR(50), -- connection, constraint, timeout, etc.

    -- Timing information
    transaction_start_time TIMESTAMP,
    error_occurred_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Context information (limited)
    table_name VARCHAR(100),
    operation_type VARCHAR(20), -- INSERT, UPDATE, DELETE, SELECT
    affected_rows INTEGER,

    -- Simple retry tracking
    retry_count INTEGER DEFAULT 0,
    max_retries INTEGER DEFAULT 3,
    retry_successful BOOLEAN DEFAULT FALSE,

    -- Manual resolution tracking
    resolved_at TIMESTAMP,
    resolution_method VARCHAR(100),
    resolved_by VARCHAR(100)
);

-- Basic transaction state tracking
CREATE TABLE active_transactions (
    transaction_id UUID PRIMARY KEY,
    connection_id VARCHAR(100) NOT NULL,
    start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Simple state management
    transaction_status VARCHAR(20) DEFAULT 'active', -- active, committed, rolled_back, failed
    isolation_level VARCHAR(30),
    read_only BOOLEAN DEFAULT FALSE,

    -- Basic operation tracking
    operations_count INTEGER DEFAULT 0,
    tables_affected TEXT[], -- Simple array of table names

    -- Timeout management (basic)
    timeout_seconds INTEGER DEFAULT 300,
    last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Error tracking
    error_count INTEGER DEFAULT 0,
    last_error_message TEXT,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Manual transaction recovery procedure (limited functionality)
CREATE OR REPLACE FUNCTION recover_failed_transaction(
    p_transaction_id UUID,
    p_recovery_strategy VARCHAR(50) DEFAULT 'rollback'
) RETURNS TABLE (
    recovery_status VARCHAR(20),
    recovery_message TEXT,
    operations_recovered INTEGER
) AS $$
DECLARE
    v_transaction_record RECORD;
    v_recovery_count INTEGER := 0;
    v_retry_count INTEGER;
    v_max_retries INTEGER;
BEGIN
    -- Get transaction details
    SELECT * INTO v_transaction_record 
    FROM active_transactions 
    WHERE transaction_id = p_transaction_id;

    IF NOT FOUND THEN
        RETURN QUERY SELECT 'error'::VARCHAR(20), 
                           'Transaction not found'::TEXT, 
                           0::INTEGER;
        RETURN;
    END IF;

    -- Check retry limits (basic logic)
    SELECT retry_count, max_retries INTO v_retry_count, v_max_retries
    FROM transaction_error_log
    WHERE transaction_id = p_transaction_id
    ORDER BY error_occurred_at DESC
    LIMIT 1;

    IF v_retry_count >= v_max_retries THEN
        RETURN QUERY SELECT 'failed'::VARCHAR(20), 
                           'Maximum retries exceeded'::TEXT, 
                           0::INTEGER;
        RETURN;
    END IF;

    -- Simple recovery strategies
    CASE p_recovery_strategy
        WHEN 'rollback' THEN
            BEGIN
                -- Attempt to rollback (very basic)
                UPDATE active_transactions 
                SET transaction_status = 'rolled_back',
                    updated_at = CURRENT_TIMESTAMP
                WHERE transaction_id = p_transaction_id;

                v_recovery_count := 1;

                RETURN QUERY SELECT 'success'::VARCHAR(20), 
                                   'Transaction rolled back'::TEXT, 
                                   v_recovery_count::INTEGER;
            EXCEPTION WHEN OTHERS THEN
                RETURN QUERY SELECT 'error'::VARCHAR(20), 
                                   SQLERRM::TEXT, 
                                   0::INTEGER;
            END;

        WHEN 'retry' THEN
            BEGIN
                -- Basic retry logic (very limited)
                UPDATE transaction_error_log 
                SET retry_count = retry_count + 1,
                    retry_successful = FALSE
                WHERE transaction_id = p_transaction_id;

                -- Reset transaction status for retry
                UPDATE active_transactions 
                SET transaction_status = 'active',
                    error_count = 0,
                    last_error_message = NULL,
                    updated_at = CURRENT_TIMESTAMP
                WHERE transaction_id = p_transaction_id;

                v_recovery_count := 1;

                RETURN QUERY SELECT 'retry'::VARCHAR(20), 
                                   'Transaction queued for retry'::TEXT, 
                                   v_recovery_count::INTEGER;
            EXCEPTION WHEN OTHERS THEN
                RETURN QUERY SELECT 'error'::VARCHAR(20), 
                                   SQLERRM::TEXT, 
                                   0::INTEGER;
            END;

        ELSE
            RETURN QUERY SELECT 'error'::VARCHAR(20), 
                               'Unknown recovery strategy'::TEXT, 
                               0::INTEGER;
    END CASE;
END;
$$ LANGUAGE plpgsql;

-- Basic transaction monitoring query (limited insights)
WITH transaction_health AS (
    SELECT 
        DATE_TRUNC('hour', start_time) as hour_bucket,

        -- Simple transaction metrics
        COUNT(*) as total_transactions,
        COUNT(CASE WHEN transaction_status = 'committed' THEN 1 END) as successful_transactions,
        COUNT(CASE WHEN transaction_status = 'rolled_back' THEN 1 END) as rolled_back_transactions,
        COUNT(CASE WHEN transaction_status = 'failed' THEN 1 END) as failed_transactions,
        COUNT(CASE WHEN transaction_status = 'active' AND 
                        EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - last_activity)) > timeout_seconds 
                  THEN 1 END) as timed_out_transactions,

        -- Basic performance metrics
        AVG(operations_count) as avg_operations_per_transaction,
        AVG(EXTRACT(EPOCH FROM (updated_at - start_time))) as avg_transaction_duration_seconds,

        -- Simple error analysis
        AVG(error_count) as avg_errors_per_transaction,
        COUNT(CASE WHEN error_count > 0 THEN 1 END) as transactions_with_errors

    FROM active_transactions
    WHERE start_time >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
    GROUP BY DATE_TRUNC('hour', start_time)
),

error_analysis AS (
    SELECT 
        DATE_TRUNC('hour', error_occurred_at) as hour_bucket,
        error_category,

        -- Error statistics
        COUNT(*) as error_count,
        COUNT(CASE WHEN retry_successful = TRUE THEN 1 END) as successful_retries,
        AVG(retry_count) as avg_retry_attempts,

        -- Common errors
        COUNT(CASE WHEN error_code LIKE 'SQLSTATE%' THEN 1 END) as sql_state_errors,
        COUNT(CASE WHEN error_message ILIKE '%timeout%' THEN 1 END) as timeout_errors,
        COUNT(CASE WHEN error_message ILIKE '%connection%' THEN 1 END) as connection_errors,
        COUNT(CASE WHEN error_message ILIKE '%deadlock%' THEN 1 END) as deadlock_errors

    FROM transaction_error_log
    WHERE error_occurred_at >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
    GROUP BY DATE_TRUNC('hour', error_occurred_at), error_category
)

SELECT 
    th.hour_bucket,

    -- Transaction metrics
    th.total_transactions,
    th.successful_transactions,
    th.failed_transactions,
    ROUND((th.successful_transactions::DECIMAL / GREATEST(th.total_transactions, 1)) * 100, 2) as success_rate_percent,

    -- Performance metrics
    ROUND(th.avg_transaction_duration_seconds, 3) as avg_duration_seconds,
    ROUND(th.avg_operations_per_transaction, 1) as avg_operations,

    -- Error metrics
    COALESCE(SUM(ea.error_count), 0) as total_errors,
    COALESCE(SUM(ea.successful_retries), 0) as successful_retries,
    COALESCE(ROUND(AVG(ea.avg_retry_attempts), 1), 0) as avg_retry_attempts,

    -- Error categories
    COALESCE(SUM(ea.timeout_errors), 0) as timeout_errors,
    COALESCE(SUM(ea.connection_errors), 0) as connection_errors,
    COALESCE(SUM(ea.deadlock_errors), 0) as deadlock_errors,

    -- Health indicators
    th.timed_out_transactions,
    CASE 
        WHEN ROUND((th.successful_transactions::DECIMAL / GREATEST(th.total_transactions, 1)) * 100, 2) >= 95 THEN 'Healthy'
        WHEN ROUND((th.successful_transactions::DECIMAL / GREATEST(th.total_transactions, 1)) * 100, 2) >= 90 THEN 'Warning'
        ELSE 'Critical'
    END as health_status

FROM transaction_health th
LEFT JOIN error_analysis ea ON th.hour_bucket = ea.hour_bucket
GROUP BY th.hour_bucket, th.total_transactions, th.successful_transactions, 
         th.failed_transactions, th.avg_transaction_duration_seconds, 
         th.avg_operations_per_transaction, th.timed_out_transactions
ORDER BY th.hour_bucket DESC;

-- Problems with traditional transaction error handling:
-- 1. Basic error categorization with limited diagnostic information
-- 2. Manual retry logic without intelligent backoff strategies
-- 3. No automatic recovery based on error type and context
-- 4. Limited visibility into transaction state and progress
-- 5. Basic timeout handling without consideration of operation complexity
-- 6. No integration with connection pool health and server status
-- 7. Manual intervention required for most recovery scenarios
-- 8. Limited support for distributed transaction patterns
-- 9. Basic error aggregation without trend analysis
-- 10. No automatic optimization based on error patterns

MongoDB's intelligent transaction error handling eliminates these limitations:

// MongoDB advanced transaction error handling - intelligent and resilient
const { MongoClient } = require('mongodb');

// Comprehensive transaction error handling and recovery system
class MongoTransactionManager {
  constructor(client, options = {}) {
    this.client = client;
    this.options = {
      // Retry configuration
      maxRetryAttempts: options.maxRetryAttempts || 5,
      initialRetryDelayMs: options.initialRetryDelayMs || 100,
      maxRetryDelayMs: options.maxRetryDelayMs || 5000,
      retryDelayMultiplier: options.retryDelayMultiplier || 2,
      jitterFactor: options.jitterFactor || 0.1,

      // Transaction configuration
      defaultTransactionOptions: {
        readConcern: { level: options.readConcernLevel || 'snapshot' },
        writeConcern: { w: options.writeConcernW || 'majority', j: true },
        readPreference: options.readPreference || 'primary',
        maxCommitTimeMS: options.maxCommitTimeMS || 10000
      },

      // Error handling configuration
      retryableErrorCodes: options.retryableErrorCodes || [
        112, // WriteConflict
        117, // ConflictingOperationInProgress  
        133, // FailedToSatisfyReadPreference
        134, // ReadConcernMajorityNotAvailableYet
        208, // ExceededTimeLimit
        225, // LockTimeout
        244, // TransactionTooLarge
        251, // NoSuchTransaction
        256, // TransactionAborted
        261, // ExceededMaxTimeMS
        263, // TemporarilyUnavailable
        6   // HostUnreachable
      ],

      // Monitoring configuration
      enableDetailedLogging: options.enableDetailedLogging || true,
      enableMetricsCollection: options.enableMetricsCollection || true
    };

    this.transactionMetrics = {
      totalTransactions: 0,
      successfulTransactions: 0,
      failedTransactions: 0,
      retriedTransactions: 0,
      totalRetryAttempts: 0,
      errorsByCode: new Map(),
      errorsByCategory: new Map(),
      performanceStats: {
        averageTransactionDuration: 0,
        transactionDurations: [],
        retryDelays: [],
        averageRetryDelay: 0
      }
    };

    this.activeTransactions = new Map();
  }

  // Execute transaction with comprehensive error handling and retry logic
  async executeTransactionWithRetry(transactionFunction, transactionOptions = {}) {
    const transactionId = this.generateTransactionId();
    const startTime = Date.now();

    // Merge transaction options
    const mergedOptions = {
      ...this.options.defaultTransactionOptions,
      ...transactionOptions
    };

    let attempt = 1;
    let lastError = null;
    let session = null;

    // Track active transaction
    this.activeTransactions.set(transactionId, {
      id: transactionId,
      startTime: startTime,
      attempt: attempt,
      status: 'active',
      operationsExecuted: 0,
      errors: []
    });

    try {
      while (attempt <= this.options.maxRetryAttempts) {
        try {
          // Create new session for each attempt
          session = this.client.startSession();

          this.log(`Starting transaction ${transactionId}, attempt ${attempt}`);

          // Update transaction tracking
          this.updateTransactionStatus(transactionId, 'active', { attempt });

          // Execute transaction with intelligent error handling
          const result = await session.withTransaction(
            async (sessionContext) => {
              try {
                // Execute the user-provided transaction function
                const transactionResult = await transactionFunction(sessionContext, {
                  transactionId,
                  attempt,
                  onOperation: (operation) => this.trackOperation(transactionId, operation)
                });

                this.log(`Transaction ${transactionId} executed successfully on attempt ${attempt}`);
                return transactionResult;

              } catch (error) {
                this.log(`Transaction ${transactionId} error in user function:`, error);
                throw error;
              }
            },
            mergedOptions
          );

          // Transaction successful
          const duration = Date.now() - startTime;

          this.updateTransactionStatus(transactionId, 'committed', { 
            duration,
            totalAttempts: attempt 
          });

          this.recordSuccessfulTransaction(transactionId, duration, attempt);

          this.log(`Transaction ${transactionId} committed successfully after ${attempt} attempts (${duration}ms)`);

          return {
            success: true,
            result: result,
            transactionId: transactionId,
            attempts: attempt,
            duration: duration,
            metrics: this.getTransactionMetrics(transactionId)
          };

        } catch (error) {
          lastError = error;

          this.log(`Transaction ${transactionId} attempt ${attempt} failed:`, error);

          // Record error for analysis
          this.recordTransactionError(transactionId, error, attempt);

          // Analyze error and determine if retry is appropriate
          const errorAnalysis = this.analyzeTransactionError(error);

          if (!errorAnalysis.retryable || attempt >= this.options.maxRetryAttempts) {
            // Error is not retryable or max attempts reached
            this.updateTransactionStatus(transactionId, 'failed', { 
              finalError: error,
              totalAttempts: attempt,
              errorAnalysis 
            });

            break;
          }

          // Calculate intelligent retry delay
          const retryDelay = this.calculateRetryDelay(attempt, errorAnalysis);

          this.log(`Transaction ${transactionId} will retry in ${retryDelay}ms (attempt ${attempt + 1}/${this.options.maxRetryAttempts})`);

          // Update metrics
          this.transactionMetrics.totalRetryAttempts++;
          this.transactionMetrics.performanceStats.retryDelays.push(retryDelay);

          // Wait before retry
          if (retryDelay > 0) {
            await this.sleep(retryDelay);
          }

          attempt++;

        } finally {
          // Always close session
          if (session) {
            try {
              await session.endSession();
            } catch (sessionError) {
              this.log(`Error ending session for transaction ${transactionId}:`, sessionError);
            }
          }
        }
      }

      // All retries exhausted
      const totalDuration = Date.now() - startTime;

      this.recordFailedTransaction(transactionId, lastError, attempt - 1, totalDuration);

      this.log(`Transaction ${transactionId} failed after ${attempt - 1} attempts (${totalDuration}ms)`);

      return {
        success: false,
        error: lastError,
        transactionId: transactionId,
        attempts: attempt - 1,
        duration: totalDuration,
        errorAnalysis: this.analyzeTransactionError(lastError),
        metrics: this.getTransactionMetrics(transactionId),
        recoveryRecommendations: this.generateRecoveryRecommendations(transactionId, lastError)
      };

    } finally {
      // Clean up transaction tracking
      this.activeTransactions.delete(transactionId);
    }
  }

  // Intelligent error analysis for MongoDB transactions
  analyzeTransactionError(error) {
    const analysis = {
      errorCode: error.code,
      errorMessage: error.message,
      errorName: error.name,
      retryable: false,
      category: 'unknown',
      severity: 'medium',
      recommendedAction: 'investigate',
      estimatedRecoveryTime: 0,
      contextualInfo: {}
    };

    // Categorize error based on code and message
    if (error.code) {
      // Transient errors that should be retried
      if (this.options.retryableErrorCodes.includes(error.code)) {
        analysis.retryable = true;
        analysis.category = this.categorizeMongoError(error.code);
        analysis.severity = 'low';
        analysis.recommendedAction = 'retry';
        analysis.estimatedRecoveryTime = this.estimateRecoveryTime(error.code);
      }

      // Specific error code analysis
      switch (error.code) {
        case 112: // WriteConflict
          analysis.category = 'concurrency';
          analysis.recommendedAction = 'retry_with_backoff';
          analysis.contextualInfo.suggestion = 'Consider optimizing transaction scope to reduce conflicts';
          break;

        case 117: // ConflictingOperationInProgress
          analysis.category = 'concurrency';
          analysis.recommendedAction = 'retry_with_longer_delay';
          analysis.contextualInfo.suggestion = 'Wait for conflicting operation to complete';
          break;

        case 133: // FailedToSatisfyReadPreference
          analysis.category = 'availability';
          analysis.recommendedAction = 'check_replica_set_status';
          analysis.contextualInfo.suggestion = 'Verify replica set member availability';
          break;

        case 208: // ExceededTimeLimit
        case 261: // ExceededMaxTimeMS
          analysis.category = 'timeout';
          analysis.recommendedAction = 'optimize_or_increase_timeout';
          analysis.contextualInfo.suggestion = 'Consider breaking transaction into smaller operations';
          break;

        case 244: // TransactionTooLarge
          analysis.category = 'resource';
          analysis.retryable = false;
          analysis.severity = 'high';
          analysis.recommendedAction = 'reduce_transaction_size';
          analysis.contextualInfo.suggestion = 'Split transaction into smaller operations';
          break;

        case 251: // NoSuchTransaction
          analysis.category = 'state';
          analysis.recommendedAction = 'restart_transaction';
          analysis.contextualInfo.suggestion = 'Transaction may have been cleaned up by server';
          break;

        case 256: // TransactionAborted
          analysis.category = 'aborted';
          analysis.recommendedAction = 'retry_full_transaction';
          analysis.contextualInfo.suggestion = 'Transaction was aborted due to conflict or timeout';
          break;
      }
    }

    // Network-related errors
    if (error.message && (
      error.message.includes('network') || 
      error.message.includes('connection') ||
      error.message.includes('timeout') ||
      error.message.includes('unreachable')
    )) {
      analysis.retryable = true;
      analysis.category = 'network';
      analysis.recommendedAction = 'retry_with_exponential_backoff';
      analysis.estimatedRecoveryTime = 5000; // 5 seconds
      analysis.contextualInfo.suggestion = 'Check network connectivity and server status';
    }

    // Resource exhaustion errors
    if (error.message && (
      error.message.includes('memory') ||
      error.message.includes('disk space') ||
      error.message.includes('too many connections')
    )) {
      analysis.retryable = true;
      analysis.category = 'resource';
      analysis.severity = 'high';
      analysis.recommendedAction = 'wait_for_resources';
      analysis.estimatedRecoveryTime = 10000; // 10 seconds
      analysis.contextualInfo.suggestion = 'Monitor server resource usage';
    }

    return analysis;
  }

  categorizeMongoError(errorCode) {
    const errorCategories = {
      112: 'concurrency',    // WriteConflict
      117: 'concurrency',    // ConflictingOperationInProgress
      133: 'availability',   // FailedToSatisfyReadPreference
      134: 'availability',   // ReadConcernMajorityNotAvailableYet
      208: 'timeout',        // ExceededTimeLimit
      225: 'concurrency',    // LockTimeout
      244: 'resource',       // TransactionTooLarge
      251: 'state',          // NoSuchTransaction
      256: 'aborted',        // TransactionAborted
      261: 'timeout',        // ExceededMaxTimeMS
      263: 'availability',   // TemporarilyUnavailable
      6: 'network'           // HostUnreachable
    };

    return errorCategories[errorCode] || 'unknown';
  }

  estimateRecoveryTime(errorCode) {
    const recoveryTimes = {
      112: 100,   // WriteConflict - quick retry
      117: 500,   // ConflictingOperationInProgress - wait for operation
      133: 2000,  // FailedToSatisfyReadPreference - wait for replica
      134: 1000,  // ReadConcernMajorityNotAvailableYet - wait for majority
      208: 5000,  // ExceededTimeLimit - wait before retry
      225: 200,   // LockTimeout - quick retry
      251: 100,   // NoSuchTransaction - immediate retry
      256: 300,   // TransactionAborted - short wait
      261: 3000,  // ExceededMaxTimeMS - moderate wait
      263: 1000,  // TemporarilyUnavailable - short wait
      6: 5000     // HostUnreachable - wait for network
    };

    return recoveryTimes[errorCode] || 1000;
  }

  // Calculate intelligent retry delay with exponential backoff and jitter
  calculateRetryDelay(attemptNumber, errorAnalysis) {
    // Base delay calculation with exponential backoff
    let baseDelay = Math.min(
      this.options.initialRetryDelayMs * Math.pow(this.options.retryDelayMultiplier, attemptNumber - 1),
      this.options.maxRetryDelayMs
    );

    // Adjust based on error analysis
    if (errorAnalysis.estimatedRecoveryTime > 0) {
      baseDelay = Math.max(baseDelay, errorAnalysis.estimatedRecoveryTime);
    }

    // Add jitter to prevent thundering herd
    const jitterRange = baseDelay * this.options.jitterFactor;
    const jitter = (Math.random() * 2 - 1) * jitterRange; // Random value between -jitterRange and +jitterRange

    const finalDelay = Math.max(0, Math.floor(baseDelay + jitter));

    this.log(`Calculated retry delay: base=${baseDelay}ms, jitter=${jitter.toFixed(1)}ms, final=${finalDelay}ms`);

    return finalDelay;
  }

  // Generate recovery recommendations based on error patterns
  generateRecoveryRecommendations(transactionId, error) {
    const recommendations = [];
    const errorAnalysis = this.analyzeTransactionError(error);

    // Category-specific recommendations
    switch (errorAnalysis.category) {
      case 'concurrency':
        recommendations.push({
          type: 'optimization',
          priority: 'medium',
          description: 'Optimize transaction scope to reduce write conflicts',
          actions: [
            'Consider breaking large transactions into smaller operations',
            'Review document access patterns for optimization opportunities',
            'Implement optimistic locking where appropriate'
          ]
        });
        break;

      case 'timeout':
        recommendations.push({
          type: 'configuration',
          priority: 'high',
          description: 'Address transaction timeout issues',
          actions: [
            'Increase maxCommitTimeMS if operations are legitimately slow',
            'Optimize query performance with proper indexing',
            'Consider breaking complex operations into smaller transactions'
          ]
        });
        break;

      case 'resource':
        recommendations.push({
          type: 'scaling',
          priority: 'high',
          description: 'Address resource constraints',
          actions: [
            'Monitor server resource usage (CPU, memory, disk)',
            'Consider vertical or horizontal scaling',
            'Implement connection pooling optimization'
          ]
        });
        break;

      case 'network':
        recommendations.push({
          type: 'infrastructure',
          priority: 'high',
          description: 'Address network connectivity issues',
          actions: [
            'Check network connectivity between application and database',
            'Verify MongoDB server status and availability',
            'Consider implementing circuit breaker pattern'
          ]
        });
        break;

      case 'availability':
        recommendations.push({
          type: 'deployment',
          priority: 'high',
          description: 'Address replica set availability',
          actions: [
            'Check replica set member status',
            'Verify read preference configuration',
            'Monitor replica lag and catch-up status'
          ]
        });
        break;
    }

    // Pattern-based recommendations
    const transactionHistory = this.getTransactionHistory(transactionId);
    if (transactionHistory && transactionHistory.errors.length > 1) {
      // Check for recurring error patterns
      const errorCodes = transactionHistory.errors.map(e => e.code);
      const uniqueErrorCodes = [...new Set(errorCodes)];

      if (uniqueErrorCodes.length === 1) {
        recommendations.push({
          type: 'pattern',
          priority: 'high',
          description: 'Recurring error pattern detected',
          actions: [
            `Address root cause of error ${uniqueErrorCodes[0]}`,
            'Consider implementing circuit breaker pattern',
            'Review application architecture for reliability improvements'
          ]
        });
      }
    }

    return recommendations;
  }

  // Advanced transaction monitoring and metrics collection
  recordSuccessfulTransaction(transactionId, duration, attempts) {
    this.transactionMetrics.totalTransactions++;
    this.transactionMetrics.successfulTransactions++;

    if (attempts > 1) {
      this.transactionMetrics.retriedTransactions++;
    }

    // Update performance statistics
    this.transactionMetrics.performanceStats.transactionDurations.push(duration);

    // Keep only recent durations for average calculation
    if (this.transactionMetrics.performanceStats.transactionDurations.length > 1000) {
      this.transactionMetrics.performanceStats.transactionDurations = 
        this.transactionMetrics.performanceStats.transactionDurations.slice(-500);
    }

    // Recalculate average
    this.transactionMetrics.performanceStats.averageTransactionDuration = 
      this.transactionMetrics.performanceStats.transactionDurations.reduce((sum, d) => sum + d, 0) /
      this.transactionMetrics.performanceStats.transactionDurations.length;

    this.log(`Transaction ${transactionId} metrics recorded: duration=${duration}ms, attempts=${attempts}`);
  }

  recordFailedTransaction(transactionId, error, attempts, duration) {
    this.transactionMetrics.totalTransactions++;
    this.transactionMetrics.failedTransactions++;

    if (attempts > 1) {
      this.transactionMetrics.retriedTransactions++;
    }

    // Record error statistics
    const errorCode = error.code || 'unknown';
    const currentCount = this.transactionMetrics.errorsByCode.get(errorCode) || 0;
    this.transactionMetrics.errorsByCode.set(errorCode, currentCount + 1);

    const errorCategory = this.categorizeMongoError(error.code);
    const currentCategoryCount = this.transactionMetrics.errorsByCategory.get(errorCategory) || 0;
    this.transactionMetrics.errorsByCategory.set(errorCategory, currentCategoryCount + 1);

    this.log(`Transaction ${transactionId} failure recorded: error=${errorCode}, attempts=${attempts}, duration=${duration}ms`);
  }

  recordTransactionError(transactionId, error, attempt) {
    const transaction = this.activeTransactions.get(transactionId);
    if (transaction) {
      transaction.errors.push({
        attempt: attempt,
        error: error,
        timestamp: new Date(),
        errorCode: error.code,
        errorMessage: error.message,
        analysis: this.analyzeTransactionError(error)
      });
    }
  }

  updateTransactionStatus(transactionId, status, additionalInfo = {}) {
    const transaction = this.activeTransactions.get(transactionId);
    if (transaction) {
      transaction.status = status;
      transaction.lastUpdated = new Date();
      Object.assign(transaction, additionalInfo);
    }
  }

  trackOperation(transactionId, operation) {
    const transaction = this.activeTransactions.get(transactionId);
    if (transaction) {
      transaction.operationsExecuted++;
      transaction.lastOperation = {
        type: operation.type,
        collection: operation.collection,
        timestamp: new Date()
      };
    }
  }

  getTransactionMetrics(transactionId) {
    const transaction = this.activeTransactions.get(transactionId);
    return {
      transactionId: transactionId,
      operationsExecuted: transaction ? transaction.operationsExecuted : 0,
      errors: transaction ? transaction.errors : [],
      status: transaction ? transaction.status : 'unknown',
      startTime: transaction ? transaction.startTime : null,
      duration: transaction ? Date.now() - transaction.startTime : 0
    };
  }

  getTransactionHistory(transactionId) {
    return this.activeTransactions.get(transactionId);
  }

  // Comprehensive transaction health monitoring
  getTransactionHealthReport() {
    const report = {
      timestamp: new Date(),
      overall: {
        totalTransactions: this.transactionMetrics.totalTransactions,
        successfulTransactions: this.transactionMetrics.successfulTransactions,
        failedTransactions: this.transactionMetrics.failedTransactions,
        retriedTransactions: this.transactionMetrics.retriedTransactions,
        totalRetryAttempts: this.transactionMetrics.totalRetryAttempts,
        successRate: this.transactionMetrics.totalTransactions > 0 ? 
          (this.transactionMetrics.successfulTransactions / this.transactionMetrics.totalTransactions) * 100 : 0,
        retryRate: this.transactionMetrics.totalTransactions > 0 ?
          (this.transactionMetrics.retriedTransactions / this.transactionMetrics.totalTransactions) * 100 : 0
      },
      performance: {
        averageTransactionDuration: this.transactionMetrics.performanceStats.averageTransactionDuration,
        averageRetryDelay: this.transactionMetrics.performanceStats.retryDelays.length > 0 ?
          this.transactionMetrics.performanceStats.retryDelays.reduce((sum, d) => sum + d, 0) /
          this.transactionMetrics.performanceStats.retryDelays.length : 0,
        totalRecentTransactions: this.transactionMetrics.performanceStats.transactionDurations.length
      },
      errors: {
        byCode: Object.fromEntries(this.transactionMetrics.errorsByCode),
        byCategory: Object.fromEntries(this.transactionMetrics.errorsByCategory),
        mostCommonError: this.getMostCommonError(),
        mostCommonCategory: this.getMostCommonErrorCategory()
      },
      activeTransactions: {
        count: this.activeTransactions.size,
        transactions: Array.from(this.activeTransactions.values()).map(t => ({
          id: t.id,
          status: t.status,
          duration: Date.now() - t.startTime,
          attempts: t.attempt,
          operationsExecuted: t.operationsExecuted,
          errorCount: t.errors ? t.errors.length : 0
        }))
      }
    };

    return report;
  }

  getMostCommonError() {
    let maxCount = 0;
    let mostCommonError = null;

    for (const [errorCode, count] of this.transactionMetrics.errorsByCode.entries()) {
      if (count > maxCount) {
        maxCount = count;
        mostCommonError = { code: errorCode, count: count };
      }
    }

    return mostCommonError;
  }

  getMostCommonErrorCategory() {
    let maxCount = 0;
    let mostCommonCategory = null;

    for (const [category, count] of this.transactionMetrics.errorsByCategory.entries()) {
      if (count > maxCount) {
        maxCount = count;
        mostCommonCategory = { category: category, count: count };
      }
    }

    return mostCommonCategory;
  }

  // Utility methods
  generateTransactionId() {
    return `txn_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  log(message, error = null) {
    if (this.options.enableDetailedLogging) {
      const timestamp = new Date().toISOString();
      if (error) {
        console.log(`[${timestamp}] ${message}`, error);
      } else {
        console.log(`[${timestamp}] ${message}`);
      }
    }
  }
}

// Example usage with comprehensive error handling
async function demonstrateTransactionErrorHandling() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();

  const transactionManager = new MongoTransactionManager(client, {
    maxRetryAttempts: 3,
    initialRetryDelayMs: 100,
    maxRetryDelayMs: 5000,
    enableDetailedLogging: true,
    enableMetricsCollection: true
  });

  try {
    // Example transaction with comprehensive error handling
    const result = await transactionManager.executeTransactionWithRetry(
      async (session, context) => {
        const { transactionId, attempt } = context;

        console.log(`Executing business logic for transaction ${transactionId}, attempt ${attempt}`);

        const db = client.db('ecommerce');
        const ordersCollection = db.collection('orders');
        const inventoryCollection = db.collection('inventory');
        const accountsCollection = db.collection('accounts');

        // Track operations for monitoring
        context.onOperation({ type: 'insert', collection: 'orders' });
        context.onOperation({ type: 'update', collection: 'inventory' });
        context.onOperation({ type: 'update', collection: 'accounts' });

        // Complex business transaction
        const order = {
          orderId: `order_${Date.now()}`,
          customerId: 'customer_123',
          items: [
            { productId: 'prod_456', quantity: 2, price: 29.99 },
            { productId: 'prod_789', quantity: 1, price: 49.99 }
          ],
          totalAmount: 109.97,
          status: 'pending',
          createdAt: new Date()
        };

        // Insert order
        const orderResult = await ordersCollection.insertOne(order, { session });

        // Update inventory
        for (const item of order.items) {
          const inventoryUpdate = await inventoryCollection.updateOne(
            { productId: item.productId, quantity: { $gte: item.quantity } },
            { $inc: { quantity: -item.quantity } },
            { session }
          );

          if (inventoryUpdate.modifiedCount === 0) {
            throw new Error(`Insufficient inventory for product ${item.productId}`);
          }
        }

        // Update customer account
        await accountsCollection.updateOne(
          { customerId: order.customerId },
          { 
            $inc: { totalOrders: 1, totalSpent: order.totalAmount },
            $set: { lastOrderDate: new Date() }
          },
          { session }
        );

        return {
          orderId: order.orderId,
          orderResult: orderResult,
          message: 'Order processed successfully'
        };
      },
      {
        // Custom transaction options
        maxCommitTimeMS: 15000,
        readConcern: { level: 'snapshot' },
        writeConcern: { w: 'majority', j: true }
      }
    );

    if (result.success) {
      console.log('Transaction completed successfully:', result);
    } else {
      console.error('Transaction failed after all retries:', result);
    }

    // Get comprehensive health report
    const healthReport = transactionManager.getTransactionHealthReport();
    console.log('Transaction Health Report:', JSON.stringify(healthReport, null, 2));

  } catch (error) {
    console.error('Unexpected error:', error);
  } finally {
    await client.close();
  }
}

// Benefits of MongoDB intelligent transaction error handling:
// - Automatic retry logic with exponential backoff and jitter
// - Intelligent error classification and recovery recommendations
// - Comprehensive transaction state tracking and monitoring
// - Advanced performance metrics and health reporting
// - Context-aware error analysis and recovery strategies
// - Built-in support for MongoDB-specific error patterns
// - Detailed logging and diagnostic information
// - Integration with MongoDB driver optimization features
// - Automatic detection of retryable vs. non-retryable errors
// - Production-ready resilience and reliability patterns

Advanced Error Recovery Patterns

Sophisticated recovery strategies for production-grade MongoDB applications:

// Advanced MongoDB error recovery patterns for enterprise resilience
class MongoResilienceManager {
  constructor(client, options = {}) {
    this.client = client;
    this.transactionManager = new MongoTransactionManager(client, options);

    this.recoveryStrategies = new Map();
    this.circuitBreakers = new Map();
    this.healthCheckers = new Map();

    this.options = {
      // Circuit breaker configuration
      circuitBreakerThreshold: options.circuitBreakerThreshold || 5,
      circuitBreakerTimeout: options.circuitBreakerTimeout || 60000,
      circuitBreakerVolumeThreshold: options.circuitBreakerVolumeThreshold || 10,

      // Health check configuration
      healthCheckInterval: options.healthCheckInterval || 30000,
      healthCheckTimeout: options.healthCheckTimeout || 5000,

      // Recovery configuration
      enableAutomaticRecovery: options.enableAutomaticRecovery || true,
      maxRecoveryAttempts: options.maxRecoveryAttempts || 3
    };

    this.initialize();
  }

  initialize() {
    // Set up circuit breakers for different operation types
    this.setupCircuitBreakers();

    // Initialize health monitoring
    this.startHealthMonitoring();

    // Register recovery strategies
    this.registerRecoveryStrategies();
  }

  setupCircuitBreakers() {
    const operationTypes = ['transaction', 'query', 'update', 'insert', 'delete'];

    operationTypes.forEach(opType => {
      this.circuitBreakers.set(opType, {
        state: 'closed', // closed, open, half-open
        failureCount: 0,
        lastFailureTime: null,
        successCount: 0,
        totalRequests: 0,
        threshold: this.options.circuitBreakerThreshold,
        timeout: this.options.circuitBreakerTimeout,
        volumeThreshold: this.options.circuitBreakerVolumeThreshold
      });
    });
  }

  // Execute operation with circuit breaker protection
  async executeWithCircuitBreaker(operationType, operation) {
    const circuitBreaker = this.circuitBreakers.get(operationType);

    if (!circuitBreaker) {
      throw new Error(`No circuit breaker configured for operation type: ${operationType}`);
    }

    // Check circuit breaker state
    const canExecute = this.checkCircuitBreaker(circuitBreaker);

    if (!canExecute) {
      throw new Error(`Circuit breaker is OPEN for ${operationType}. Service temporarily unavailable.`);
    }

    try {
      // Execute operation
      const result = await operation();

      // Record success
      this.recordCircuitBreakerSuccess(circuitBreaker);

      return result;

    } catch (error) {
      // Record failure
      this.recordCircuitBreakerFailure(circuitBreaker);

      throw error;
    }
  }

  checkCircuitBreaker(circuitBreaker) {
    const now = Date.now();

    switch (circuitBreaker.state) {
      case 'closed':
        return true;

      case 'open':
        // Check if timeout has elapsed
        if (now - circuitBreaker.lastFailureTime >= circuitBreaker.timeout) {
          circuitBreaker.state = 'half-open';
          return true;
        }
        return false;

      case 'half-open':
        return true;

      default:
        return false;
    }
  }

  recordCircuitBreakerSuccess(circuitBreaker) {
    circuitBreaker.successCount++;
    circuitBreaker.totalRequests++;

    if (circuitBreaker.state === 'half-open') {
      // Reset circuit breaker on successful half-open request
      circuitBreaker.state = 'closed';
      circuitBreaker.failureCount = 0;
    }
  }

  recordCircuitBreakerFailure(circuitBreaker) {
    circuitBreaker.failureCount++;
    circuitBreaker.totalRequests++;
    circuitBreaker.lastFailureTime = Date.now();

    // Check if should open circuit
    if (circuitBreaker.totalRequests >= circuitBreaker.volumeThreshold &&
        circuitBreaker.failureCount >= circuitBreaker.threshold) {
      circuitBreaker.state = 'open';
      console.log(`Circuit breaker opened due to ${circuitBreaker.failureCount} failures`);
    }
  }

  // Comprehensive transaction execution with full resilience features
  async executeResilientTransaction(transactionFunction, options = {}) {
    const operationType = 'transaction';

    return await this.executeWithCircuitBreaker(operationType, async () => {
      // Execute transaction with comprehensive error handling
      const result = await this.transactionManager.executeTransactionWithRetry(
        transactionFunction,
        options
      );

      // If transaction failed, attempt recovery if enabled
      if (!result.success && this.options.enableAutomaticRecovery) {
        const recoveryResult = await this.attemptTransactionRecovery(result);
        if (recoveryResult && recoveryResult.success) {
          return recoveryResult;
        }
      }

      return result;
    });
  }

  // Intelligent transaction recovery based on error patterns
  async attemptTransactionRecovery(failedResult) {
    const { error, transactionId, attempts, errorAnalysis } = failedResult;

    console.log(`Attempting recovery for failed transaction ${transactionId}`);

    // Get appropriate recovery strategy
    const recoveryStrategy = this.getRecoveryStrategy(errorAnalysis);

    if (!recoveryStrategy) {
      console.log(`No recovery strategy available for error category: ${errorAnalysis.category}`);
      return null;
    }

    try {
      const recoveryResult = await recoveryStrategy.execute(failedResult);

      console.log(`Recovery attempt completed for transaction ${transactionId}:`, recoveryResult);

      return recoveryResult;

    } catch (recoveryError) {
      console.error(`Recovery failed for transaction ${transactionId}:`, recoveryError);
      return null;
    }
  }

  registerRecoveryStrategies() {
    // Network connectivity recovery
    this.recoveryStrategies.set('network', {
      execute: async (failedResult) => {
        console.log('Executing network recovery strategy');

        // Wait for network to recover
        await this.waitForNetworkRecovery();

        // Check server connectivity
        const healthOk = await this.performHealthCheck();

        if (healthOk) {
          console.log('Network recovery successful, retrying transaction');
          // Could retry the transaction here if the original function is available
          return { success: true, recovered: true, strategy: 'network' };
        }

        return { success: false, recovered: false, strategy: 'network' };
      }
    });

    // Resource recovery
    this.recoveryStrategies.set('resource', {
      execute: async (failedResult) => {
        console.log('Executing resource recovery strategy');

        // Wait for resources to become available
        await this.waitForResourceAvailability();

        // Check resource status
        const resourcesOk = await this.checkResourceStatus();

        if (resourcesOk) {
          console.log('Resource recovery successful');
          return { success: true, recovered: true, strategy: 'resource' };
        }

        return { success: false, recovered: false, strategy: 'resource' };
      }
    });

    // Availability recovery (replica set issues)
    this.recoveryStrategies.set('availability', {
      execute: async (failedResult) => {
        console.log('Executing availability recovery strategy');

        // Check replica set status
        const replicaSetOk = await this.checkReplicaSetHealth();

        if (replicaSetOk) {
          console.log('Availability recovery successful');
          return { success: true, recovered: true, strategy: 'availability' };
        }

        // Wait for replica set to recover
        await this.waitForReplicaSetRecovery();

        const recoveredReplicaSetOk = await this.checkReplicaSetHealth();

        return {
          success: recoveredReplicaSetOk,
          recovered: recoveredReplicaSetOk,
          strategy: 'availability'
        };
      }
    });
  }

  getRecoveryStrategy(errorAnalysis) {
    return this.recoveryStrategies.get(errorAnalysis.category);
  }

  // Health monitoring and recovery assistance
  startHealthMonitoring() {
    setInterval(async () => {
      try {
        await this.performComprehensiveHealthCheck();
      } catch (error) {
        console.error('Health monitoring error:', error);
      }
    }, this.options.healthCheckInterval);
  }

  async performComprehensiveHealthCheck() {
    const healthStatus = {
      timestamp: new Date(),
      overall: 'unknown',
      components: {}
    };

    try {
      // Check basic connectivity
      healthStatus.components.connectivity = await this.checkConnectivity();

      // Check replica set status
      healthStatus.components.replicaSet = await this.checkReplicaSetHealth();

      // Check resource status
      healthStatus.components.resources = await this.checkResourceStatus();

      // Check circuit breaker status
      healthStatus.components.circuitBreakers = this.getCircuitBreakerStatus();

      // Check transaction manager health
      healthStatus.components.transactionManager = this.transactionManager.getTransactionHealthReport();

      // Determine overall health
      const componentStatuses = Object.values(healthStatus.components);
      const healthyComponents = componentStatuses.filter(status => 
        status === true || (typeof status === 'object' && status.healthy !== false)
      );

      if (healthyComponents.length === componentStatuses.length) {
        healthStatus.overall = 'healthy';
      } else if (healthyComponents.length >= componentStatuses.length * 0.7) {
        healthStatus.overall = 'degraded';
      } else {
        healthStatus.overall = 'unhealthy';
      }

      // Store health status
      this.lastHealthStatus = healthStatus;

      return healthStatus;

    } catch (error) {
      healthStatus.overall = 'error';
      healthStatus.error = error.message;
      return healthStatus;
    }
  }

  async checkConnectivity() {
    try {
      const admin = this.client.db('admin');
      await admin.command({ ping: 1 }, { maxTimeMS: this.options.healthCheckTimeout });
      return true;
    } catch (error) {
      return false;
    }
  }

  async checkReplicaSetHealth() {
    try {
      const admin = this.client.db('admin');
      const status = await admin.command({ replSetGetStatus: 1 });

      // Check if majority of members are healthy
      const healthyMembers = status.members.filter(member => 
        member.health === 1 && ['PRIMARY', 'SECONDARY'].includes(member.stateStr)
      );

      return {
        healthy: healthyMembers.length >= Math.ceil(status.members.length / 2),
        totalMembers: status.members.length,
        healthyMembers: healthyMembers.length,
        primaryAvailable: status.members.some(m => m.stateStr === 'PRIMARY')
      };

    } catch (error) {
      // Might not be a replica set or insufficient privileges
      return { healthy: true, note: 'Replica set status unavailable' };
    }
  }

  async checkResourceStatus() {
    try {
      const admin = this.client.db('admin');
      const serverStatus = await admin.command({ serverStatus: 1 });

      const memUsage = serverStatus.mem.resident / serverStatus.mem.virtual;
      const connectionUsage = serverStatus.connections.current / serverStatus.connections.available;

      return {
        healthy: memUsage < 0.9 && connectionUsage < 0.9,
        memoryUsage: memUsage,
        connectionUsage: connectionUsage,
        connections: serverStatus.connections,
        memory: serverStatus.mem
      };

    } catch (error) {
      return { healthy: false, error: error.message };
    }
  }

  getCircuitBreakerStatus() {
    const status = {};

    for (const [opType, breaker] of this.circuitBreakers.entries()) {
      status[opType] = {
        state: breaker.state,
        failureCount: breaker.failureCount,
        successCount: breaker.successCount,
        totalRequests: breaker.totalRequests,
        failureRate: breaker.totalRequests > 0 ? 
          (breaker.failureCount / breaker.totalRequests) * 100 : 0
      };
    }

    return status;
  }

  // Recovery assistance methods
  async waitForNetworkRecovery() {
    const maxWaitTime = 30000; // 30 seconds
    const checkInterval = 1000;  // 1 second
    let waited = 0;

    while (waited < maxWaitTime) {
      try {
        const connected = await this.checkConnectivity();
        if (connected) {
          return true;
        }
      } catch (error) {
        // Continue waiting
      }

      await this.sleep(checkInterval);
      waited += checkInterval;
    }

    return false;
  }

  async waitForResourceAvailability() {
    const maxWaitTime = 60000; // 60 seconds
    const checkInterval = 5000;  // 5 seconds
    let waited = 0;

    while (waited < maxWaitTime) {
      try {
        const resourceStatus = await this.checkResourceStatus();
        if (resourceStatus.healthy) {
          return true;
        }
      } catch (error) {
        // Continue waiting
      }

      await this.sleep(checkInterval);
      waited += checkInterval;
    }

    return false;
  }

  async waitForReplicaSetRecovery() {
    const maxWaitTime = 120000; // 2 minutes
    const checkInterval = 10000;  // 10 seconds
    let waited = 0;

    while (waited < maxWaitTime) {
      try {
        const replicaStatus = await this.checkReplicaSetHealth();
        if (replicaStatus.healthy) {
          return true;
        }
      } catch (error) {
        // Continue waiting
      }

      await this.sleep(checkInterval);
      waited += checkInterval;
    }

    return false;
  }

  async performHealthCheck() {
    const health = await this.performComprehensiveHealthCheck();
    return health.overall === 'healthy' || health.overall === 'degraded';
  }

  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  // Get comprehensive resilience report
  getResilienceReport() {
    return {
      timestamp: new Date(),
      circuitBreakers: this.getCircuitBreakerStatus(),
      transactionHealth: this.transactionManager.getTransactionHealthReport(),
      lastHealthCheck: this.lastHealthStatus,
      recoveryStrategies: Array.from(this.recoveryStrategies.keys()),
      configuration: {
        circuitBreakerThreshold: this.options.circuitBreakerThreshold,
        circuitBreakerTimeout: this.options.circuitBreakerTimeout,
        healthCheckInterval: this.options.healthCheckInterval,
        automaticRecoveryEnabled: this.options.enableAutomaticRecovery
      }
    };
  }
}

SQL-Style Error Handling with QueryLeaf

QueryLeaf provides familiar approaches to MongoDB transaction error handling and monitoring:

-- QueryLeaf transaction error handling with SQL-familiar syntax

-- Monitor transaction error patterns
SELECT 
  DATE_TRUNC('hour', error_timestamp) as hour_bucket,
  error_category,
  error_code,

  -- Error statistics
  COUNT(*) as error_count,
  COUNT(DISTINCT transaction_id) as affected_transactions,
  AVG(retry_attempts) as avg_retry_attempts,
  COUNT(CASE WHEN recovery_successful = true THEN 1 END) as successful_recoveries,

  -- Performance impact
  AVG(transaction_duration_ms) as avg_failed_transaction_duration,
  AVG(time_to_failure_ms) as avg_time_to_failure,

  -- Recovery metrics
  AVG(recovery_time_ms) as avg_recovery_time,
  MAX(recovery_time_ms) as max_recovery_time,

  -- Success rates
  ROUND((COUNT(CASE WHEN recovery_successful = true THEN 1 END)::DECIMAL / COUNT(*)) * 100, 2) as recovery_success_rate

FROM TRANSACTION_ERROR_LOG()
WHERE error_timestamp >= NOW() - INTERVAL '24 hours'
GROUP BY DATE_TRUNC('hour', error_timestamp), error_category, error_code
ORDER BY hour_bucket DESC, error_count DESC;

-- Analyze transaction resilience patterns
WITH transaction_resilience AS (
  SELECT 
    transaction_id,
    transaction_type,

    -- Transaction characteristics
    operation_count,
    total_duration_ms,
    retry_attempts,

    -- Error analysis
    first_error_code,
    first_error_category,
    total_errors,

    -- Recovery analysis
    recovery_strategy_used,
    recovery_successful,
    recovery_duration_ms,

    -- Final outcome
    final_status, -- committed, failed, recovered

    -- Timing analysis
    created_at,
    completed_at

  FROM TRANSACTION_HISTORY()
  WHERE created_at >= NOW() - INTERVAL '7 days'
),

resilience_patterns AS (
  SELECT 
    transaction_type,
    first_error_category,

    -- Volume metrics
    COUNT(*) as transaction_count,
    COUNT(CASE WHEN final_status = 'committed' THEN 1 END) as successful_transactions,
    COUNT(CASE WHEN final_status = 'recovered' THEN 1 END) as recovered_transactions,
    COUNT(CASE WHEN final_status = 'failed' THEN 1 END) as failed_transactions,

    -- Retry analysis
    AVG(retry_attempts) as avg_retry_attempts,
    MAX(retry_attempts) as max_retry_attempts,
    COUNT(CASE WHEN retry_attempts > 0 THEN 1 END) as transactions_with_retries,

    -- Recovery analysis
    COUNT(CASE WHEN recovery_strategy_used IS NOT NULL THEN 1 END) as recovery_attempts,
    COUNT(CASE WHEN recovery_successful = true THEN 1 END) as successful_recoveries,
    AVG(recovery_duration_ms) as avg_recovery_duration,

    -- Performance metrics
    AVG(total_duration_ms) as avg_total_duration,
    PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY total_duration_ms) as p95_duration,

    -- Success rates
    ROUND((COUNT(CASE WHEN final_status IN ('committed', 'recovered') THEN 1 END)::DECIMAL / COUNT(*)) * 100, 2) as overall_success_rate,
    ROUND((COUNT(CASE WHEN recovery_successful = true THEN 1 END)::DECIMAL / 
           GREATEST(COUNT(CASE WHEN recovery_strategy_used IS NOT NULL THEN 1 END), 1)) * 100, 2) as recovery_success_rate

  FROM transaction_resilience
  GROUP BY transaction_type, first_error_category
)

SELECT 
  transaction_type,
  first_error_category,

  -- Volume and success metrics
  transaction_count,
  successful_transactions,
  recovered_transactions,
  failed_transactions,
  overall_success_rate,

  -- Retry patterns
  avg_retry_attempts,
  max_retry_attempts,
  ROUND((transactions_with_retries::DECIMAL / transaction_count) * 100, 2) as retry_rate_percent,

  -- Recovery effectiveness
  recovery_attempts,
  successful_recoveries,
  recovery_success_rate,
  avg_recovery_duration,

  -- Performance characteristics
  avg_total_duration,
  p95_duration,

  -- Health assessment
  CASE 
    WHEN overall_success_rate >= 99 THEN 'Excellent'
    WHEN overall_success_rate >= 95 THEN 'Good' 
    WHEN overall_success_rate >= 90 THEN 'Fair'
    ELSE 'Poor'
  END as resilience_grade,

  -- Recommendations
  CASE 
    WHEN recovery_success_rate < 50 AND recovery_attempts > 0 THEN 'Improve recovery strategies'
    WHEN avg_retry_attempts > 3 THEN 'Review retry configuration'
    WHEN failed_transactions > successful_transactions * 0.1 THEN 'Investigate error root causes'
    ELSE 'Performance acceptable'
  END as recommendation

FROM resilience_patterns
ORDER BY transaction_count DESC, overall_success_rate ASC;

-- Real-time transaction health monitoring
SELECT 
  -- Current status
  COUNT(CASE WHEN status = 'active' THEN 1 END) as active_transactions,
  COUNT(CASE WHEN status = 'retrying' THEN 1 END) as retrying_transactions,
  COUNT(CASE WHEN status = 'recovering' THEN 1 END) as recovering_transactions,
  COUNT(CASE WHEN status = 'failed' THEN 1 END) as failed_transactions,

  -- Recent performance (last 5 minutes)
  AVG(CASE WHEN completed_at >= NOW() - INTERVAL '5 minutes' 
           THEN duration_ms END) as recent_avg_duration_ms,
  COUNT(CASE WHEN completed_at >= NOW() - INTERVAL '5 minutes' 
             AND final_status = 'committed' THEN 1 END) as recent_successful_transactions,
  COUNT(CASE WHEN completed_at >= NOW() - INTERVAL '5 minutes' 
             AND final_status = 'failed' THEN 1 END) as recent_failed_transactions,

  -- Error rates
  ROUND((COUNT(CASE WHEN error_occurred_at >= NOW() - INTERVAL '5 minutes' THEN 1 END)::DECIMAL /
         GREATEST(COUNT(CASE WHEN created_at >= NOW() - INTERVAL '5 minutes' THEN 1 END), 1)) * 100, 2) 
         as recent_error_rate_percent,

  -- Circuit breaker status
  COUNT(CASE WHEN circuit_breaker_state = 'open' THEN 1 END) as open_circuit_breakers,
  COUNT(CASE WHEN circuit_breaker_state = 'half-open' THEN 1 END) as half_open_circuit_breakers,

  -- Recovery metrics
  COUNT(CASE WHEN recovery_in_progress = true THEN 1 END) as active_recoveries,
  AVG(CASE WHEN recovery_completed_at >= NOW() - INTERVAL '5 minutes' 
           THEN recovery_duration_ms END) as recent_avg_recovery_time_ms,

  -- Health indicators
  CASE 
    WHEN COUNT(CASE WHEN status = 'failed' THEN 1 END) > 
         COUNT(CASE WHEN status = 'active' THEN 1 END) * 0.5 THEN 'Critical'
    WHEN COUNT(CASE WHEN circuit_breaker_state = 'open' THEN 1 END) > 0 THEN 'Degraded'
    WHEN COUNT(CASE WHEN status = 'retrying' THEN 1 END) > 
         COUNT(CASE WHEN status = 'active' THEN 1 END) * 0.3 THEN 'Warning'
    ELSE 'Healthy'
  END as overall_health_status,

  NOW() as report_timestamp

FROM ACTIVE_TRANSACTION_STATUS()
CROSS JOIN CIRCUIT_BREAKER_STATUS()
CROSS JOIN RECOVERY_STATUS();

-- Transaction error prevention and optimization
CREATE ALERT TRANSACTION_ERROR_PREVENTION
ON TRANSACTION_ERROR_LOG()
WHEN (
  -- High error rate
  (SELECT COUNT(*) FROM TRANSACTION_ERROR_LOG() 
   WHERE error_timestamp >= NOW() - INTERVAL '5 minutes') > 10
  OR
  -- Circuit breaker opened
  (SELECT COUNT(*) FROM CIRCUIT_BREAKER_STATUS() 
   WHERE state = 'open') > 0
  OR
  -- Recovery failing
  (SELECT AVG(CASE WHEN recovery_successful = true THEN 1.0 ELSE 0.0 END) 
   FROM TRANSACTION_ERROR_LOG() 
   WHERE error_timestamp >= NOW() - INTERVAL '15 minutes' 
   AND recovery_strategy_used IS NOT NULL) < 0.5
)
NOTIFY ['[email protected]', '[email protected]']
WITH MESSAGE TEMPLATE '''
{% raw %}
Transaction Error Alert

Current Status:
- Recent Errors (5 min): {{ recent_error_count }}
- Open Circuit Breakers: {{ open_circuit_breaker_count }}
- Active Recoveries: {{ active_recovery_count }}
- Recovery Success Rate: {{ recovery_success_rate }}%

Top Error Categories:
{{ top_error_categories }}

Recommended Actions:
{{ error_prevention_recommendations }}

Dashboard: https://monitoring.company.com/mongodb/transactions
{% endraw %}
'''
EVERY 1 MINUTES;

-- QueryLeaf transaction error handling provides:
-- 1. SQL-familiar error monitoring and analysis
-- 2. Comprehensive transaction resilience reporting
-- 3. Real-time health monitoring and alerting
-- 4. Intelligent error pattern detection
-- 5. Recovery strategy effectiveness analysis
-- 6. Circuit breaker status monitoring
-- 7. Performance impact assessment
-- 8. Automated prevention and optimization recommendations
-- 9. Integration with MongoDB's native error handling
-- 10. Production-ready operational visibility

Best Practices for MongoDB Transaction Error Handling

Error Classification Strategy

Optimal error handling configuration for different application patterns:

  1. High-Frequency Applications: Aggressive retry policies with intelligent backoff
  2. Mission-Critical Systems: Comprehensive recovery strategies with circuit breakers
  3. Batch Processing: Extended timeout configurations with resource monitoring
  4. Real-time Applications: Fast-fail approaches with immediate fallback mechanisms
  5. Microservices: Distributed error handling with service-level circuit breakers
  6. Analytics Workloads: Specialized error handling for long-running operations

Recovery Strategy Guidelines

Essential patterns for production transaction recovery:

  1. Automatic Retry Logic: Exponential backoff with jitter for transient failures
  2. Circuit Breaker Pattern: Prevent cascading failures with intelligent state management
  3. Health Monitoring: Continuous assessment of system and transaction health
  4. Recovery Automation: Context-aware recovery strategies for different error types
  5. Performance Monitoring: Track error impact on application performance
  6. Operational Alerting: Proactive notification of error patterns and recovery issues

Conclusion

MongoDB transaction error handling and recovery requires sophisticated strategies that balance reliability, performance, and operational complexity. By implementing intelligent retry mechanisms, comprehensive error classification, and automated recovery patterns, applications can maintain consistency and reliability even when facing distributed system challenges.

Key error handling benefits include:

  • Intelligent Recovery: Automatic retry logic with context-aware recovery strategies
  • Comprehensive Monitoring: Detailed error tracking and performance analysis
  • Circuit Breaker Protection: Prevention of cascading failures with intelligent state management
  • Health Assessment: Continuous monitoring of transaction and system health
  • Operational Visibility: Real-time insights into error patterns and recovery effectiveness
  • Production Resilience: Enterprise-grade reliability patterns for mission-critical applications

Whether you're building high-throughput web applications, distributed microservices, data processing pipelines, or real-time analytics platforms, MongoDB's intelligent transaction error handling with QueryLeaf's familiar management interface provides the foundation for resilient, reliable database operations. This combination enables you to leverage advanced error recovery capabilities while maintaining familiar database administration patterns and operational procedures.

QueryLeaf Integration: QueryLeaf automatically translates SQL-familiar error handling patterns into optimal MongoDB transaction configurations while providing comprehensive monitoring and recovery through SQL-style queries. Advanced error classification, recovery automation, and performance analysis are seamlessly managed through familiar database administration interfaces, making sophisticated error handling both powerful and accessible.

The integration of intelligent error handling with SQL-style database operations makes MongoDB an ideal platform for applications requiring both high reliability and familiar error management patterns, ensuring your transactions remain both consistent and resilient as they scale to meet demanding production requirements.

MongoDB Aggregation Framework for Real-Time Analytics: Advanced Data Processing Pipelines and SQL-Compatible Query Patterns

Modern applications require sophisticated data processing capabilities that can handle complex analytical queries, real-time aggregations, and advanced transformations at scale. Traditional approaches to data analytics often rely on separate ETL processes, batch processing systems, and complex data warehouses that introduce latency, complexity, and operational overhead that becomes increasingly problematic as data volumes and processing demands grow.

MongoDB's Aggregation Framework provides powerful in-database processing capabilities that enable real-time analytics, complex data transformations, and sophisticated analytical queries directly within the operational database. Unlike traditional batch-oriented analytics approaches, MongoDB aggregation pipelines process data in real-time, support complex multi-stage transformations, and integrate seamlessly with operational workloads while delivering high-performance analytical results.

The Traditional Data Analytics Limitations

Conventional relational database analytics approaches have significant constraints for modern real-time processing requirements:

-- Traditional PostgreSQL analytics - limited window functions and complex subqueries

-- Basic sales analytics with traditional SQL limitations
WITH monthly_sales_summary AS (
  SELECT 
    DATE_TRUNC('month', order_date) as month,
    product_category,
    customer_id,
    salesperson_id,
    region,

    -- Basic aggregations
    COUNT(*) as order_count,
    SUM(total_amount) as total_revenue,
    AVG(total_amount) as avg_order_value,
    MIN(total_amount) as min_order_value,
    MAX(total_amount) as max_order_value,

    -- Limited window function capabilities
    SUM(total_amount) OVER (
      PARTITION BY product_category, region 
      ORDER BY DATE_TRUNC('month', order_date)
      RANGE BETWEEN INTERVAL '3 months' PRECEDING AND CURRENT ROW
    ) as rolling_3_month_revenue,

    LAG(SUM(total_amount)) OVER (
      PARTITION BY product_category, region 
      ORDER BY DATE_TRUNC('month', order_date)
    ) as previous_month_revenue,

    -- Row number for ranking (limited functionality)
    ROW_NUMBER() OVER (
      PARTITION BY DATE_TRUNC('month', order_date), region
      ORDER BY SUM(total_amount) DESC
    ) as revenue_rank_in_region

  FROM orders o
  LEFT JOIN order_items oi ON o.order_id = oi.order_id
  LEFT JOIN products p ON oi.product_id = p.product_id
  LEFT JOIN customers c ON o.customer_id = c.customer_id
  LEFT JOIN salespeople s ON o.salesperson_id = s.salesperson_id
  WHERE o.order_date >= CURRENT_DATE - INTERVAL '12 months'
    AND o.status = 'completed'
  GROUP BY 
    DATE_TRUNC('month', order_date), 
    product_category, 
    customer_id, 
    salesperson_id, 
    region
),

customer_segmentation AS (
  SELECT 
    customer_id,
    region,

    -- Customer metrics calculation
    COUNT(*) as total_orders,
    SUM(total_revenue) as lifetime_revenue,
    AVG(avg_order_value) as avg_order_value,
    MAX(month) as last_order_month,
    MIN(month) as first_order_month,

    -- Recency, Frequency, Monetary calculation (limited)
    EXTRACT(DAYS FROM (CURRENT_DATE - MAX(month))) as days_since_last_order,
    COUNT(*) as frequency_score,
    SUM(total_revenue) as monetary_score,

    -- Simple percentile calculation (limited support)
    PERCENT_RANK() OVER (ORDER BY SUM(total_revenue)) as revenue_percentile,
    PERCENT_RANK() OVER (ORDER BY COUNT(*)) as frequency_percentile,

    -- Basic customer categorization
    CASE 
      WHEN SUM(total_revenue) > 10000 AND COUNT(*) > 10 THEN 'high_value'
      WHEN SUM(total_revenue) > 5000 OR COUNT(*) > 5 THEN 'medium_value'
      WHEN EXTRACT(DAYS FROM (CURRENT_DATE - MAX(month))) > 90 THEN 'at_risk'
      ELSE 'low_value'
    END as customer_segment,

    -- Growth trend analysis (very limited)
    CASE 
      WHEN COUNT(*) FILTER (WHERE month >= CURRENT_DATE - INTERVAL '3 months') > 0 THEN 'active'
      WHEN COUNT(*) FILTER (WHERE month >= CURRENT_DATE - INTERVAL '6 months') > 0 THEN 'declining'
      ELSE 'inactive'
    END as activity_trend

  FROM monthly_sales_summary
  GROUP BY customer_id, region
),

product_performance AS (
  SELECT 
    product_category,
    region,
    month,

    -- Product metrics
    SUM(order_count) as total_orders,
    SUM(total_revenue) as category_revenue,
    AVG(avg_order_value) as avg_category_order_value,
    COUNT(DISTINCT customer_id) as unique_customers,

    -- Market share calculation (complex with traditional SQL)
    SUM(total_revenue) / (
      SELECT SUM(total_revenue) 
      FROM monthly_sales_summary mss2 
      WHERE mss2.month = monthly_sales_summary.month 
        AND mss2.region = monthly_sales_summary.region
    ) * 100 as market_share_percent,

    -- Growth rate calculation
    SUM(total_revenue) / NULLIF(LAG(SUM(total_revenue)) OVER (
      PARTITION BY product_category, region 
      ORDER BY month
    ), 0) - 1 as month_over_month_growth,

    -- Seasonal analysis (limited capabilities)
    AVG(SUM(total_revenue)) OVER (
      PARTITION BY product_category, region, EXTRACT(MONTH FROM month)
      ORDER BY month
      ROWS BETWEEN 11 PRECEDING AND CURRENT ROW
    ) as seasonal_avg_revenue

  FROM monthly_sales_summary
  GROUP BY product_category, region, month
),

advanced_analytics AS (
  SELECT 
    cs.customer_segment,
    cs.region,
    cs.activity_trend,

    -- Customer segment analysis
    COUNT(*) as customers_in_segment,
    AVG(cs.lifetime_revenue) as avg_lifetime_value,
    AVG(cs.total_orders) as avg_orders_per_customer,
    AVG(cs.days_since_last_order) as avg_days_since_last_order,

    -- Revenue contribution by segment
    SUM(cs.lifetime_revenue) as segment_total_revenue,
    SUM(cs.lifetime_revenue) / (
      SELECT SUM(lifetime_revenue) FROM customer_segmentation
    ) * 100 as revenue_contribution_percent,

    -- Top products for each segment (limited subquery approach)
    (
      SELECT product_category 
      FROM monthly_sales_summary mss
      WHERE mss.customer_id IN (
        SELECT cs2.customer_id 
        FROM customer_segmentation cs2 
        WHERE cs2.customer_segment = cs.customer_segment
          AND cs2.region = cs.region
      )
      GROUP BY product_category
      ORDER BY SUM(total_revenue) DESC
      LIMIT 1
    ) as top_product_category,

    -- Cohort analysis (very complex with traditional SQL)
    COUNT(*) FILTER (
      WHERE cs.first_order_month >= CURRENT_DATE - INTERVAL '1 month'
    ) as new_customers_this_month,

    COUNT(*) FILTER (
      WHERE cs.last_order_month >= CURRENT_DATE - INTERVAL '1 month'
        AND cs.first_order_month < CURRENT_DATE - INTERVAL '1 month'
    ) as returning_customers_this_month

  FROM customer_segmentation cs
  GROUP BY cs.customer_segment, cs.region, cs.activity_trend
)

SELECT 
  customer_segment,
  region,
  activity_trend,
  customers_in_segment,
  ROUND(avg_lifetime_value::numeric, 2) as avg_lifetime_value,
  ROUND(avg_orders_per_customer::numeric, 2) as avg_orders_per_customer,
  ROUND(avg_days_since_last_order::numeric, 1) as avg_days_since_last_order,
  ROUND(segment_total_revenue::numeric, 2) as segment_revenue,
  ROUND(revenue_contribution_percent::numeric, 2) as revenue_contribution_pct,
  top_product_category,
  new_customers_this_month,
  returning_customers_this_month,

  -- Customer health score (simplified)
  CASE 
    WHEN customer_segment = 'high_value' AND activity_trend = 'active' THEN 95
    WHEN customer_segment = 'high_value' AND activity_trend = 'declining' THEN 70
    WHEN customer_segment = 'medium_value' AND activity_trend = 'active' THEN 80
    WHEN customer_segment = 'medium_value' AND activity_trend = 'declining' THEN 55
    WHEN customer_segment = 'low_value' AND activity_trend = 'active' THEN 65
    WHEN activity_trend = 'inactive' THEN 25
    ELSE 40
  END as customer_health_score,

  -- Recommendations (limited business logic)
  CASE 
    WHEN customer_segment = 'high_value' AND activity_trend = 'declining' THEN 'Urgent: Re-engagement campaign needed'
    WHEN customer_segment = 'medium_value' AND activity_trend = 'active' THEN 'Opportunity: Upsell to premium products'
    WHEN customer_segment = 'at_risk' THEN 'Action: Retention campaign required'
    WHEN new_customers_this_month > returning_customers_this_month THEN 'Focus: Improve customer retention'
    ELSE 'Monitor: Continue current strategy'
  END as recommended_action

FROM advanced_analytics
ORDER BY 
  CASE customer_segment 
    WHEN 'high_value' THEN 1 
    WHEN 'medium_value' THEN 2 
    WHEN 'low_value' THEN 3 
    ELSE 4 
  END,
  segment_revenue DESC;

-- Traditional PostgreSQL analytics problems:
-- 1. Complex multi-table JOINs required for comprehensive analysis
-- 2. Limited window function capabilities for advanced analytics
-- 3. Difficult to implement complex transformations and nested aggregations
-- 4. Poor performance with large datasets and complex calculations
-- 5. Limited support for hierarchical and nested data structures
-- 6. No built-in support for time-series analytics and forecasting
-- 7. Complex subqueries required for conditional aggregations
-- 8. Difficult to implement real-time analytics and streaming calculations
-- 9. Limited flexibility for dynamic grouping and pivot operations
-- 10. No native support for advanced statistical functions and machine learning

-- MySQL limitations are even more severe
SELECT 
  DATE_FORMAT(order_date, '%Y-%m') as month,
  product_category,
  region,
  COUNT(*) as order_count,
  SUM(total_amount) as revenue,
  AVG(total_amount) as avg_order_value,

  -- Very limited analytical capabilities
  -- No window functions in older MySQL versions
  -- No complex aggregation support
  -- Limited JSON processing capabilities
  -- Poor performance with complex queries

  (SELECT SUM(total_amount) 
   FROM orders o2 
   WHERE DATE_FORMAT(o2.order_date, '%Y-%m') = DATE_FORMAT(orders.order_date, '%Y-%m')
     AND o2.region = orders.region) as region_monthly_total

FROM orders
JOIN order_items ON orders.order_id = order_items.order_id
JOIN products ON order_items.product_id = products.product_id
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
  AND status = 'completed'
GROUP BY 
  DATE_FORMAT(order_date, '%Y-%m'), 
  product_category, 
  region
ORDER BY month DESC, revenue DESC;

-- MySQL problems:
-- - No window functions in older versions
-- - Very limited JSON support and processing
-- - Basic aggregation functions only
-- - Poor performance with complex analytical queries
-- - No support for advanced statistical calculations
-- - Limited date/time processing capabilities
-- - No native support for real-time analytics
-- - Basic subquery support with performance issues

MongoDB's Aggregation Framework provides comprehensive real-time analytics capabilities:

// MongoDB Advanced Aggregation Framework - powerful real-time analytics and data processing
const { MongoClient } = require('mongodb');

class MongoDBAnalyticsEngine {
  constructor(db) {
    this.db = db;
    this.collections = {
      orders: db.collection('orders'),
      products: db.collection('products'),
      customers: db.collection('customers'),
      analytics: db.collection('analytics_cache')
    };
    this.pipelineCache = new Map();
  }

  async performComprehensiveAnalytics() {
    console.log('Executing comprehensive real-time analytics with MongoDB Aggregation Framework...');

    // Execute multiple analytical pipelines in parallel
    const [
      salesAnalytics,
      customerSegmentation,
      productPerformance,
      timeSeriesAnalysis,
      predictiveInsights
    ] = await Promise.all([
      this.executeSalesAnalyticsPipeline(),
      this.executeCustomerSegmentationPipeline(),
      this.executeProductPerformancePipeline(),
      this.executeTimeSeriesAnalytics(),
      this.executePredictiveAnalytics()
    ]);

    return {
      salesAnalytics,
      customerSegmentation,
      productPerformance,
      timeSeriesAnalysis,
      predictiveInsights,
      generatedAt: new Date()
    };
  }

  async executeSalesAnalyticsPipeline() {
    console.log('Executing advanced sales analytics pipeline...');

    const pipeline = [
      // Stage 1: Match recent completed orders
      {
        $match: {
          status: 'completed',
          orderDate: { $gte: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000) }, // Last 12 months
          'totals.total': { $gt: 0 }
        }
      },

      // Stage 2: Add computed fields and date transformations
      {
        $addFields: {
          year: { $year: '$orderDate' },
          month: { $month: '$orderDate' },
          dayOfYear: { $dayOfYear: '$orderDate' },
          weekOfYear: { $week: '$orderDate' },
          quarter: { 
            $ceil: { $divide: [{ $month: '$orderDate' }, 3] }
          },

          // Calculate order metrics
          orderValue: '$totals.total',
          itemCount: { $size: '$items' },
          avgItemValue: { 
            $divide: ['$totals.total', { $size: '$items' }] 
          },

          // Customer type classification
          customerType: {
            $switch: {
              branches: [
                { case: { $gte: ['$totals.total', 1000] }, then: 'high_value' },
                { case: { $gte: ['$totals.total', 500] }, then: 'medium_value' },
                { case: { $gte: ['$totals.total', 100] }, then: 'regular' }
              ],
              default: 'low_value'
            }
          },

          // Season classification
          season: {
            $switch: {
              branches: [
                { case: { $in: [{ $month: '$orderDate' }, [12, 1, 2]] }, then: 'winter' },
                { case: { $in: [{ $month: '$orderDate' }, [3, 4, 5]] }, then: 'spring' },
                { case: { $in: [{ $month: '$orderDate' }, [6, 7, 8]] }, then: 'summer' },
                { case: { $in: [{ $month: '$orderDate' }, [9, 10, 11]] }, then: 'fall' }
              ],
              default: 'unknown'
            }
          }
        }
      },

      // Stage 3: Lookup customer information
      {
        $lookup: {
          from: 'customers',
          localField: 'customerId',
          foreignField: '_id',
          as: 'customer',
          pipeline: [
            {
              $project: {
                name: 1,
                email: 1,
                'profile.location.country': 1,
                'profile.location.region': 1,
                'account.type': 1,
                'account.registrationDate': 1,
                'preferences.category': 1
              }
            }
          ]
        }
      },

      // Stage 4: Unwind customer data
      { $unwind: '$customer' },

      // Stage 5: Unwind order items for detailed analysis
      { $unwind: '$items' },

      // Stage 6: Lookup product information
      {
        $lookup: {
          from: 'products',
          localField: 'items.productId',
          foreignField: '_id',
          as: 'product',
          pipeline: [
            {
              $project: {
                name: 1,
                category: 1,
                brand: 1,
                'pricing.cost': 1,
                'specifications.weight': 1,
                'inventory.supplier': 1
              }
            }
          ]
        }
      },

      // Stage 7: Unwind product data
      { $unwind: '$product' },

      // Stage 8: Calculate item-level metrics
      {
        $addFields: {
          itemRevenue: { $multiply: ['$items.quantity', '$items.unitPrice'] },
          itemProfit: { 
            $multiply: [
              '$items.quantity', 
              { $subtract: ['$items.unitPrice', '$product.pricing.cost'] }
            ]
          },
          profitMargin: {
            $divide: [
              { $subtract: ['$items.unitPrice', '$product.pricing.cost'] },
              '$items.unitPrice'
            ]
          }
        }
      },

      // Stage 9: Group by multiple dimensions for comprehensive analysis
      {
        $group: {
          _id: {
            year: '$year',
            month: '$month',
            quarter: '$quarter',
            season: '$season',
            category: '$product.category',
            brand: '$product.brand',
            country: '$customer.profile.location.country',
            region: '$customer.profile.location.region',
            customerType: '$customerType',
            accountType: '$customer.account.type'
          },

          // Order-level metrics
          totalOrders: { $sum: 1 },
          uniqueCustomers: { $addToSet: '$customerId' },
          totalRevenue: { $sum: '$itemRevenue' },
          totalProfit: { $sum: '$itemProfit' },
          totalQuantity: { $sum: '$items.quantity' },

          // Statistical measures
          avgOrderValue: { $avg: '$orderValue' },
          minOrderValue: { $min: '$orderValue' },
          maxOrderValue: { $max: '$orderValue' },
          stdDevOrderValue: { $stdDevPop: '$orderValue' },

          // Product performance
          avgProfitMargin: { $avg: '$profitMargin' },
          avgItemPrice: { $avg: '$items.unitPrice' },
          totalWeight: { $sum: { $multiply: ['$items.quantity', '$product.specifications.weight'] } },

          // Customer insights
          newCustomers: {
            $sum: {
              $cond: [
                { $gte: [
                  '$customer.account.registrationDate',
                  { $dateFromParts: { year: '$year', month: '$month', day: 1 } }
                ]},
                1, 0
              ]
            }
          },

          // Supplier diversity
          uniqueSuppliers: { $addToSet: '$product.inventory.supplier' },

          // Sample orders for detailed analysis
          sampleOrders: { $push: {
            orderId: '$_id',
            customerId: '$customerId',
            orderValue: '$orderValue',
            itemCount: '$itemCount',
            orderDate: '$orderDate'
          }}
        }
      },

      // Stage 10: Calculate derived metrics
      {
        $addFields: {
          uniqueCustomerCount: { $size: '$uniqueCustomers' },
          uniqueSupplierCount: { $size: '$uniqueSuppliers' },
          averageOrdersPerCustomer: { 
            $divide: ['$totalOrders', { $size: '$uniqueCustomers' }] 
          },
          revenuePerCustomer: { 
            $divide: ['$totalRevenue', { $size: '$uniqueCustomers' }] 
          },
          profitMarginPercent: { 
            $multiply: [{ $divide: ['$totalProfit', '$totalRevenue'] }, 100] 
          },
          customerAcquisitionRate: {
            $divide: ['$newCustomers', { $size: '$uniqueCustomers' }]
          }
        }
      },

      // Stage 11: Add ranking and percentile information
      {
        $setWindowFields: {
          partitionBy: { year: '$_id.year', quarter: '$_id.quarter' },
          sortBy: { totalRevenue: -1 },
          output: {
            revenueRank: { $rank: {} },
            revenuePercentile: { $percentRank: {} },
            cumulativeRevenue: { $sum: '$totalRevenue', window: { documents: ['unbounded preceding', 'current'] } },
            movingAvgRevenue: { $avg: '$totalRevenue', window: { documents: [-2, 2] } }
          }
        }
      },

      // Stage 12: Calculate growth rates using window functions
      {
        $setWindowFields: {
          partitionBy: { 
            category: '$_id.category', 
            country: '$_id.country' 
          },
          sortBy: { year: 1, month: 1 },
          output: {
            previousMonthRevenue: { 
              $shift: { output: '$totalRevenue', by: -1 } 
            },
            previousYearRevenue: { 
              $shift: { output: '$totalRevenue', by: -12 } 
            }
          }
        }
      },

      // Stage 13: Calculate final growth metrics
      {
        $addFields: {
          monthOverMonthGrowth: {
            $cond: [
              { $gt: ['$previousMonthRevenue', 0] },
              { 
                $subtract: [
                  { $divide: ['$totalRevenue', '$previousMonthRevenue'] },
                  1
                ]
              },
              null
            ]
          },
          yearOverYearGrowth: {
            $cond: [
              { $gt: ['$previousYearRevenue', 0] },
              { 
                $subtract: [
                  { $divide: ['$totalRevenue', '$previousYearRevenue'] },
                  1
                ]
              },
              null
            ]
          }
        }
      },

      // Stage 14: Add performance indicators
      {
        $addFields: {
          performanceIndicator: {
            $switch: {
              branches: [
                { 
                  case: { $and: [
                    { $gt: ['$monthOverMonthGrowth', 0.1] },
                    { $gt: ['$profitMarginPercent', 20] }
                  ]},
                  then: 'excellent'
                },
                { 
                  case: { $and: [
                    { $gt: ['$monthOverMonthGrowth', 0.05] },
                    { $gt: ['$profitMarginPercent', 15] }
                  ]},
                  then: 'good'
                },
                { 
                  case: { $or: [
                    { $lt: ['$monthOverMonthGrowth', -0.1] },
                    { $lt: ['$profitMarginPercent', 5] }
                  ]},
                  then: 'concerning'
                }
              ],
              default: 'average'
            }
          },

          // Business recommendations
          recommendation: {
            $switch: {
              branches: [
                { 
                  case: { $lt: ['$monthOverMonthGrowth', -0.2] },
                  then: 'Urgent: Investigate revenue decline and implement recovery strategy'
                },
                { 
                  case: { $lt: ['$profitMarginPercent', 5] },
                  then: 'Action: Review pricing strategy and cost structure'
                },
                { 
                  case: { $and: [
                    { $gt: ['$monthOverMonthGrowth', 0.15] },
                    { $gt: ['$revenuePercentile', 0.8] }
                  ]},
                  then: 'Opportunity: Scale successful strategies and increase investment'
                },
                { 
                  case: { $lt: ['$customerAcquisitionRate', 0.1] },
                  then: 'Focus: Improve customer acquisition and marketing effectiveness'
                }
              ],
              default: 'Monitor: Continue current strategies with minor optimizations'
            }
          }
        }
      },

      // Stage 15: Sort by strategic importance
      {
        $sort: {
          'totalRevenue': -1,
          'profitMarginPercent': -1,
          '_id.year': -1,
          '_id.month': -1
        }
      },

      // Stage 16: Limit to top performing segments for detailed analysis
      { $limit: 100 }
    ];

    const results = await this.collections.orders.aggregate(pipeline).toArray();

    console.log(`Sales analytics completed: ${results.length} segments analyzed`);
    return results;
  }

  async executeCustomerSegmentationPipeline() {
    console.log('Executing advanced customer segmentation pipeline...');

    const pipeline = [
      // Stage 1: Match active customers with orders
      {
        $match: {
          'account.status': 'active',
          'account.createdAt': { $gte: new Date(Date.now() - 730 * 24 * 60 * 60 * 1000) } // Last 2 years
        }
      },

      // Stage 2: Lookup customer orders
      {
        $lookup: {
          from: 'orders',
          localField: '_id',
          foreignField: 'customerId',
          as: 'orders',
          pipeline: [
            {
              $match: {
                status: 'completed',
                orderDate: { $gte: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000) }
              }
            },
            {
              $project: {
                orderDate: 1,
                'totals.total': 1,
                'totals.currency': 1,
                items: 1
              }
            }
          ]
        }
      },

      // Stage 3: Calculate RFM metrics (Recency, Frequency, Monetary)
      {
        $addFields: {
          // Recency: Days since last order
          recency: {
            $cond: [
              { $gt: [{ $size: '$orders' }, 0] },
              {
                $divide: [
                  { $subtract: [new Date(), { $max: '$orders.orderDate' }] },
                  1000 * 60 * 60 * 24 // Convert to days
                ]
              },
              999 // Default high recency for customers with no orders
            ]
          },

          // Frequency: Number of orders
          frequency: { $size: '$orders' },

          // Monetary: Total spending
          monetary: {
            $reduce: {
              input: '$orders',
              initialValue: 0,
              in: { $add: ['$$value', '$$this.totals.total'] }
            }
          },

          // Additional customer metrics
          avgOrderValue: {
            $cond: [
              { $gt: [{ $size: '$orders' }, 0] },
              {
                $divide: [
                  {
                    $reduce: {
                      input: '$orders',
                      initialValue: 0,
                      in: { $add: ['$$value', '$$this.totals.total'] }
                    }
                  },
                  { $size: '$orders' }
                ]
              },
              0
            ]
          },

          firstOrderDate: { $min: '$orders.orderDate' },
          lastOrderDate: { $max: '$orders.orderDate' },

          // Calculate customer lifetime (days)
          customerLifetime: {
            $cond: [
              { $gt: [{ $size: '$orders' }, 0] },
              {
                $divide: [
                  { $subtract: [{ $max: '$orders.orderDate' }, { $min: '$orders.orderDate' }] },
                  1000 * 60 * 60 * 24
                ]
              },
              0
            ]
          }
        }
      },

      // Stage 4: Calculate RFM scores using percentile ranking
      {
        $setWindowFields: {
          sortBy: { recency: 1 }, // Lower recency is better (more recent)
          output: {
            recencyScore: {
              $percentRank: {}
            }
          }
        }
      },

      {
        $setWindowFields: {
          sortBy: { frequency: -1 }, // Higher frequency is better
          output: {
            frequencyScore: {
              $percentRank: {}
            }
          }
        }
      },

      {
        $setWindowFields: {
          sortBy: { monetary: -1 }, // Higher monetary is better
          output: {
            monetaryScore: {
              $percentRank: {}
            }
          }
        }
      },

      // Stage 5: Create RFM segments
      {
        $addFields: {
          // Convert percentile scores to 1-5 scale
          recencyBucket: {
            $ceil: { $multiply: [{ $subtract: [1, '$recencyScore'] }, 5] }
          },
          frequencyBucket: {
            $ceil: { $multiply: ['$frequencyScore', 5] }
          },
          monetaryBucket: {
            $ceil: { $multiply: ['$monetaryScore', 5] }
          }
        }
      },

      // Stage 6: Create customer segments based on RFM
      {
        $addFields: {
          rfmScore: {
            $concat: [
              { $toString: '$recencyBucket' },
              { $toString: '$frequencyBucket' },
              { $toString: '$monetaryBucket' }
            ]
          },

          customerSegment: {
            $switch: {
              branches: [
                // Champions: High value, bought recently, buy often
                { 
                  case: { $and: [
                    { $gte: ['$recencyBucket', 4] },
                    { $gte: ['$frequencyBucket', 4] },
                    { $gte: ['$monetaryBucket', 4] }
                  ]},
                  then: 'champions'
                },
                // Loyal customers: High frequency and monetary, but not recent
                { 
                  case: { $and: [
                    { $gte: ['$frequencyBucket', 4] },
                    { $gte: ['$monetaryBucket', 4] }
                  ]},
                  then: 'loyal_customers'
                },
                // Potential loyalists: Recent customers with good frequency
                { 
                  case: { $and: [
                    { $gte: ['$recencyBucket', 4] },
                    { $gte: ['$frequencyBucket', 3] }
                  ]},
                  then: 'potential_loyalists'
                },
                // New customers: Recent but low frequency/monetary
                { 
                  case: { $and: [
                    { $gte: ['$recencyBucket', 4] },
                    { $lte: ['$frequencyBucket', 2] }
                  ]},
                  then: 'new_customers'
                },
                // Promising: Recent moderate spenders
                { 
                  case: { $and: [
                    { $gte: ['$recencyBucket', 3] },
                    { $gte: ['$monetaryBucket', 3] }
                  ]},
                  then: 'promising'
                },
                // Need attention: Recent low spenders
                { 
                  case: { $and: [
                    { $gte: ['$recencyBucket', 3] },
                    { $lte: ['$monetaryBucket', 2] }
                  ]},
                  then: 'need_attention'
                },
                // About to sleep: Low recency but good historical value
                { 
                  case: { $and: [
                    { $lte: ['$recencyBucket', 2] },
                    { $gte: ['$monetaryBucket', 3] }
                  ]},
                  then: 'about_to_sleep'
                },
                // At risk: Low recency and frequency but good monetary
                { 
                  case: { $and: [
                    { $lte: ['$recencyBucket', 2] },
                    { $lte: ['$frequencyBucket', 2] },
                    { $gte: ['$monetaryBucket', 3] }
                  ]},
                  then: 'at_risk'
                },
                // Cannot lose: Very low recency but high monetary
                { 
                  case: { $and: [
                    { $eq: ['$recencyBucket', 1] },
                    { $gte: ['$monetaryBucket', 4] }
                  ]},
                  then: 'cannot_lose'
                },
                // Hibernating: Low across all dimensions
                { 
                  case: { $and: [
                    { $lte: ['$recencyBucket', 2] },
                    { $lte: ['$frequencyBucket', 2] },
                    { $lte: ['$monetaryBucket', 2] }
                  ]},
                  then: 'hibernating'
                }
              ],
              default: 'others'
            }
          },

          // Calculate customer lifetime value
          customerLifetimeValue: {
            $multiply: [
              '$avgOrderValue',
              { $divide: ['$frequency', { $max: [1, { $divide: ['$customerLifetime', 365] }] }] }, // Orders per year
              3 // Projected future years
            ]
          },

          // Churn risk assessment
          churnRisk: {
            $switch: {
              branches: [
                { case: { $gte: ['$recency', 180] }, then: 'high' },
                { case: { $gte: ['$recency', 90] }, then: 'medium' },
                { case: { $gte: ['$recency', 30] }, then: 'low' }
              ],
              default: 'very_low'
            }
          }
        }
      },

      // Stage 7: Enrich with customer profile data
      {
        $addFields: {
          profileCompleteness: {
            $divide: [
              {
                $add: [
                  { $cond: [{ $ne: ['$profile.firstName', null] }, 1, 0] },
                  { $cond: [{ $ne: ['$profile.lastName', null] }, 1, 0] },
                  { $cond: [{ $ne: ['$profile.phone', null] }, 1, 0] },
                  { $cond: [{ $ne: ['$profile.location', null] }, 1, 0] },
                  { $cond: [{ $ne: ['$profile.dateOfBirth', null] }, 1, 0] },
                  { $cond: [{ $ne: ['$preferences', null] }, 1, 0] }
                ]
              },
              6
            ]
          },

          engagementLevel: {
            $switch: {
              branches: [
                { 
                  case: { $and: [
                    { $gte: ['$frequency', 10] },
                    { $lte: ['$recency', 30] }
                  ]},
                  then: 'highly_engaged'
                },
                { 
                  case: { $and: [
                    { $gte: ['$frequency', 5] },
                    { $lte: ['$recency', 60] }
                  ]},
                  then: 'moderately_engaged'
                },
                { 
                  case: { $and: [
                    { $gte: ['$frequency', 2] },
                    { $lte: ['$recency', 120] }
                  ]},
                  then: 'lightly_engaged'
                }
              ],
              default: 'disengaged'
            }
          }
        }
      },

      // Stage 8: Create final customer analysis
      {
        $project: {
          _id: 1,
          email: 1,
          'profile.firstName': 1,
          'profile.lastName': 1,
          'profile.location.country': 1,
          'profile.location.region': 1,
          'account.type': 1,
          'account.createdAt': 1,

          // RFM Analysis
          recency: { $round: ['$recency', 1] },
          frequency: 1,
          monetary: { $round: ['$monetary', 2] },
          rfmScore: 1,
          recencyBucket: 1,
          frequencyBucket: 1,
          monetaryBucket: 1,

          // Customer Classification
          customerSegment: 1,
          churnRisk: 1,
          engagementLevel: 1,

          // Business Metrics
          avgOrderValue: { $round: ['$avgOrderValue', 2] },
          customerLifetimeValue: { $round: ['$customerLifetimeValue', 2] },
          customerLifetime: { $round: ['$customerLifetime', 0] },
          profileCompleteness: { $round: [{ $multiply: ['$profileCompleteness', 100] }, 1] },

          // Timeline
          firstOrderDate: 1,
          lastOrderDate: 1,

          // Marketing recommendations
          marketingAction: {
            $switch: {
              branches: [
                { case: { $eq: ['$customerSegment', 'champions'] }, then: 'Reward and advocate program' },
                { case: { $eq: ['$customerSegment', 'loyal_customers'] }, then: 'Upsell and cross-sell premium products' },
                { case: { $eq: ['$customerSegment', 'potential_loyalists'] }, then: 'Loyalty program enrollment' },
                { case: { $eq: ['$customerSegment', 'new_customers'] }, then: 'Onboarding and education campaign' },
                { case: { $eq: ['$customerSegment', 'promising'] }, then: 'Targeted promotions and engagement' },
                { case: { $eq: ['$customerSegment', 'need_attention'] }, then: 'Value demonstration and support' },
                { case: { $eq: ['$customerSegment', 'about_to_sleep'] }, then: 'Re-engagement campaign with incentives' },
                { case: { $eq: ['$customerSegment', 'at_risk'] }, then: 'Urgent retention program' },
                { case: { $eq: ['$customerSegment', 'cannot_lose'] }, then: 'Win-back campaign with premium offers' },
                { case: { $eq: ['$customerSegment', 'hibernating'] }, then: 'Reactivation with significant discount' }
              ],
              default: 'Monitor and nurture'
            }
          }
        }
      },

      // Stage 9: Sort by customer value and risk
      {
        $sort: {
          customerLifetimeValue: -1,
          recency: 1,
          frequency: -1
        }
      }
    ];

    const results = await this.collections.customers.aggregate(pipeline).toArray();

    console.log(`Customer segmentation completed: ${results.length} customers analyzed`);
    return results;
  }

  async executeProductPerformancePipeline() {
    console.log('Executing product performance analysis pipeline...');

    const pipeline = [
      // Stage 1: Match products with sales data
      {
        $lookup: {
          from: 'orders',
          let: { productId: '$_id' },
          pipeline: [
            {
              $match: {
                status: 'completed',
                orderDate: { $gte: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000) }
              }
            },
            { $unwind: '$items' },
            {
              $match: {
                $expr: { $eq: ['$items.productId', '$$productId'] }
              }
            },
            {
              $project: {
                orderDate: 1,
                customerId: 1,
                quantity: '$items.quantity',
                unitPrice: '$items.unitPrice',
                totalPrice: '$items.totalPrice',
                'customer.location.country': 1
              }
            }
          ],
          as: 'sales'
        }
      },

      // Stage 2: Calculate comprehensive product metrics
      {
        $addFields: {
          // Sales volume metrics
          totalUnitsSold: {
            $reduce: {
              input: '$sales',
              initialValue: 0,
              in: { $add: ['$$value', '$$this.quantity'] }
            }
          },

          totalRevenue: {
            $reduce: {
              input: '$sales',
              initialValue: 0,
              in: { $add: ['$$value', '$$this.totalPrice'] }
            }
          },

          totalOrders: { $size: '$sales' },
          uniqueCustomers: { $size: { $setUnion: [{ $map: { input: '$sales', as: 'sale', in: '$$sale.customerId' } }, []] } },

          // Pricing analysis
          avgSellingPrice: {
            $cond: [
              { $gt: [{ $size: '$sales' }, 0] },
              {
                $divide: [
                  {
                    $reduce: {
                      input: '$sales',
                      initialValue: 0,
                      in: { $add: ['$$value', '$$this.unitPrice'] }
                    }
                  },
                  { $size: '$sales' }
                ]
              },
              0
            ]
          },

          // Profit analysis
          totalProfit: {
            $reduce: {
              input: '$sales',
              initialValue: 0,
              in: { 
                $add: [
                  '$$value', 
                  { 
                    $multiply: [
                      '$$this.quantity',
                      { $subtract: ['$$this.unitPrice', '$pricing.cost'] }
                    ]
                  }
                ]
              }
            }
          },

          // Time-based analysis
          firstSaleDate: { $min: '$sales.orderDate' },
          lastSaleDate: { $max: '$sales.orderDate' },

          // Calculate monthly sales trend
          monthlySales: {
            $map: {
              input: { $range: [0, 12] },
              as: 'monthOffset',
              in: {
                month: {
                  $dateFromParts: {
                    year: { $year: { $dateSubtract: { startDate: new Date(), unit: 'month', amount: '$$monthOffset' } } },
                    month: { $month: { $dateSubtract: { startDate: new Date(), unit: 'month', amount: '$$monthOffset' } } },
                    day: 1
                  }
                },
                sales: {
                  $reduce: {
                    input: {
                      $filter: {
                        input: '$sales',
                        cond: {
                          $and: [
                            { $gte: ['$$this.orderDate', { $dateSubtract: { startDate: new Date(), unit: 'month', amount: { $add: ['$$monthOffset', 1] } } }] },
                            { $lt: ['$$this.orderDate', { $dateSubtract: { startDate: new Date(), unit: 'month', amount: '$$monthOffset' } }] }
                          ]
                        }
                      }
                    },
                    initialValue: 0,
                    in: { $add: ['$$value', '$$this.totalPrice'] }
                  }
                }
              }
            }
          }
        }
      },

      // Stage 3: Calculate performance indicators
      {
        $addFields: {
          // Performance ratios
          profitMargin: {
            $cond: [
              { $gt: ['$totalRevenue', 0] },
              { $divide: ['$totalProfit', '$totalRevenue'] },
              0
            ]
          },

          revenuePerCustomer: {
            $cond: [
              { $gt: ['$uniqueCustomers', 0] },
              { $divide: ['$totalRevenue', '$uniqueCustomers'] },
              0
            ]
          },

          avgOrderValue: {
            $cond: [
              { $gt: ['$totalOrders', 0] },
              { $divide: ['$totalRevenue', '$totalOrders'] },
              0
            ]
          },

          // Inventory turnover (simplified)
          inventoryTurnover: {
            $cond: [
              { $gt: ['$inventory.quantity', 0] },
              { $divide: ['$totalUnitsSold', '$inventory.quantity'] },
              0
            ]
          },

          // Product lifecycle stage
          lifecycleStage: {
            $switch: {
              branches: [
                { 
                  case: { 
                    $lt: [
                      '$firstSaleDate', 
                      { $dateSubtract: { startDate: new Date(), unit: 'day', amount: 90 } }
                    ]
                  },
                  then: 'new'
                },
                {
                  case: { $and: [
                    { $gt: ['$totalRevenue', 10000] },
                    { $gt: ['$profitMargin', 0.2] }
                  ]},
                  then: 'growth'
                },
                {
                  case: { $and: [
                    { $gt: ['$totalRevenue', 50000] },
                    { $gte: ['$profitMargin', 0.15] }
                  ]},
                  then: 'maturity'
                },
                {
                  case: { $or: [
                    { $lt: ['$profitMargin', 0.1] },
                    { $lt: [
                      '$lastSaleDate',
                      { $dateSubtract: { startDate: new Date(), unit: 'day', amount: 60 } }
                    ]}
                  ]},
                  then: 'decline'
                }
              ],
              default: 'development'
            }
          },

          // Sales trend analysis
          salesTrend: {
            $let: {
              vars: {
                recentSales: { $slice: ['$monthlySales.sales', 0, 6] },
                olderSales: { $slice: ['$monthlySales.sales', 6, 6] }
              },
              in: {
                $cond: [
                  { $and: [
                    { $gt: [{ $avg: '$$recentSales' }, { $avg: '$$olderSales' }] },
                    { $gt: [{ $avg: '$$recentSales' }, 0] }
                  ]},
                  'growing',
                  {
                    $cond: [
                      { $lt: [{ $avg: '$$recentSales' }, { $multiply: [{ $avg: '$$olderSales' }, 0.8] }] },
                      'declining',
                      'stable'
                    ]
                  }
                ]
              }
            }
          }
        }
      },

      // Stage 4: Add competitive analysis using window functions
      {
        $setWindowFields: {
          partitionBy: '$category',
          sortBy: { totalRevenue: -1 },
          output: {
            categoryRank: { $rank: {} },
            categoryPercentile: { $percentRank: {} },
            marketShareInCategory: {
              $divide: [
                '$totalRevenue',
                { $sum: '$totalRevenue', window: { documents: ['unbounded preceding', 'unbounded following'] } }
              ]
            }
          }
        }
      },

      // Stage 5: Calculate final performance scores
      {
        $addFields: {
          // Overall performance score (0-100)
          performanceScore: {
            $multiply: [
              {
                $add: [
                  { $multiply: ['$categoryPercentile', 0.3] }, // Market position
                  { $multiply: [{ $min: ['$profitMargin', 0.5] }, 0.25] }, // Profitability (capped at 50%)
                  { $multiply: [{ $divide: [{ $min: ['$inventoryTurnover', 10] }, 10] }, 0.2] }, // Efficiency (capped at 10x)
                  { 
                    $multiply: [
                      {
                        $switch: {
                          branches: [
                            { case: { $eq: ['$salesTrend', 'growing'] }, then: 1 },
                            { case: { $eq: ['$salesTrend', 'stable'] }, then: 0.7 },
                            { case: { $eq: ['$salesTrend', 'declining'] }, then: 0.3 }
                          ],
                          default: 0.5
                        }
                      },
                      0.25
                    ]
                  } // Growth trend
                ]
              },
              100
            ]
          },

          // Strategic recommendations
          strategicRecommendation: {
            $switch: {
              branches: [
                {
                  case: { $and: [
                    { $eq: ['$salesTrend', 'growing'] },
                    { $gt: ['$profitMargin', 0.25] },
                    { $lt: ['$categoryRank', 5] }
                  ]},
                  then: 'Star Product: Increase investment and marketing focus'
                },
                {
                  case: { $and: [
                    { $eq: ['$lifecycleStage', 'maturity'] },
                    { $gt: ['$profitMargin', 0.2] }
                  ]},
                  then: 'Cash Cow: Optimize operations and maintain market share'
                },
                {
                  case: { $and: [
                    { $eq: ['$salesTrend', 'growing'] },
                    { $lt: ['$profitMargin', 0.15] }
                  ]},
                  then: 'Question Mark: Improve margins or consider repositioning'
                },
                {
                  case: { $and: [
                    { $eq: ['$salesTrend', 'declining'] },
                    { $lt: ['$profitMargin', 0.1] }
                  ]},
                  then: 'Dog: Consider discontinuation or major repositioning'
                },
                {
                  case: { $eq: ['$lifecycleStage', 'new'] },
                  then: 'Monitor closely and provide marketing support'
                }
              ],
              default: 'Maintain current strategy with regular monitoring'
            }
          }
        }
      },

      // Stage 6: Final projection and sorting
      {
        $project: {
          _id: 1,
          name: 1,
          category: 1,
          brand: 1,
          'pricing.cost': 1,
          'pricing.retail': 1,

          // Sales performance
          totalUnitsSold: 1,
          totalRevenue: { $round: ['$totalRevenue', 2] },
          totalProfit: { $round: ['$totalProfit', 2] },
          totalOrders: 1,
          uniqueCustomers: 1,

          // Financial metrics
          avgSellingPrice: { $round: ['$avgSellingPrice', 2] },
          profitMargin: { $round: [{ $multiply: ['$profitMargin', 100] }, 2] },
          revenuePerCustomer: { $round: ['$revenuePerCustomer', 2] },
          avgOrderValue: { $round: ['$avgOrderValue', 2] },

          // Performance indicators
          performanceScore: { $round: ['$performanceScore', 1] },
          lifecycleStage: 1,
          salesTrend: 1,
          categoryRank: 1,
          marketShareInCategory: { $round: [{ $multiply: ['$marketShareInCategory', 100] }, 3] },

          // Operational metrics
          inventoryTurnover: { $round: ['$inventoryTurnover', 2] },
          'inventory.quantity': 1,
          'inventory.lowStockThreshold': 1,

          // Timeline
          firstSaleDate: 1,
          lastSaleDate: 1,

          // Strategic guidance
          strategicRecommendation: 1,

          // Monthly trend data (last 6 months)
          recentMonthlySales: { $slice: ['$monthlySales', 0, 6] }
        }
      },

      // Stage 7: Sort by performance score and revenue
      {
        $sort: {
          performanceScore: -1,
          totalRevenue: -1
        }
      }
    ];

    const results = await this.collections.products.aggregate(pipeline).toArray();

    console.log(`Product performance analysis completed: ${results.length} products analyzed`);
    return results;
  }

  async executeTimeSeriesAnalytics() {
    console.log('Executing time-series analytics with advanced forecasting...');

    const pipeline = [
      // Stage 1: Match recent orders for time-series analysis
      {
        $match: {
          status: 'completed',
          orderDate: { $gte: new Date(Date.now() - 730 * 24 * 60 * 60 * 1000) } // Last 2 years
        }
      },

      // Stage 2: Group by time periods
      {
        $group: {
          _id: {
            year: { $year: '$orderDate' },
            month: { $month: '$orderDate' },
            week: { $week: '$orderDate' },
            dayOfWeek: { $dayOfWeek: '$orderDate' },
            hour: { $hour: '$orderDate' }
          },

          // Core metrics
          orderCount: { $sum: 1 },
          totalRevenue: { $sum: '$totals.total' },
          avgOrderValue: { $avg: '$totals.total' },
          uniqueCustomers: { $addToSet: '$customerId' },

          // Item-level aggregations
          totalItemsSold: {
            $sum: {
              $reduce: {
                input: '$items',
                initialValue: 0,
                in: { $add: ['$$value', '$$this.quantity'] }
              }
            }
          },

          // Distribution analysis
          orderValues: { $push: '$totals.total' },

          // Customer behavior
          newCustomers: {
            $sum: {
              $cond: [
                { $eq: [{ $dayOfYear: '$orderDate' }, { $dayOfYear: '$customer.account.createdAt' }] },
                1,
                0
              ]
            }
          },

          // Geographic distribution
          countries: { $addToSet: '$shippingAddress.country' },
          regions: { $addToSet: '$shippingAddress.region' }
        }
      },

      // Stage 3: Add time-based calculations
      {
        $addFields: {
          // Convert _id to more usable date format
          date: {
            $dateFromParts: {
              year: '$_id.year',
              month: '$_id.month',
              day: 1
            }
          },

          uniqueCustomerCount: { $size: '$uniqueCustomers' },
          uniqueCountryCount: { $size: '$countries' },

          // Statistical measures
          revenueStdDev: { $stdDevPop: '$orderValues' },
          medianOrderValue: {
            $let: {
              vars: {
                sortedValues: {
                  $sortArray: {
                    input: '$orderValues',
                    sortBy: 1
                  }
                }
              },
              in: {
                $arrayElemAt: [
                  '$$sortedValues',
                  { $floor: { $divide: [{ $size: '$$sortedValues' }, 2] } }
                ]
              }
            }
          },

          // Time period classifications
          periodType: {
            $switch: {
              branches: [
                { case: { $gte: ['$_id.dayOfWeek', 2] }, then: 'weekend' },
                { case: { $lte: ['$_id.dayOfWeek', 6] }, then: 'weekday' }
              ],
              default: 'weekend'
            }
          },

          timeOfDay: {
            $switch: {
              branches: [
                { case: { $lt: ['$_id.hour', 6] }, then: 'late_night' },
                { case: { $lt: ['$_id.hour', 12] }, then: 'morning' },
                { case: { $lt: ['$_id.hour', 18] }, then: 'afternoon' },
                { case: { $lt: ['$_id.hour', 22] }, then: 'evening' }
              ],
              default: 'night'
            }
          }
        }
      },

      // Stage 4: Add moving averages and trend analysis
      {
        $setWindowFields: {
          partitionBy: null,
          sortBy: { date: 1 },
          output: {
            // Moving averages
            revenue7DayMA: {
              $avg: '$totalRevenue',
              window: { documents: [-6, 0] }
            },
            revenue30DayMA: {
              $avg: '$totalRevenue',
              window: { documents: [-29, 0] }
            },

            // Growth calculations
            previousDayRevenue: {
              $shift: { output: '$totalRevenue', by: -1 }
            },
            previousWeekRevenue: {
              $shift: { output: '$totalRevenue', by: -7 }
            },
            previousMonthRevenue: {
              $shift: { output: '$totalRevenue', by: -30 }
            },

            // Volatility measures
            revenueVolatility: {
              $stdDevPop: '$totalRevenue',
              window: { documents: [-29, 0] }
            },

            // Trend strength
            trendLine: {
              $linearFill: '$totalRevenue'
            }
          }
        }
      },

      // Stage 5: Calculate growth rates and trend indicators
      {
        $addFields: {
          dayOverDayGrowth: {
            $cond: [
              { $gt: ['$previousDayRevenue', 0] },
              { $subtract: [{ $divide: ['$totalRevenue', '$previousDayRevenue'] }, 1] },
              null
            ]
          },

          weekOverWeekGrowth: {
            $cond: [
              { $gt: ['$previousWeekRevenue', 0] },
              { $subtract: [{ $divide: ['$totalRevenue', '$previousWeekRevenue'] }, 1] },
              null
            ]
          },

          monthOverMonthGrowth: {
            $cond: [
              { $gt: ['$previousMonthRevenue', 0] },
              { $subtract: [{ $divide: ['$totalRevenue', '$previousMonthRevenue'] }, 1] },
              null
            ]
          },

          // Trend classification
          trendDirection: {
            $switch: {
              branches: [
                { 
                  case: { $gt: ['$revenue7DayMA', { $multiply: ['$revenue30DayMA', 1.05] }] },
                  then: 'strong_upward'
                },
                { 
                  case: { $gt: ['$revenue7DayMA', { $multiply: ['$revenue30DayMA', 1.02] }] },
                  then: 'upward'
                },
                { 
                  case: { $lt: ['$revenue7DayMA', { $multiply: ['$revenue30DayMA', 0.95] }] },
                  then: 'strong_downward'
                },
                { 
                  case: { $lt: ['$revenue7DayMA', { $multiply: ['$revenue30DayMA', 0.98] }] },
                  then: 'downward'
                }
              ],
              default: 'stable'
            }
          },

          // Seasonality detection
          seasonalityScore: {
            $divide: [
              '$revenueStdDev',
              { $max: ['$revenue30DayMA', 1] }
            ]
          },

          // Performance classification
          performanceCategory: {
            $switch: {
              branches: [
                { 
                  case: { $gte: ['$totalRevenue', { $multiply: ['$revenue30DayMA', 1.2] }] },
                  then: 'exceptional'
                },
                { 
                  case: { $gte: ['$totalRevenue', { $multiply: ['$revenue30DayMA', 1.1] }] },
                  then: 'above_average'
                },
                { 
                  case: { $lte: ['$totalRevenue', { $multiply: ['$revenue30DayMA', 0.8] }] },
                  then: 'below_average'
                },
                { 
                  case: { $lte: ['$totalRevenue', { $multiply: ['$revenue30DayMA', 0.9] }] },
                  then: 'poor'
                }
              ],
              default: 'average'
            }
          }
        }
      },

      // Stage 6: Add forecasting indicators
      {
        $addFields: {
          // Simple linear trend projection (next 7 days)
          next7DayForecast: {
            $add: [
              '$revenue7DayMA',
              {
                $multiply: [
                  7,
                  { $subtract: ['$revenue7DayMA', { $shift: { output: '$revenue7DayMA', by: -7 } }] }
                ]
              }
            ]
          },

          // Confidence interval for forecast
          forecastConfidence: {
            $subtract: [
              100,
              { $multiply: [{ $divide: ['$revenueVolatility', '$revenue7DayMA'] }, 100] }
            ]
          },

          // Anomaly detection
          isAnomaly: {
            $or: [
              { $gt: ['$totalRevenue', { $add: ['$revenue7DayMA', { $multiply: ['$revenueVolatility', 2] }] }] },
              { $lt: ['$totalRevenue', { $subtract: ['$revenue7DayMA', { $multiply: ['$revenueVolatility', 2] }] }] }
            ]
          },

          // Business recommendations
          recommendation: {
            $switch: {
              branches: [
                {
                  case: { $eq: ['$trendDirection', 'strong_downward'] },
                  then: 'Urgent: Investigate revenue decline and implement recovery strategies'
                },
                {
                  case: { $eq: ['$trendDirection', 'strong_upward'] },
                  then: 'Opportunity: Scale successful initiatives and increase capacity'
                },
                {
                  case: { $gt: ['$seasonalityScore', 0.5] },
                  then: 'High volatility detected: Implement demand smoothing strategies'
                },
                {
                  case: { $eq: ['$performanceCategory', 'exceptional'] },
                  then: 'Analyze success factors for replication'
                }
              ],
              default: 'Continue monitoring with current strategy'
            }
          }
        }
      },

      // Stage 7: Final projection and filtering
      {
        $project: {
          date: 1,
          year: '$_id.year',
          month: '$_id.month',
          week: '$_id.week',
          dayOfWeek: '$_id.dayOfWeek',
          hour: '$_id.hour',

          // Core metrics
          orderCount: 1,
          totalRevenue: { $round: ['$totalRevenue', 2] },
          avgOrderValue: { $round: ['$avgOrderValue', 2] },
          uniqueCustomerCount: 1,
          totalItemsSold: 1,

          // Statistical measures
          medianOrderValue: { $round: ['$medianOrderValue', 2] },
          revenueStdDev: { $round: ['$revenueStdDev', 2] },

          // Trend analysis
          revenue7DayMA: { $round: ['$revenue7DayMA', 2] },
          revenue30DayMA: { $round: ['$revenue30DayMA', 2] },
          dayOverDayGrowth: { $round: [{ $multiply: ['$dayOverDayGrowth', 100] }, 2] },
          weekOverWeekGrowth: { $round: [{ $multiply: ['$weekOverWeekGrowth', 100] }, 2] },
          monthOverMonthGrowth: { $round: [{ $multiply: ['$monthOverMonthGrowth', 100] }, 2] },

          trendDirection: 1,
          performanceCategory: 1,
          seasonalityScore: { $round: ['$seasonalityScore', 3] },

          // Forecasting
          next7DayForecast: { $round: ['$next7DayForecast', 2] },
          forecastConfidence: { $round: ['$forecastConfidence', 1] },
          isAnomaly: 1,

          // Context
          periodType: 1,
          timeOfDay: 1,
          uniqueCountryCount: 1,

          // Business intelligence
          recommendation: 1
        }
      },

      // Stage 8: Sort by date descending
      {
        $sort: { date: -1 }
      },

      // Stage 9: Limit to recent data for performance
      {
        $limit: 365 // Last year of daily data
      }
    ];

    const results = await this.collections.orders.aggregate(pipeline).toArray();

    console.log(`Time-series analytics completed: ${results.length} time periods analyzed`);
    return results;
  }

  async executePredictiveAnalytics() {
    console.log('Executing predictive analytics and machine learning insights...');

    const pipeline = [
      // Stage 1: Create customer behavioral features
      {
        $match: {
          'account.status': 'active'
        }
      },

      // Stage 2: Lookup order history
      {
        $lookup: {
          from: 'orders',
          localField: '_id',
          foreignField: 'customerId',
          as: 'orders',
          pipeline: [
            {
              $match: {
                status: 'completed',
                orderDate: { $gte: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000) }
              }
            },
            {
              $project: {
                orderDate: 1,
                'totals.total': 1,
                daysSinceRegistration: {
                  $divide: [
                    { $subtract: ['$orderDate', '$customer.account.createdAt'] },
                    1000 * 60 * 60 * 24
                  ]
                }
              }
            },
            { $sort: { orderDate: 1 } }
          ]
        }
      },

      // Stage 3: Calculate predictive features
      {
        $addFields: {
          // Temporal features
          daysSinceRegistration: {
            $divide: [
              { $subtract: [new Date(), '$account.createdAt'] },
              1000 * 60 * 60 * 24
            ]
          },

          daysSinceLastOrder: {
            $cond: [
              { $gt: [{ $size: '$orders' }, 0] },
              {
                $divide: [
                  { $subtract: [new Date(), { $max: '$orders.orderDate' }] },
                  1000 * 60 * 60 * 24
                ]
              },
              999
            ]
          },

          // Purchase behavior features
          totalOrders: { $size: '$orders' },
          totalSpent: {
            $reduce: {
              input: '$orders',
              initialValue: 0,
              in: { $add: ['$$value', '$$this.totals.total'] }
            }
          },

          // Purchase frequency and regularity
          avgDaysBetweenOrders: {
            $cond: [
              { $gt: [{ $size: '$orders' }, 1] },
              {
                $divide: [
                  {
                    $divide: [
                      { $subtract: [{ $max: '$orders.orderDate' }, { $min: '$orders.orderDate' }] },
                      1000 * 60 * 60 * 24
                    ]
                  },
                  { $subtract: [{ $size: '$orders' }, 1] }
                ]
              },
              null
            ]
          },

          // Purchase pattern analysis
          orderFrequencyTrend: {
            $let: {
              vars: {
                recentOrders: {
                  $size: {
                    $filter: {
                      input: '$orders',
                      cond: { $gte: ['$$this.orderDate', { $dateSubtract: { startDate: new Date(), unit: 'day', amount: 90 } }] }
                    }
                  }
                },
                olderOrders: {
                  $size: {
                    $filter: {
                      input: '$orders',
                      cond: { 
                        $and: [
                          { $lt: ['$$this.orderDate', { $dateSubtract: { startDate: new Date(), unit: 'day', amount: 90 } }] },
                          { $gte: ['$$this.orderDate', { $dateSubtract: { startDate: new Date(), unit: 'day', amount: 180 } }] }
                        ]
                      }
                    }
                  }
                }
              },
              in: {
                $cond: [
                  { $gt: ['$$olderOrders', 0] },
                  { $subtract: [{ $divide: ['$$recentOrders', 90] }, { $divide: ['$$olderOrders', 90] }] },
                  0
                ]
              }
            }
          }
        }
      },

      // Stage 4: Calculate churn probability using logistic regression approximation
      {
        $addFields: {
          // Feature normalization and scoring
          recencyScore: {
            $cond: [
              { $gt: ['$daysSinceLastOrder', 180] }, 0.8,
              { $cond: [
                { $gt: ['$daysSinceLastOrder', 90] }, 0.6,
                { $cond: [
                  { $gt: ['$daysSinceLastOrder', 30] }, 0.3,
                  0.1
                ]}
              ]}
            ]
          },

          frequencyScore: {
            $cond: [
              { $lt: ['$totalOrders', 2] }, 0.7,
              { $cond: [
                { $lt: ['$totalOrders', 5] }, 0.5,
                { $cond: [
                  { $lt: ['$totalOrders', 10] }, 0.3,
                  0.1
                ]}
              ]}
            ]
          },

          monetaryScore: {
            $cond: [
              { $lt: ['$totalSpent', 100] }, 0.6,
              { $cond: [
                { $lt: ['$totalSpent', 500] }, 0.4,
                { $cond: [
                  { $lt: ['$totalSpent', 1000] }, 0.2,
                  0.1
                ]}
              ]}
            ]
          },

          engagementScore: {
            $cond: [
              { $lt: ['$orderFrequencyTrend', -0.5] }, 0.8,
              { $cond: [
                { $lt: ['$orderFrequencyTrend', 0] }, 0.6,
                { $cond: [
                  { $gt: ['$orderFrequencyTrend', 0.5] }, 0.1,
                  0.3
                ]}
              ]}
            ]
          }
        }
      },

      // Stage 5: Calculate composite churn probability
      {
        $addFields: {
          churnProbability: {
            $multiply: [
              {
                $add: [
                  { $multiply: ['$recencyScore', 0.35] },
                  { $multiply: ['$frequencyScore', 0.25] },
                  { $multiply: ['$monetaryScore', 0.25] },
                  { $multiply: ['$engagementScore', 0.15] }
                ]
              },
              100
            ]
          },

          // Customer lifetime value prediction
          predictedLifetimeValue: {
            $cond: [
              { $and: [
                { $gt: ['$totalOrders', 0] },
                { $gt: ['$avgDaysBetweenOrders', 0] }
              ]},
              {
                $multiply: [
                  { $divide: ['$totalSpent', '$totalOrders'] }, // Average order value
                  { $divide: [365, '$avgDaysBetweenOrders'] }, // Orders per year
                  { $subtract: [5, { $multiply: ['$churnProbability', 0.05] }] } // Expected years (adjusted for churn risk)
                ]
              },
              '$totalSpent'
            ]
          },

          // Next purchase prediction
          nextPurchasePrediction: {
            $cond: [
              { $gt: ['$avgDaysBetweenOrders', 0] },
              {
                $dateAdd: {
                  startDate: { $max: '$orders.orderDate' },
                  unit: 'day',
                  amount: { 
                    $multiply: [
                      '$avgDaysBetweenOrders',
                      { $add: [1, { $multiply: ['$churnProbability', 0.01] }] } // Adjust for churn risk
                    ]
                  }
                }
              },
              null
            ]
          },

          // Upselling opportunity score
          upsellOpportunity: {
            $multiply: [
              {
                $add: [
                  { $cond: [{ $gt: ['$totalOrders', 5] }, 0.3, 0] },
                  { $cond: [{ $gt: ['$totalSpent', 500] }, 0.3, 0] },
                  { $cond: [{ $lt: ['$daysSinceLastOrder', 30] }, 0.25, 0] },
                  { $cond: [{ $gt: ['$orderFrequencyTrend', 0] }, 0.15, 0] }
                ]
              },
              100
            ]
          }
        }
      },

      // Stage 6: Risk segmentation and recommendations
      {
        $addFields: {
          riskSegment: {
            $switch: {
              branches: [
                { case: { $gte: ['$churnProbability', 70] }, then: 'high_risk' },
                { case: { $gte: ['$churnProbability', 50] }, then: 'medium_risk' },
                { case: { $gte: ['$churnProbability', 30] }, then: 'low_risk' }
              ],
              default: 'stable'
            }
          },

          valueSegment: {
            $switch: {
              branches: [
                { case: { $gte: ['$predictedLifetimeValue', 2000] }, then: 'high_value' },
                { case: { $gte: ['$predictedLifetimeValue', 1000] }, then: 'medium_value' },
                { case: { $gte: ['$predictedLifetimeValue', 500] }, then: 'moderate_value' }
              ],
              default: 'low_value'
            }
          },

          // AI-driven marketing recommendations
          marketingRecommendation: {
            $switch: {
              branches: [
                {
                  case: { $and: [
                    { $eq: ['$riskSegment', 'high_risk'] },
                    { $in: ['$valueSegment', ['high_value', 'medium_value']] }
                  ]},
                  then: 'Urgent win-back campaign with premium incentives'
                },
                {
                  case: { $and: [
                    { $eq: ['$riskSegment', 'medium_risk'] },
                    { $gte: ['$upsellOpportunity', 60] }
                  ]},
                  then: 'Proactive engagement with upselling opportunities'
                },
                {
                  case: { $and: [
                    { $eq: ['$riskSegment', 'stable'] },
                    { $gte: ['$upsellOpportunity', 70] }
                  ]},
                  then: 'Cross-sell and premium product recommendations'
                },
                {
                  case: { $eq: ['$riskSegment', 'low_risk'] },
                  then: 'Retention campaign with loyalty program enrollment'
                }
              ],
              default: 'Monitor and maintain current engagement level'
            }
          }
        }
      },

      // Stage 7: Add market basket analysis
      {
        $lookup: {
          from: 'orders',
          let: { customerId: '$_id' },
          pipeline: [
            {
              $match: {
                $expr: { $eq: ['$customerId', '$$customerId'] },
                status: 'completed'
              }
            },
            { $unwind: '$items' },
            {
              $group: {
                _id: '$items.productId',
                purchaseCount: { $sum: 1 },
                totalQuantity: { $sum: '$items.quantity' },
                totalSpent: { $sum: '$items.totalPrice' }
              }
            },
            { $sort: { purchaseCount: -1 } },
            { $limit: 5 }
          ],
          as: 'topProducts'
        }
      },

      // Stage 8: Final projection
      {
        $project: {
          _id: 1,
          email: 1,
          'profile.firstName': 1,
          'profile.lastName': 1,
          'account.type': 1,
          'account.createdAt': 1,

          // Behavioral metrics
          daysSinceRegistration: { $round: ['$daysSinceRegistration', 0] },
          daysSinceLastOrder: { $round: ['$daysSinceLastOrder', 0] },
          totalOrders: 1,
          totalSpent: { $round: ['$totalSpent', 2] },
          avgDaysBetweenOrders: { $round: ['$avgDaysBetweenOrders', 1] },

          // Predictive scores
          churnProbability: { $round: ['$churnProbability', 1] },
          predictedLifetimeValue: { $round: ['$predictedLifetimeValue', 2] },
          upsellOpportunity: { $round: ['$upsellOpportunity', 1] },

          // Segmentation
          riskSegment: 1,
          valueSegment: 1,

          // Predictions
          nextPurchasePrediction: 1,
          marketingRecommendation: 1,

          // Product affinity
          topProducts: 1,

          // Trend analysis
          orderFrequencyTrend: { $round: ['$orderFrequencyTrend', 3] }
        }
      },

      // Stage 9: Sort by strategic importance
      {
        $sort: {
          predictedLifetimeValue: -1,
          churnProbability: -1,
          upsellOpportunity: -1
        }
      },

      // Stage 10: Limit to top opportunities
      {
        $limit: 1000
      }
    ];

    const results = await this.collections.customers.aggregate(pipeline).toArray();

    console.log(`Predictive analytics completed: ${results.length} customers analyzed with ML insights`);
    return results;
  }

  async cacheAnalyticsResults(analysisType, data) {
    console.log(`Caching ${analysisType} analytics results...`);

    try {
      await this.collections.analytics.replaceOne(
        { type: analysisType },
        {
          type: analysisType,
          data: data,
          generatedAt: new Date(),
          expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000) // 24 hour TTL
        },
        { upsert: true }
      );

    } catch (error) {
      console.warn('Failed to cache analytics results:', error.message);
    }
  }

  async getAnalyticsDashboard() {
    console.log('Generating comprehensive analytics dashboard...');

    const [
      salesSummary,
      customerInsights,
      productInsights,
      timeSeriesInsights,
      predictiveInsights
    ] = await Promise.all([
      this.getSalesSummary(),
      this.getCustomerInsights(),
      this.getProductInsights(),
      this.getTimeSeriesInsights(),
      this.getPredictiveInsights()
    ]);

    return {
      dashboard: {
        salesSummary,
        customerInsights,
        productInsights,
        timeSeriesInsights,
        predictiveInsights
      },
      metadata: {
        generatedAt: new Date(),
        dataFreshness: '< 1 hour',
        recordsCovered: {
          orders: salesSummary.totalOrders || 0,
          customers: customerInsights.totalCustomers || 0,
          products: productInsights.totalProducts || 0
        }
      }
    };
  }

  async getSalesSummary() {
    const pipeline = [
      {
        $match: {
          status: 'completed',
          orderDate: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
        }
      },
      {
        $group: {
          _id: null,
          totalOrders: { $sum: 1 },
          totalRevenue: { $sum: '$totals.total' },
          avgOrderValue: { $avg: '$totals.total' },
          uniqueCustomers: { $addToSet: '$customerId' }
        }
      },
      {
        $project: {
          totalOrders: 1,
          totalRevenue: { $round: ['$totalRevenue', 2] },
          avgOrderValue: { $round: ['$avgOrderValue', 2] },
          uniqueCustomers: { $size: '$uniqueCustomers' }
        }
      }
    ];

    const result = await this.collections.orders.aggregate(pipeline).toArray();
    return result[0] || {};
  }

  async getCustomerInsights() {
    const pipeline = [
      {
        $group: {
          _id: null,
          totalCustomers: { $sum: 1 },
          activeCustomers: { $sum: { $cond: [{ $eq: ['$account.status', 'active'] }, 1, 0] } },
          premiumCustomers: { $sum: { $cond: [{ $eq: ['$account.type', 'premium'] }, 1, 0] } }
        }
      }
    ];

    const result = await this.collections.customers.aggregate(pipeline).toArray();
    return result[0] || {};
  }

  async getProductInsights() {
    const pipeline = [
      {
        $group: {
          _id: null,
          totalProducts: { $sum: 1 },
          activeProducts: { $sum: { $cond: [{ $eq: ['$status', 'active'] }, 1, 0] } },
          avgPrice: { $avg: '$pricing.retail' }
        }
      },
      {
        $project: {
          totalProducts: 1,
          activeProducts: 1,
          avgPrice: { $round: ['$avgPrice', 2] }
        }
      }
    ];

    const result = await this.collections.products.aggregate(pipeline).toArray();
    return result[0] || {};
  }

  async getTimeSeriesInsights() {
    return {
      trend: 'upward',
      growthRate: 12.5,
      volatility: 'moderate'
    };
  }

  async getPredictiveInsights() {
    return {
      averageChurnRisk: 25.3,
      highValueCustomers: 150,
      upsellOpportunities: 320
    };
  }
}

// Benefits of MongoDB Advanced Aggregation Framework:
// - Real-time analytics processing without ETL pipelines or data warehouses
// - Complex multi-stage transformations with window functions and statistical operations
// - Advanced time-series analysis with forecasting and trend detection capabilities
// - Machine learning integration for predictive analytics and customer segmentation
// - Flexible aggregation patterns that adapt to changing analytical requirements
// - High-performance processing that scales with data volume and complexity
// - SQL-compatible analytical operations through QueryLeaf integration
// - Comprehensive business intelligence capabilities within the operational database
// - Advanced statistical functions and mathematical operations for data science
// - Real-time dashboard generation with automated insights and recommendations

module.exports = {
  MongoDBAnalyticsEngine
};

SQL-Style Aggregation with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB aggregation operations:

-- QueryLeaf advanced analytics with SQL-familiar aggregation syntax

-- Complex sales analytics with window functions and advanced aggregations
WITH monthly_sales_analysis AS (
  SELECT 
    DATE_TRUNC('month', order_date) as month,
    product_category,
    customer_location.country,
    customer_type,

    -- Basic aggregations
    COUNT(*) as order_count,
    COUNT(DISTINCT customer_id) as unique_customers,
    SUM(total_amount) as total_revenue,
    AVG(total_amount) as avg_order_value,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY total_amount) as median_order_value,
    STDDEV_POP(total_amount) as order_value_stddev,

    -- Item-level aggregations
    SUM(item_quantity) as total_items_sold,
    AVG(item_quantity) as avg_items_per_order,
    SUM(item_quantity * item_unit_price) as item_revenue,
    AVG(item_unit_price) as avg_item_price,

    -- Advanced calculations
    SUM(item_quantity * (item_unit_price - product_cost)) as total_profit,
    AVG((item_unit_price - product_cost) / item_unit_price) as avg_profit_margin,

    -- Customer behavior metrics
    COUNT(*) FILTER (WHERE customer_registration_date >= DATE_TRUNC('month', order_date)) as new_customers,
    COUNT(DISTINCT customer_id) FILTER (WHERE previous_order_date < DATE_TRUNC('month', order_date) - INTERVAL '3 months') as returning_customers,

    -- Geographic diversity
    COUNT(DISTINCT customer_location.country) as unique_countries,
    COUNT(DISTINCT customer_location.region) as unique_regions

  FROM orders o
  CROSS JOIN UNNEST(o.items) as item
  JOIN products p ON item.product_id = p._id
  JOIN customers c ON o.customer_id = c._id
  WHERE o.status = 'completed'
    AND o.order_date >= CURRENT_DATE - INTERVAL '24 months'
  GROUP BY 
    DATE_TRUNC('month', order_date),
    product_category,
    customer_location.country,
    customer_type
),

-- Advanced window functions for trend analysis
sales_with_trends AS (
  SELECT 
    *,

    -- Moving averages
    AVG(total_revenue) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month
      ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
    ) as revenue_3month_ma,

    AVG(total_revenue) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month  
      ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
    ) as revenue_6month_ma,

    -- Growth calculations
    LAG(total_revenue, 1) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month
    ) as prev_month_revenue,

    LAG(total_revenue, 12) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month
    ) as prev_year_revenue,

    -- Ranking and percentiles
    RANK() OVER (
      PARTITION BY month
      ORDER BY total_revenue DESC
    ) as monthly_revenue_rank,

    PERCENT_RANK() OVER (
      PARTITION BY month
      ORDER BY total_revenue
    ) as monthly_revenue_percentile,

    -- Cumulative calculations
    SUM(total_revenue) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month
      ROWS UNBOUNDED PRECEDING
    ) as cumulative_revenue,

    -- Volatility measures
    STDDEV(total_revenue) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month
      ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
    ) as revenue_volatility,

    -- Lead/lag for forecasting
    LEAD(total_revenue, 1) OVER (
      PARTITION BY product_category, customer_location.country
      ORDER BY month
    ) as next_month_actual,

    -- Dense rank for market position
    DENSE_RANK() OVER (
      PARTITION BY month
      ORDER BY total_revenue DESC
    ) as market_position

  FROM monthly_sales_analysis
),

-- Calculate growth rates and performance indicators
performance_metrics AS (
  SELECT 
    *,

    -- Growth rate calculations
    CASE 
      WHEN prev_month_revenue > 0 THEN 
        ROUND(((total_revenue - prev_month_revenue) / prev_month_revenue * 100), 2)
      ELSE NULL
    END as month_over_month_growth,

    CASE 
      WHEN prev_year_revenue > 0 THEN
        ROUND(((total_revenue - prev_year_revenue) / prev_year_revenue * 100), 2)
      ELSE NULL
    END as year_over_year_growth,

    -- Trend classification
    CASE 
      WHEN revenue_3month_ma > revenue_6month_ma * 1.05 THEN 'strong_growth'
      WHEN revenue_3month_ma > revenue_6month_ma * 1.02 THEN 'moderate_growth'
      WHEN revenue_3month_ma < revenue_6month_ma * 0.95 THEN 'declining'
      WHEN revenue_3month_ma < revenue_6month_ma * 0.98 THEN 'weak_growth'
      ELSE 'stable'
    END as trend_classification,

    -- Performance assessment
    CASE 
      WHEN monthly_revenue_percentile >= 0.9 THEN 'top_performer'
      WHEN monthly_revenue_percentile >= 0.75 THEN 'strong_performer'
      WHEN monthly_revenue_percentile >= 0.5 THEN 'average_performer'
      WHEN monthly_revenue_percentile >= 0.25 THEN 'weak_performer'
      ELSE 'bottom_performer'
    END as performance_category,

    -- Volatility assessment
    CASE 
      WHEN revenue_volatility / NULLIF(revenue_6month_ma, 0) > 0.3 THEN 'high_volatility'
      WHEN revenue_volatility / NULLIF(revenue_6month_ma, 0) > 0.15 THEN 'moderate_volatility'
      ELSE 'low_volatility'
    END as volatility_level,

    -- Market share approximation
    ROUND(
      (total_revenue / SUM(total_revenue) OVER (PARTITION BY month) * 100), 
      3
    ) as market_share_percent,

    -- Customer metrics
    ROUND((total_revenue / unique_customers), 2) as revenue_per_customer,
    ROUND((total_profit / total_revenue * 100), 2) as profit_margin_percent,
    ROUND((new_customers / unique_customers * 100), 2) as new_customer_rate,

    -- Operational efficiency
    ROUND((total_items_sold / order_count), 2) as items_per_order,
    ROUND((total_revenue / total_items_sold), 2) as revenue_per_item

  FROM sales_with_trends
),

-- Advanced customer segmentation with RFM analysis
customer_rfm_analysis AS (
  SELECT 
    customer_id,
    customer_type,
    customer_location.country,
    customer_registration_date,

    -- Recency calculation (days since last order)
    EXTRACT(DAYS FROM (CURRENT_DATE - MAX(order_date))) as recency_days,

    -- Frequency (number of orders)
    COUNT(*) as frequency,

    -- Monetary (total spending)
    SUM(total_amount) as monetary_value,

    -- Additional behavioral metrics
    AVG(total_amount) as avg_order_value,
    MIN(order_date) as first_order_date,
    MAX(order_date) as last_order_date,
    COUNT(DISTINCT product_category) as unique_categories_purchased,
    EXTRACT(DAYS FROM (MAX(order_date) - MIN(order_date))) as customer_lifetime_days,

    -- Purchase patterns
    AVG(EXTRACT(DAYS FROM (order_date - LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)))) as avg_days_between_orders,
    STDDEV(total_amount) as order_value_consistency,

    -- Seasonal analysis
    COUNT(*) FILTER (WHERE EXTRACT(QUARTER FROM order_date) = 1) as q1_orders,
    COUNT(*) FILTER (WHERE EXTRACT(QUARTER FROM order_date) = 2) as q2_orders,
    COUNT(*) FILTER (WHERE EXTRACT(QUARTER FROM order_date) = 3) as q3_orders,
    COUNT(*) FILTER (WHERE EXTRACT(QUARTER FROM order_date) = 4) as q4_orders

  FROM orders o
  JOIN customers c ON o.customer_id = c._id
  WHERE o.status = 'completed'
    AND o.order_date >= CURRENT_DATE - INTERVAL '24 months'
  GROUP BY customer_id, customer_type, customer_location.country, customer_registration_date
),

-- Calculate RFM scores and customer segments
customer_segments AS (
  SELECT 
    *,

    -- RFM score calculations using percentile ranking
    NTILE(5) OVER (ORDER BY recency_days ASC) as recency_score, -- Lower recency is better
    NTILE(5) OVER (ORDER BY frequency DESC) as frequency_score, -- Higher frequency is better  
    NTILE(5) OVER (ORDER BY monetary_value DESC) as monetary_score, -- Higher monetary is better

    -- Customer lifetime value prediction
    CASE 
      WHEN avg_days_between_orders > 0 THEN
        ROUND(
          (avg_order_value * (365.0 / avg_days_between_orders) * 3), -- 3 year projection
          2
        )
      ELSE monetary_value
    END as predicted_lifetime_value,

    -- Churn risk assessment
    CASE 
      WHEN recency_days > 180 THEN 'high_risk'
      WHEN recency_days > 90 THEN 'medium_risk'
      WHEN recency_days > 30 THEN 'low_risk'
      ELSE 'active'
    END as churn_risk,

    -- Engagement level
    CASE 
      WHEN frequency >= 10 AND recency_days <= 30 THEN 'highly_engaged'
      WHEN frequency >= 5 AND recency_days <= 60 THEN 'moderately_engaged'
      WHEN frequency >= 2 AND recency_days <= 120 THEN 'lightly_engaged'
      ELSE 'disengaged'
    END as engagement_level,

    -- Purchase diversity
    CASE 
      WHEN unique_categories_purchased >= 5 THEN 'diverse_buyer'
      WHEN unique_categories_purchased >= 3 THEN 'selective_buyer'
      ELSE 'focused_buyer'
    END as purchase_diversity

  FROM customer_rfm_analysis
),

-- Final customer classification
customer_classification AS (
  SELECT 
    *,

    -- RFM segment classification
    CASE 
      WHEN recency_score >= 4 AND frequency_score >= 4 AND monetary_score >= 4 THEN 'champions'
      WHEN recency_score >= 2 AND frequency_score >= 3 AND monetary_score >= 3 THEN 'loyal_customers'
      WHEN recency_score >= 3 AND frequency_score <= 3 AND monetary_score <= 3 THEN 'potential_loyalists'
      WHEN recency_score >= 4 AND frequency_score <= 1 THEN 'new_customers'
      WHEN recency_score >= 3 AND frequency_score <= 1 AND monetary_score <= 2 THEN 'promising'
      WHEN recency_score <= 2 AND frequency_score >= 2 AND monetary_score >= 2 THEN 'need_attention'
      WHEN recency_score <= 2 AND frequency_score <= 2 AND monetary_score >= 3 THEN 'about_to_sleep'
      WHEN recency_score <= 2 AND frequency_score <= 2 AND monetary_score <= 2 THEN 'at_risk'
      WHEN recency_score <= 1 AND frequency_score <= 2 AND monetary_score >= 4 THEN 'cannot_lose_them'
      ELSE 'hibernating'
    END as rfm_segment,

    -- Marketing action recommendations
    CASE 
      WHEN recency_score >= 4 AND frequency_score >= 4 THEN 'Reward with loyalty program and exclusive offers'
      WHEN monetary_score >= 4 AND recency_score <= 2 THEN 'Win-back campaign with premium incentives'
      WHEN recency_score >= 4 AND frequency_score <= 2 THEN 'Nurture with educational content and onboarding'
      WHEN frequency_score >= 3 AND recency_days > 60 THEN 'Re-engagement campaign with personalized offers'
      WHEN churn_risk = 'high_risk' AND monetary_score >= 3 THEN 'Urgent retention campaign'
      ELSE 'Monitor and maintain regular communication'
    END as marketing_recommendation

  FROM customer_segments
),

-- Product performance analysis with advanced metrics
product_performance AS (
  SELECT 
    p._id as product_id,
    p.name as product_name,
    p.category,
    p.brand,
    p.pricing.cost,
    p.pricing.retail,

    -- Sales metrics from orders
    COALESCE(sales.total_units_sold, 0) as total_units_sold,
    COALESCE(sales.total_revenue, 0) as total_revenue,
    COALESCE(sales.total_orders, 0) as total_orders,
    COALESCE(sales.unique_customers, 0) as unique_customers,
    COALESCE(sales.avg_selling_price, p.pricing.retail) as avg_selling_price,

    -- Profitability analysis
    COALESCE(sales.total_profit, 0) as total_profit,
    CASE 
      WHEN COALESCE(sales.total_revenue, 0) > 0 THEN
        ROUND((COALESCE(sales.total_profit, 0) / sales.total_revenue * 100), 2)
      ELSE 0
    END as profit_margin_percent,

    -- Performance indicators
    CASE 
      WHEN COALESCE(sales.total_revenue, 0) = 0 THEN 'no_sales'
      WHEN sales.first_sale_date >= CURRENT_DATE - INTERVAL '90 days' THEN 'new_product'
      WHEN sales.total_revenue >= 50000 AND sales.total_profit / sales.total_revenue >= 0.2 THEN 'star'
      WHEN sales.total_revenue >= 10000 AND sales.total_profit / sales.total_revenue >= 0.15 THEN 'promising'
      WHEN sales.last_sale_date < CURRENT_DATE - INTERVAL '60 days' THEN 'declining'
      ELSE 'stable'
    END as performance_category,

    -- Inventory analysis
    p.inventory.quantity as current_stock,
    CASE 
      WHEN COALESCE(sales.total_units_sold, 0) > 0 AND p.inventory.quantity > 0 THEN
        ROUND((sales.total_units_sold / p.inventory.quantity), 2)
      ELSE 0
    END as inventory_turnover,

    -- Market position
    sales.category_rank,
    sales.category_market_share,

    -- Time-based metrics
    sales.first_sale_date,
    sales.last_sale_date,
    sales.sales_trend

  FROM products p
  LEFT JOIN (
    SELECT 
      item.product_id,
      COUNT(DISTINCT o.order_id) as total_orders,
      SUM(item.quantity) as total_units_sold,
      SUM(item.quantity * item.unit_price) as total_revenue,
      COUNT(DISTINCT o.customer_id) as unique_customers,
      AVG(item.unit_price) as avg_selling_price,
      MIN(o.order_date) as first_sale_date,
      MAX(o.order_date) as last_sale_date,
      SUM(item.quantity * (item.unit_price - p.pricing.cost)) as total_profit,

      -- Category ranking
      RANK() OVER (PARTITION BY p.category ORDER BY SUM(item.quantity * item.unit_price) DESC) as category_rank,

      -- Market share within category
      ROUND(
        (SUM(item.quantity * item.unit_price) / 
         SUM(SUM(item.quantity * item.unit_price)) OVER (PARTITION BY p.category) * 100),
        2
      ) as category_market_share,

      -- Sales trend analysis
      CASE 
        WHEN COUNT(*) FILTER (WHERE o.order_date >= CURRENT_DATE - INTERVAL '90 days') >
             COUNT(*) FILTER (WHERE o.order_date BETWEEN CURRENT_DATE - INTERVAL '180 days' AND CURRENT_DATE - INTERVAL '90 days') 
        THEN 'growing'
        WHEN COUNT(*) FILTER (WHERE o.order_date >= CURRENT_DATE - INTERVAL '90 days') <
             COUNT(*) FILTER (WHERE o.order_date BETWEEN CURRENT_DATE - INTERVAL '180 days' AND CURRENT_DATE - INTERVAL '90 days') * 0.8
        THEN 'declining'  
        ELSE 'stable'
      END as sales_trend

    FROM orders o
    CROSS JOIN UNNEST(o.items) as item
    JOIN products p ON item.product_id = p._id
    WHERE o.status = 'completed'
      AND o.order_date >= CURRENT_DATE - INTERVAL '12 months'
    GROUP BY item.product_id, p.category, p.pricing.cost
  ) sales ON p._id = sales.product_id
)

-- Final consolidated analytics report
SELECT 
  'EXECUTIVE_SUMMARY' as report_section,

  -- Overall business performance
  (SELECT COUNT(*) FROM performance_metrics WHERE month >= CURRENT_DATE - INTERVAL '1 month') as current_month_segments,
  (SELECT ROUND(AVG(total_revenue), 2) FROM performance_metrics WHERE month >= CURRENT_DATE - INTERVAL '1 month') as avg_monthly_revenue,
  (SELECT ROUND(AVG(month_over_month_growth), 2) FROM performance_metrics WHERE month_over_month_growth IS NOT NULL) as avg_growth_rate,

  -- Customer insights
  (SELECT COUNT(*) FROM customer_classification WHERE rfm_segment = 'champions') as champion_customers,
  (SELECT COUNT(*) FROM customer_classification WHERE churn_risk = 'high_risk') as high_risk_customers,
  (SELECT ROUND(AVG(predicted_lifetime_value), 2) FROM customer_classification) as avg_customer_lifetime_value,

  -- Product insights
  (SELECT COUNT(*) FROM product_performance WHERE performance_category = 'star') as star_products,
  (SELECT COUNT(*) FROM product_performance WHERE performance_category = 'declining') as declining_products,
  (SELECT ROUND(AVG(profit_margin_percent), 2) FROM product_performance WHERE total_revenue > 0) as avg_profit_margin,

  -- Strategic recommendations
  CASE 
    WHEN (SELECT AVG(month_over_month_growth) FROM performance_metrics WHERE month_over_month_growth IS NOT NULL) < -10 
    THEN 'URGENT: Implement revenue recovery strategy'
    WHEN (SELECT COUNT(*) FROM customer_classification WHERE churn_risk = 'high_risk') > 
         (SELECT COUNT(*) FROM customer_classification WHERE rfm_segment = 'champions')
    THEN 'FOCUS: Customer retention and re-engagement programs'
    WHEN (SELECT COUNT(*) FROM product_performance WHERE performance_category = 'star') < 5
    THEN 'OPPORTUNITY: Invest in product development and innovation'
    ELSE 'MAINTAIN: Continue current strategies with incremental improvements'
  END as primary_strategic_recommendation

UNION ALL

-- Performance trends
SELECT 
  'PERFORMANCE_TRENDS',
  month::text,
  product_category,
  customer_location.country,
  total_revenue,
  month_over_month_growth,
  trend_classification,
  performance_category,
  market_share_percent
FROM performance_metrics
WHERE month >= CURRENT_DATE - INTERVAL '6 months'
ORDER BY month DESC, total_revenue DESC
LIMIT 20

UNION ALL

-- Top customer segments  
SELECT 
  'CUSTOMER_SEGMENTS',
  rfm_segment,
  COUNT(*)::text as customer_count,
  ROUND(AVG(monetary_value), 2)::text as avg_lifetime_value,
  churn_risk,
  engagement_level,
  marketing_recommendation
FROM customer_classification  
GROUP BY rfm_segment, churn_risk, engagement_level, marketing_recommendation
ORDER BY AVG(monetary_value) DESC
LIMIT 15

UNION ALL

-- Product performance summary
SELECT 
  'PRODUCT_PERFORMANCE',
  product_name,
  category,
  total_revenue::text,
  profit_margin_percent::text,
  performance_category,
  inventory_turnover::text,
  sales_trend,
  CASE category_rank WHEN 1 THEN 'Category Leader' ELSE category_rank::text END
FROM product_performance
WHERE total_revenue > 0
ORDER BY total_revenue DESC
LIMIT 25;

-- QueryLeaf provides comprehensive aggregation capabilities:
-- 1. SQL-familiar window functions with OVER clauses and frame specifications  
-- 2. Advanced statistical functions including percentiles, standard deviation, and ranking
-- 3. Complex GROUP BY operations with ROLLUP, CUBE, and GROUPING SETS support
-- 4. Sophisticated JOIN operations including LATERAL joins for nested processing
-- 5. CTEs (Common Table Expressions) for complex multi-stage analytical queries  
-- 6. CASE expressions and conditional logic for business rule implementation
-- 7. Date/time functions for temporal analysis and time-series processing
-- 8. String and array functions for text processing and data transformation
-- 9. JSON processing functions for nested document analysis and extraction
-- 10. Integration with MongoDB's native aggregation optimizations and indexing

Best Practices for MongoDB Aggregation Implementation

Pipeline Design Principles

Essential guidelines for effective aggregation pipeline construction:

  1. Early Filtering: Place $match stages early to reduce data volume through the pipeline
  2. Index Utilization: Design pipelines to leverage existing indexes for optimal performance
  3. Stage Ordering: Order stages to minimize computational overhead and data transfer
  4. Memory Management: Monitor memory usage and use allowDiskUse for large datasets
  5. Field Projection: Use $project to limit fields and reduce document size early
  6. Pipeline Caching: Cache frequently-used aggregation results for improved performance

Performance Optimization Strategies

Optimize MongoDB aggregation pipelines for production workloads:

  1. Compound Indexes: Create indexes that support multiple pipeline stages
  2. Covered Queries: Design pipelines that can be satisfied entirely from indexes
  3. Parallel Processing: Use multiple concurrent pipelines for independent analyses
  4. Result Caching: Implement intelligent caching for expensive aggregations
  5. Incremental Updates: Process only new/changed data for time-series analytics
  6. Resource Monitoring: Track aggregation performance and optimize accordingly

Conclusion

MongoDB's Aggregation Framework provides comprehensive real-time analytics capabilities that eliminate the need for separate ETL processes, data warehouses, and batch processing systems. The powerful pipeline architecture enables sophisticated data transformations, statistical analysis, and predictive modeling directly within the operational database, delivering immediate insights while maintaining high performance at scale.

Key MongoDB Aggregation Framework benefits include:

  • Real-Time Processing: Immediate analytical results without data movement or batch delays
  • Advanced Analytics: Comprehensive statistical functions, window operations, and machine learning integration
  • Flexible Pipelines: Multi-stage transformations that adapt to evolving analytical requirements
  • Scalable Performance: High-performance processing that scales with data volume and complexity
  • SQL Compatibility: Familiar analytical operations through QueryLeaf's SQL interface
  • Integrated Architecture: Seamless integration with operational workloads and existing applications

Whether you're building real-time dashboards, customer analytics platforms, financial reporting systems, or any application requiring sophisticated data analysis, MongoDB's Aggregation Framework with QueryLeaf's familiar SQL interface provides the foundation for powerful, maintainable analytical solutions.

QueryLeaf Integration: QueryLeaf automatically optimizes MongoDB aggregation operations while providing SQL-familiar analytics syntax, window functions, and statistical operations. Complex analytical queries, predictive models, and real-time insights are seamlessly handled through familiar SQL constructs, making advanced data processing both powerful and accessible to SQL-oriented development teams.

The combination of MongoDB's native aggregation capabilities with SQL-style analytics operations makes MongoDB an ideal platform for applications requiring both operational efficiency and analytical sophistication, ensuring your applications can deliver immediate insights while maintaining optimal performance as data volumes and complexity grow.

MongoDB Connection Pooling Optimization Strategies: Advanced Connection Management and Performance Tuning for High-Throughput Applications

High-throughput database applications require sophisticated connection management strategies and comprehensive pooling optimization techniques that can handle concurrent request patterns, varying workload demands, and complex scaling requirements while maintaining optimal performance and resource utilization. Traditional database connection approaches often struggle with dynamic scaling, connection overhead management, and the complexity of balancing connection availability with resource consumption, leading to performance bottlenecks, resource exhaustion, and operational challenges in production environments.

MongoDB provides comprehensive connection pooling capabilities through intelligent connection management, sophisticated monitoring features, and optimized driver implementations that enable applications to achieve maximum throughput with minimal connection overhead. Unlike traditional databases that require manual connection tuning procedures and complex pooling configuration, MongoDB drivers integrate advanced pooling algorithms directly with automatic connection scaling, real-time performance monitoring, and intelligent connection lifecycle management.

The Traditional Connection Management Challenge

Conventional approaches to database connection management in enterprise applications face significant limitations in scalability and resource optimization:

-- Traditional PostgreSQL connection management - manual pooling with limited optimization capabilities

-- Basic connection tracking table with minimal functionality
CREATE TABLE connection_pool_stats (
    pool_id SERIAL PRIMARY KEY,
    application_name VARCHAR(100) NOT NULL,
    database_name VARCHAR(100) NOT NULL,
    host_address VARCHAR(255) NOT NULL,
    port_number INTEGER DEFAULT 5432,

    -- Basic pool configuration (static settings)
    min_connections INTEGER DEFAULT 5,
    max_connections INTEGER DEFAULT 20,
    connection_timeout INTEGER DEFAULT 30,
    idle_timeout INTEGER DEFAULT 600,

    -- Simple usage statistics (very limited visibility)
    active_connections INTEGER DEFAULT 0,
    idle_connections INTEGER DEFAULT 0,
    total_connections_created BIGINT DEFAULT 0,
    total_connections_destroyed BIGINT DEFAULT 0,

    -- Basic performance metrics
    avg_connection_wait_time DECIMAL(8,3),
    max_connection_wait_time DECIMAL(8,3),
    connection_failures BIGINT DEFAULT 0,

    -- Manual tracking timestamps
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_analyzed TIMESTAMP
);

-- Query execution tracking table (basic functionality)
CREATE TABLE query_execution_log (
    execution_id SERIAL PRIMARY KEY,
    pool_id INTEGER REFERENCES connection_pool_stats(pool_id),
    session_id VARCHAR(100),

    -- Query identification
    query_hash VARCHAR(64),
    query_type VARCHAR(50), -- SELECT, INSERT, UPDATE, DELETE
    query_text TEXT, -- Usually truncated for storage

    -- Basic timing information
    start_time TIMESTAMP NOT NULL,
    end_time TIMESTAMP,
    execution_duration DECIMAL(10,3),

    -- Connection usage
    connection_acquired_at TIMESTAMP,
    connection_released_at TIMESTAMP,
    connection_wait_time DECIMAL(8,3),

    -- Simple result metrics
    rows_affected INTEGER,
    rows_returned INTEGER,

    -- Status tracking
    execution_status VARCHAR(20), -- success, error, timeout
    error_message TEXT,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Manual connection pool configuration (static and inflexible)
CREATE TABLE pool_configuration (
    config_id SERIAL PRIMARY KEY,
    pool_name VARCHAR(100) UNIQUE NOT NULL,

    -- Static configuration parameters
    initial_pool_size INTEGER DEFAULT 5,
    maximum_pool_size INTEGER DEFAULT 50,
    minimum_idle_connections INTEGER DEFAULT 2,

    -- Timeout settings (fixed values)
    connection_timeout_seconds INTEGER DEFAULT 30,
    idle_connection_timeout_seconds INTEGER DEFAULT 1800,
    validation_timeout_seconds INTEGER DEFAULT 5,

    -- Simple retry configuration
    max_retry_attempts INTEGER DEFAULT 3,
    retry_delay_seconds INTEGER DEFAULT 1,

    -- Basic health check
    validation_query VARCHAR(500) DEFAULT 'SELECT 1',
    validate_on_borrow BOOLEAN DEFAULT true,
    validate_on_return BOOLEAN DEFAULT false,

    -- Manual maintenance
    test_while_idle BOOLEAN DEFAULT true,
    time_between_eviction_runs INTEGER DEFAULT 300,
    num_tests_per_eviction_run INTEGER DEFAULT 3,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Complex query to analyze connection performance (expensive and limited)
WITH connection_performance AS (
    SELECT 
        cps.application_name,
        cps.database_name,
        cps.host_address,

        -- Basic pool utilization
        CASE 
            WHEN cps.max_connections > 0 THEN 
                (cps.active_connections::DECIMAL / cps.max_connections) * 100
            ELSE 0 
        END as pool_utilization_percent,

        -- Simple connection metrics
        cps.active_connections,
        cps.idle_connections,
        cps.total_connections_created,
        cps.total_connections_destroyed,

        -- Basic performance statistics
        cps.avg_connection_wait_time,
        cps.max_connection_wait_time,
        cps.connection_failures,

        -- Query performance (limited aggregation)
        COUNT(qel.execution_id) as total_queries_24h,
        AVG(qel.execution_duration) as avg_query_duration,
        AVG(qel.connection_wait_time) as avg_connection_wait,

        -- Simple error tracking
        COUNT(CASE WHEN qel.execution_status = 'error' THEN 1 END) as error_count,
        COUNT(CASE WHEN qel.execution_status = 'timeout' THEN 1 END) as timeout_count,

        -- Basic connection efficiency (limited insights)
        CASE 
            WHEN COUNT(qel.execution_id) > 0 THEN
                COUNT(CASE WHEN qel.connection_wait_time < 0.100 THEN 1 END)::DECIMAL / 
                COUNT(qel.execution_id) * 100
            ELSE 0
        END as fast_connection_percentage

    FROM connection_pool_stats cps
    LEFT JOIN query_execution_log qel ON cps.pool_id = qel.pool_id
        AND qel.start_time >= CURRENT_TIMESTAMP - INTERVAL '24 hours'

    WHERE cps.last_updated >= CURRENT_TIMESTAMP - INTERVAL '1 hour'

    GROUP BY cps.pool_id, cps.application_name, cps.database_name, 
             cps.host_address, cps.active_connections, cps.idle_connections,
             cps.max_connections, cps.total_connections_created, 
             cps.total_connections_destroyed, cps.avg_connection_wait_time,
             cps.max_connection_wait_time, cps.connection_failures
),

pool_health_analysis AS (
    SELECT *,
        -- Simple health scoring (limited factors)
        CASE 
            WHEN pool_utilization_percent > 90 THEN 'Critical'
            WHEN pool_utilization_percent > 75 THEN 'Warning'
            WHEN avg_connection_wait > 1.0 THEN 'Warning'
            WHEN error_count > total_queries_24h * 0.05 THEN 'Warning'
            ELSE 'Healthy'
        END as pool_health_status,

        -- Basic recommendation logic (very limited)
        CASE 
            WHEN pool_utilization_percent > 85 THEN 
                'Consider increasing max_connections'
            WHEN avg_connection_wait > 0.5 THEN 
                'Review connection timeout settings'
            WHEN error_count > 10 THEN 
                'Investigate connection failures'
            ELSE 'Pool configuration appears adequate'
        END as basic_recommendation

    FROM connection_performance
)

SELECT 
    application_name,
    database_name,
    host_address,

    -- Pool status overview
    pool_utilization_percent,
    pool_health_status,
    active_connections,
    idle_connections,

    -- Performance metrics
    total_queries_24h,
    avg_query_duration,
    avg_connection_wait,
    fast_connection_percentage,

    -- Error tracking
    error_count,
    timeout_count,

    -- Basic recommendations
    basic_recommendation,

    CURRENT_TIMESTAMP as analysis_timestamp

FROM pool_health_analysis
ORDER BY 
    CASE pool_health_status
        WHEN 'Critical' THEN 1
        WHEN 'Warning' THEN 2
        ELSE 3
    END,
    pool_utilization_percent DESC;

-- Problems with traditional connection pooling approach:
-- 1. Static configuration cannot adapt to changing workloads
-- 2. Limited visibility into connection lifecycle and performance
-- 3. Manual tuning required for optimal performance
-- 4. No automatic scaling based on demand patterns
-- 5. Basic health checking with limited diagnostic capabilities
-- 6. Inefficient connection distribution across database instances
-- 7. No built-in monitoring for connection pool performance
-- 8. Difficult to troubleshoot connection-related performance issues
-- 9. Limited integration with application performance monitoring
-- 10. Manual intervention required for pool optimization

MongoDB's intelligent connection pooling eliminates these limitations:

// MongoDB optimized connection pooling - intelligent and performance-focused
// Advanced connection management with automatic optimization

const { MongoClient } = require('mongodb');

// Comprehensive connection pool configuration
class MongoConnectionPoolManager {
  constructor(connectionUri, options = {}) {
    this.connectionUri = connectionUri;
    this.poolOptions = {
      // Intelligent pool sizing
      minPoolSize: options.minPoolSize || 5,
      maxPoolSize: options.maxPoolSize || 100,
      maxIdleTimeMS: options.maxIdleTimeMS || 30000,

      // Advanced connection management
      waitQueueTimeoutMS: options.waitQueueTimeoutMS || 2500,
      serverSelectionTimeoutMS: options.serverSelectionTimeoutMS || 5000,
      socketTimeoutMS: options.socketTimeoutMS || 45000,
      connectTimeoutMS: options.connectTimeoutMS || 10000,

      // Intelligent retry logic
      retryWrites: true,
      retryReads: true,
      maxStalenessSeconds: options.maxStalenessSeconds || 90,

      // Advanced monitoring capabilities
      monitorCommands: true,

      // Intelligent load balancing
      loadBalanced: options.loadBalanced || false,

      // Connection compression
      compressors: options.compressors || ['snappy', 'zlib'],

      // SSL/TLS optimization
      ssl: options.ssl || true,
      sslValidate: options.sslValidate || true,

      // Advanced read preferences
      readPreference: options.readPreference || 'secondaryPreferred',
      readConcern: { level: options.readConcernLevel || 'majority' },

      // Write concern optimization
      writeConcern: {
        w: options.writeConcernW || 'majority',
        j: options.writeConcernJ || true,
        wtimeout: options.writeConcernTimeout || 5000
      }
    };

    this.client = null;
    this.connectionMetrics = new Map();
    this.performanceStats = {
      totalConnections: 0,
      activeConnections: 0,
      connectionWaitTimes: [],
      queryExecutionTimes: [],
      connectionErrors: 0,
      poolHealthScore: 100
    };
  }

  async initializeConnectionPool() {
    try {
      console.log('Initializing MongoDB connection pool with intelligent optimization...');

      // Create client with advanced pooling options
      this.client = new MongoClient(this.connectionUri, this.poolOptions);

      // Set up comprehensive event listeners for monitoring
      this.setupConnectionMonitoring();

      // Connect with retry logic and health checking
      await this.connectWithHealthCheck();

      // Initialize performance monitoring
      this.startPerformanceMonitoring();

      // Setup automatic pool optimization
      this.setupAutomaticOptimization();

      console.log('MongoDB connection pool initialized successfully');
      return this.client;

    } catch (error) {
      console.error('Failed to initialize MongoDB connection pool:', error);
      throw error;
    }
  }

  setupConnectionMonitoring() {
    // Connection pool monitoring events
    this.client.on('connectionPoolCreated', (event) => {
      console.log(`Connection pool created: ${event.address}`);
      this.logPoolEvent('pool_created', event);
    });

    this.client.on('connectionPoolReady', (event) => {
      console.log(`Connection pool ready: ${event.address}`);
      this.logPoolEvent('pool_ready', event);
    });

    this.client.on('connectionCreated', (event) => {
      this.performanceStats.totalConnections++;
      this.logPoolEvent('connection_created', event);
    });

    this.client.on('connectionReady', (event) => {
      this.performanceStats.activeConnections++;
      this.logPoolEvent('connection_ready', event);
    });

    this.client.on('connectionClosed', (event) => {
      this.performanceStats.activeConnections = Math.max(0, this.performanceStats.activeConnections - 1);
      this.logPoolEvent('connection_closed', event);
    });

    this.client.on('connectionCheckOutStarted', (event) => {
      event.startTime = Date.now();
      this.logPoolEvent('checkout_started', event);
    });

    this.client.on('connectionCheckedOut', (event) => {
      const waitTime = event.startTime ? Date.now() - event.startTime : 0;
      this.performanceStats.connectionWaitTimes.push(waitTime);
      this.logPoolEvent('connection_checked_out', { ...event, waitTime });
    });

    this.client.on('connectionCheckedIn', (event) => {
      this.logPoolEvent('connection_checked_in', event);
    });

    // Command monitoring for performance analysis
    this.client.on('commandStarted', (event) => {
      event.startTime = Date.now();
      this.logCommandEvent('command_started', event);
    });

    this.client.on('commandSucceeded', (event) => {
      const executionTime = event.startTime ? Date.now() - event.startTime : 0;
      this.performanceStats.queryExecutionTimes.push(executionTime);
      this.logCommandEvent('command_succeeded', { ...event, executionTime });
    });

    this.client.on('commandFailed', (event) => {
      this.performanceStats.connectionErrors++;
      const executionTime = event.startTime ? Date.now() - event.startTime : 0;
      this.logCommandEvent('command_failed', { ...event, executionTime });
    });

    // Server monitoring for intelligent scaling
    this.client.on('serverHeartbeatStarted', (event) => {
      this.logServerEvent('heartbeat_started', event);
    });

    this.client.on('serverHeartbeatSucceeded', (event) => {
      this.logServerEvent('heartbeat_succeeded', event);
    });

    this.client.on('serverHeartbeatFailed', (event) => {
      this.logServerEvent('heartbeat_failed', event);
    });
  }

  async connectWithHealthCheck() {
    const maxRetries = 3;
    let retryCount = 0;

    while (retryCount < maxRetries) {
      try {
        await this.client.connect();

        // Perform health check
        const healthCheck = await this.performHealthCheck();
        if (healthCheck.healthy) {
          console.log('Connection pool health check passed');
          return;
        } else {
          throw new Error(`Health check failed: ${healthCheck.issues.join(', ')}`);
        }

      } catch (error) {
        retryCount++;
        console.error(`Connection attempt ${retryCount} failed:`, error.message);

        if (retryCount >= maxRetries) {
          throw error;
        }

        // Exponential backoff
        await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 1000));
      }
    }
  }

  async performHealthCheck() {
    try {
      // Test basic connectivity
      const admin = this.client.db('admin');
      const pingResult = await admin.command({ ping: 1 });

      // Test read operations
      const testDb = this.client.db('test');
      await testDb.collection('healthcheck').findOne({}, { maxTimeMS: 5000 });

      // Check connection pool stats
      const poolStats = await this.getConnectionPoolStats();

      const issues = [];

      // Analyze pool health
      if (poolStats.availableConnections < 2) {
        issues.push('Low available connections');
      }

      if (poolStats.averageWaitTime > 1000) {
        issues.push('High average connection wait time');
      }

      if (poolStats.errorRate > 0.05) {
        issues.push('High error rate detected');
      }

      return {
        healthy: issues.length === 0,
        issues: issues,
        timestamp: new Date(),
        poolStats: poolStats
      };

    } catch (error) {
      return {
        healthy: false,
        issues: [`Health check error: ${error.message}`],
        timestamp: new Date()
      };
    }
  }

  startPerformanceMonitoring() {
    // Real-time performance monitoring
    setInterval(async () => {
      try {
        const stats = await this.getDetailedPerformanceStats();
        this.analyzePerformanceTrends(stats);
        this.updatePoolHealthScore(stats);

        // Log performance summary
        console.log(`Pool Performance - Health: ${this.performanceStats.poolHealthScore}%, ` +
                   `Active: ${stats.activeConnections}, ` +
                   `Avg Wait: ${stats.averageWaitTime}ms, ` +
                   `Avg Query: ${stats.averageQueryTime}ms`);

      } catch (error) {
        console.error('Performance monitoring error:', error);
      }
    }, 30000); // Every 30 seconds
  }

  async getDetailedPerformanceStats() {
    const now = Date.now();
    const fiveMinutesAgo = now - (5 * 60 * 1000);

    // Filter recent metrics
    const recentWaitTimes = this.performanceStats.connectionWaitTimes
      .filter(time => time.timestamp > fiveMinutesAgo);
    const recentQueryTimes = this.performanceStats.queryExecutionTimes
      .filter(time => time.timestamp > fiveMinutesAgo);

    const stats = {
      timestamp: now,
      totalConnections: this.performanceStats.totalConnections,
      activeConnections: this.performanceStats.activeConnections,

      // Connection timing analysis
      averageWaitTime: this.calculateAverage(recentWaitTimes.map(t => t.value)),
      maxWaitTime: Math.max(...(recentWaitTimes.map(t => t.value) || [0])),
      p95WaitTime: this.calculatePercentile(recentWaitTimes.map(t => t.value), 0.95),

      // Query performance analysis
      averageQueryTime: this.calculateAverage(recentQueryTimes.map(t => t.value)),
      maxQueryTime: Math.max(...(recentQueryTimes.map(t => t.value) || [0])),
      p95QueryTime: this.calculatePercentile(recentQueryTimes.map(t => t.value), 0.95),

      // Error analysis
      errorRate: this.calculateErrorRate(fiveMinutesAgo),
      connectionErrors: this.performanceStats.connectionErrors,

      // Pool utilization
      poolUtilization: (this.performanceStats.activeConnections / this.poolOptions.maxPoolSize) * 100,

      // Connection efficiency
      connectionEfficiency: this.calculateConnectionEfficiency(recentWaitTimes),

      // Server health indicators
      serverHealth: await this.getServerHealthIndicators()
    };

    return stats;
  }

  setupAutomaticOptimization() {
    // Intelligent pool optimization based on performance metrics
    setInterval(async () => {
      try {
        const stats = await this.getDetailedPerformanceStats();
        const optimizations = this.generateOptimizationRecommendations(stats);

        if (optimizations.length > 0) {
          console.log('Applying automatic optimizations:', optimizations);
          await this.applyOptimizations(optimizations);
        }

      } catch (error) {
        console.error('Automatic optimization error:', error);
      }
    }, 300000); // Every 5 minutes
  }

  generateOptimizationRecommendations(stats) {
    const recommendations = [];

    // High utilization optimization
    if (stats.poolUtilization > 85) {
      recommendations.push({
        type: 'increase_pool_size',
        current: this.poolOptions.maxPoolSize,
        recommended: Math.min(this.poolOptions.maxPoolSize * 1.2, 200),
        reason: 'High pool utilization detected'
      });
    }

    // High wait time optimization
    if (stats.averageWaitTime > 500) {
      recommendations.push({
        type: 'reduce_idle_timeout',
        current: this.poolOptions.maxIdleTimeMS,
        recommended: Math.max(this.poolOptions.maxIdleTimeMS * 0.8, 10000),
        reason: 'High connection wait times detected'
      });
    }

    // Low utilization optimization
    if (stats.poolUtilization < 20 && this.poolOptions.maxPoolSize > 20) {
      recommendations.push({
        type: 'decrease_pool_size',
        current: this.poolOptions.maxPoolSize,
        recommended: Math.max(this.poolOptions.maxPoolSize * 0.8, 10),
        reason: 'Low pool utilization detected'
      });
    }

    // Error rate optimization
    if (stats.errorRate > 0.05) {
      recommendations.push({
        type: 'increase_timeout',
        current: this.poolOptions.serverSelectionTimeoutMS,
        recommended: this.poolOptions.serverSelectionTimeoutMS * 1.5,
        reason: 'High error rate suggests timeout issues'
      });
    }

    return recommendations;
  }

  async applyOptimizations(optimizations) {
    for (const optimization of optimizations) {
      try {
        switch (optimization.type) {
          case 'increase_pool_size':
            // Note: Pool size changes require connection recreation
            console.log(`Recommending pool size increase from ${optimization.current} to ${optimization.recommended}`);
            break;

          case 'decrease_pool_size':
            console.log(`Recommending pool size decrease from ${optimization.current} to ${optimization.recommended}`);
            break;

          case 'reduce_idle_timeout':
            console.log(`Recommending idle timeout reduction from ${optimization.current} to ${optimization.recommended}`);
            break;

          case 'increase_timeout':
            console.log(`Recommending timeout increase from ${optimization.current} to ${optimization.recommended}`);
            break;
        }

        // Log optimization for operational tracking
        this.logOptimization(optimization);

      } catch (error) {
        console.error(`Failed to apply optimization ${optimization.type}:`, error);
      }
    }
  }

  async getConnectionPoolStats() {
    return {
      totalConnections: this.performanceStats.totalConnections,
      activeConnections: this.performanceStats.activeConnections,
      availableConnections: this.poolOptions.maxPoolSize - this.performanceStats.activeConnections,
      maxPoolSize: this.poolOptions.maxPoolSize,
      minPoolSize: this.poolOptions.minPoolSize,

      // Recent performance metrics
      averageWaitTime: this.calculateAverage(
        this.performanceStats.connectionWaitTimes
          .slice(-100)
          .map(t => t.value || t)
      ),

      averageQueryTime: this.calculateAverage(
        this.performanceStats.queryExecutionTimes
          .slice(-100)
          .map(t => t.value || t)
      ),

      errorRate: this.calculateErrorRate(Date.now() - (60 * 60 * 1000)), // Last hour
      poolHealthScore: this.performanceStats.poolHealthScore
    };
  }

  // Utility methods for calculations
  calculateAverage(values) {
    if (!values || values.length === 0) return 0;
    return values.reduce((sum, val) => sum + val, 0) / values.length;
  }

  calculatePercentile(values, percentile) {
    if (!values || values.length === 0) return 0;
    const sorted = [...values].sort((a, b) => a - b);
    const index = Math.ceil(sorted.length * percentile) - 1;
    return sorted[index] || 0;
  }

  calculateErrorRate(since) {
    const totalQueries = this.performanceStats.queryExecutionTimes
      .filter(t => t.timestamp > since).length;
    const errors = this.performanceStats.connectionErrors;
    return totalQueries > 0 ? errors / totalQueries : 0;
  }

  calculateConnectionEfficiency(waitTimes) {
    if (!waitTimes || waitTimes.length === 0) return 100;
    const fastConnections = waitTimes.filter(t => t.value < 100).length;
    return (fastConnections / waitTimes.length) * 100;
  }

  async getServerHealthIndicators() {
    try {
      const admin = this.client.db('admin');
      const serverStatus = await admin.command({ serverStatus: 1 });

      return {
        uptime: serverStatus.uptime,
        connections: serverStatus.connections,
        opcounters: serverStatus.opcounters,
        mem: serverStatus.mem,
        globalLock: serverStatus.globalLock
      };
    } catch (error) {
      console.error('Failed to get server health indicators:', error);
      return null;
    }
  }

  updatePoolHealthScore(stats) {
    let score = 100;

    // Penalize high utilization
    if (stats.poolUtilization > 90) score -= 30;
    else if (stats.poolUtilization > 75) score -= 15;

    // Penalize high wait times
    if (stats.averageWaitTime > 1000) score -= 25;
    else if (stats.averageWaitTime > 500) score -= 10;

    // Penalize errors
    if (stats.errorRate > 0.05) score -= 20;
    else if (stats.errorRate > 0.02) score -= 10;

    // Penalize low efficiency
    if (stats.connectionEfficiency < 70) score -= 15;
    else if (stats.connectionEfficiency < 85) score -= 5;

    this.performanceStats.poolHealthScore = Math.max(0, Math.min(100, score));
  }

  logPoolEvent(eventType, event) {
    this.connectionMetrics.set(`${eventType}_${Date.now()}`, {
      type: eventType,
      timestamp: new Date(),
      ...event
    });
  }

  logCommandEvent(eventType, event) {
    // Store command execution metrics
    const timestamp = new Date();

    if (eventType === 'command_succeeded' && event.executionTime !== undefined) {
      this.performanceStats.queryExecutionTimes.push({
        value: event.executionTime,
        timestamp: timestamp.getTime()
      });
    }

    // Keep only recent metrics to prevent memory growth
    if (this.performanceStats.queryExecutionTimes.length > 10000) {
      this.performanceStats.queryExecutionTimes = 
        this.performanceStats.queryExecutionTimes.slice(-5000);
    }
  }

  logServerEvent(eventType, event) {
    // Log server-level events for health monitoring
    console.log(`Server event: ${eventType}`, {
      timestamp: new Date(),
      ...event
    });
  }

  logOptimization(optimization) {
    console.log('Optimization Applied:', {
      timestamp: new Date(),
      ...optimization
    });
  }

  // Graceful shutdown
  async shutdown() {
    console.log('Shutting down MongoDB connection pool...');

    try {
      if (this.client) {
        await this.client.close(true); // Force close all connections
        console.log('MongoDB connection pool shut down successfully');
      }
    } catch (error) {
      console.error('Error during connection pool shutdown:', error);
    }
  }
}

// Example usage with intelligent configuration
async function createOptimizedMongoConnection() {
  const connectionManager = new MongoConnectionPoolManager(
    'mongodb://localhost:27017/production_db',
    {
      // Intelligent pool sizing based on application type
      minPoolSize: 10,           // Minimum connections for baseline performance
      maxPoolSize: 100,          // Maximum connections for peak load
      maxIdleTimeMS: 30000,      // 30 seconds idle timeout

      // Optimized timeouts for production
      waitQueueTimeoutMS: 2500,        // 2.5 seconds max wait for connection
      serverSelectionTimeoutMS: 5000,  // 5 seconds for server selection
      socketTimeoutMS: 45000,          // 45 seconds for socket operations
      connectTimeoutMS: 10000,         // 10 seconds connection timeout

      // Performance optimizations
      compressors: ['snappy', 'zlib'],
      loadBalanced: true,              // Enable load balancing
      readPreference: 'secondaryPreferred',
      readConcernLevel: 'majority',

      // Write concern for consistency
      writeConcernW: 'majority',
      writeConcernJ: true,
      writeConcernTimeout: 5000
    }
  );

  try {
    const client = await connectionManager.initializeConnectionPool();

    // Return both client and manager for full control
    return {
      client,
      manager: connectionManager
    };

  } catch (error) {
    console.error('Failed to create optimized MongoDB connection:', error);
    throw error;
  }
}

// Benefits of MongoDB intelligent connection pooling:
// - Automatic connection scaling based on demand
// - Real-time performance monitoring and optimization
// - Intelligent retry logic with exponential backoff
// - Advanced health checking and diagnostic capabilities
// - Built-in connection efficiency analysis
// - Automatic pool optimization based on performance metrics
// - Comprehensive event tracking for troubleshooting
// - Native integration with MongoDB driver optimizations
// - Load balancing and failover support
// - Zero-downtime connection management

Advanced Connection Pool Optimization Techniques

Strategic connection management patterns for production-grade performance:

// Advanced MongoDB connection pooling patterns for enterprise applications
class EnterpriseConnectionManager {
  constructor() {
    this.connectionPools = new Map();
    this.routingStrategies = new Map();
    this.performanceMetrics = new Map();
    this.healthCheckers = new Map();
  }

  // Multi-tier connection pooling strategy
  async createTieredConnectionPools(configurations) {
    const poolTiers = {
      // High-priority pool for critical operations
      critical: {
        minPoolSize: 15,
        maxPoolSize: 50,
        maxIdleTimeMS: 10000,
        waitQueueTimeoutMS: 1000,
        priority: 'high'
      },

      // Standard pool for regular operations
      standard: {
        minPoolSize: 10,
        maxPoolSize: 75,
        maxIdleTimeMS: 30000,
        waitQueueTimeoutMS: 2500,
        priority: 'normal'
      },

      // Batch pool for background operations
      batch: {
        minPoolSize: 5,
        maxPoolSize: 25,
        maxIdleTimeMS: 60000,
        waitQueueTimeoutMS: 10000,
        priority: 'low'
      },

      // Analytics pool for reporting queries
      analytics: {
        minPoolSize: 3,
        maxPoolSize: 20,
        maxIdleTimeMS: 120000,
        waitQueueTimeoutMS: 30000,
        readPreference: 'secondary',
        priority: 'analytics'
      }
    };

    for (const [tierName, config] of Object.entries(poolTiers)) {
      try {
        const connectionManager = new MongoConnectionPoolManager(
          configurations[tierName]?.uri || configurations.default.uri,
          {
            ...config,
            ...configurations[tierName]
          }
        );

        const poolInfo = await connectionManager.initializeConnectionPool();

        this.connectionPools.set(tierName, {
          manager: connectionManager,
          client: poolInfo,
          config: config,
          createdAt: new Date(),
          lastHealthCheck: null
        });

        console.log(`Initialized ${tierName} connection pool with ${config.maxPoolSize} max connections`);

      } catch (error) {
        console.error(`Failed to create ${tierName} connection pool:`, error);
        throw error;
      }
    }
  }

  // Intelligent connection routing based on operation type
  getConnectionForOperation(operationType, priority = 'normal') {
    const routingRules = {
      'user_query': priority === 'high' ? 'critical' : 'standard',
      'admin_operation': 'critical',
      'bulk_insert': 'batch',
      'bulk_update': 'batch',
      'reporting_query': 'analytics',
      'aggregation': priority === 'high' ? 'standard' : 'analytics',
      'index_operation': 'batch',
      'backup_operation': 'batch',
      'monitoring_query': 'analytics'
    };

    const preferredPool = routingRules[operationType] || 'standard';
    const poolInfo = this.connectionPools.get(preferredPool);

    if (poolInfo && this.isPoolHealthy(preferredPool)) {
      return poolInfo.client;
    }

    // Fallback to standard pool if preferred pool is unavailable
    const fallbackPool = this.connectionPools.get('standard');
    if (fallbackPool && this.isPoolHealthy('standard')) {
      console.warn(`Using fallback pool for ${operationType} (preferred: ${preferredPool})`);
      return fallbackPool.client;
    }

    throw new Error('No healthy connection pools available');
  }

  // Advanced performance monitoring across all pools
  async getComprehensivePerformanceReport() {
    const report = {
      timestamp: new Date(),
      overallHealth: 'unknown',
      pools: {},
      recommendations: [],
      alerts: []
    };

    let totalHealthScore = 0;
    let poolCount = 0;

    for (const [poolName, poolInfo] of this.connectionPools.entries()) {
      try {
        const stats = await poolInfo.manager.getDetailedPerformanceStats();

        report.pools[poolName] = {
          ...stats,
          configuration: poolInfo.config,
          uptime: Date.now() - poolInfo.createdAt.getTime(),
          healthStatus: this.calculatePoolHealth(stats)
        };

        totalHealthScore += stats.poolHealthScore || 0;
        poolCount++;

        // Generate pool-specific recommendations
        const poolRecommendations = this.generatePoolRecommendations(poolName, stats);
        report.recommendations.push(...poolRecommendations);

        // Check for alerts
        const alerts = this.checkForAlerts(poolName, stats);
        report.alerts.push(...alerts);

      } catch (error) {
        report.pools[poolName] = {
          error: error.message,
          healthStatus: 'unhealthy'
        };

        report.alerts.push({
          severity: 'critical',
          pool: poolName,
          message: `Pool health check failed: ${error.message}`
        });
      }
    }

    // Calculate overall health
    report.overallHealth = poolCount > 0 ? 
      (totalHealthScore / poolCount > 80 ? 'healthy' : 
       totalHealthScore / poolCount > 60 ? 'warning' : 'critical') : 'unknown';

    return report;
  }

  isPoolHealthy(poolName) {
    const poolInfo = this.connectionPools.get(poolName);
    if (!poolInfo) return false;

    // Simple health check - can be enhanced with more sophisticated logic
    return poolInfo.manager.performanceStats.poolHealthScore > 50;
  }

  calculatePoolHealth(stats) {
    if (stats.poolHealthScore >= 80) return 'healthy';
    if (stats.poolHealthScore >= 60) return 'warning';
    return 'critical';
  }

  generatePoolRecommendations(poolName, stats) {
    const recommendations = [];

    // High utilization recommendations
    if (stats.poolUtilization > 85) {
      recommendations.push({
        pool: poolName,
        type: 'capacity',
        severity: 'high',
        message: `${poolName} pool utilization is ${stats.poolUtilization.toFixed(1)}% - consider increasing pool size`,
        suggestedAction: `Increase maxPoolSize from ${stats.maxPoolSize} to ${Math.ceil(stats.maxPoolSize * 1.3)}`
      });
    }

    // Performance recommendations
    if (stats.averageWaitTime > 1000) {
      recommendations.push({
        pool: poolName,
        type: 'performance',
        severity: 'medium',
        message: `${poolName} pool has high average wait time: ${stats.averageWaitTime.toFixed(1)}ms`,
        suggestedAction: 'Review connection timeout settings and pool sizing'
      });
    }

    // Error rate recommendations
    if (stats.errorRate > 0.05) {
      recommendations.push({
        pool: poolName,
        type: 'reliability',
        severity: 'high',
        message: `${poolName} pool has high error rate: ${(stats.errorRate * 100).toFixed(1)}%`,
        suggestedAction: 'Investigate connection failures and server health'
      });
    }

    return recommendations;
  }

  checkForAlerts(poolName, stats) {
    const alerts = [];

    // Critical utilization alert
    if (stats.poolUtilization > 95) {
      alerts.push({
        severity: 'critical',
        pool: poolName,
        type: 'utilization',
        message: `${poolName} pool utilization critical: ${stats.poolUtilization.toFixed(1)}%`,
        threshold: 95,
        currentValue: stats.poolUtilization
      });
    }

    // High error rate alert
    if (stats.errorRate > 0.1) {
      alerts.push({
        severity: 'critical',
        pool: poolName,
        type: 'error_rate',
        message: `${poolName} pool error rate critical: ${(stats.errorRate * 100).toFixed(1)}%`,
        threshold: 10,
        currentValue: stats.errorRate * 100
      });
    }

    // Connection timeout alert
    if (stats.p95WaitTime > 5000) {
      alerts.push({
        severity: 'warning',
        pool: poolName,
        type: 'latency',
        message: `${poolName} pool 95th percentile wait time high: ${stats.p95WaitTime.toFixed(1)}ms`,
        threshold: 5000,
        currentValue: stats.p95WaitTime
      });
    }

    return alerts;
  }

  // Automatic pool rebalancing based on usage patterns
  async rebalanceConnectionPools() {
    console.log('Starting automatic pool rebalancing...');

    const report = await this.getComprehensivePerformanceReport();

    for (const [poolName, poolStats] of Object.entries(report.pools)) {
      if (poolStats.error) continue;

      const rebalanceActions = this.calculateRebalanceActions(poolName, poolStats);

      for (const action of rebalanceActions) {
        await this.executeRebalanceAction(poolName, action);
      }
    }

    console.log('Pool rebalancing completed');
  }

  calculateRebalanceActions(poolName, stats) {
    const actions = [];

    // Pool size adjustments
    if (stats.poolUtilization > 80 && stats.maxPoolSize < 200) {
      actions.push({
        type: 'increase_pool_size',
        currentSize: stats.maxPoolSize,
        newSize: Math.min(Math.ceil(stats.maxPoolSize * 1.2), 200),
        reason: 'High utilization'
      });
    } else if (stats.poolUtilization < 30 && stats.maxPoolSize > 10) {
      actions.push({
        type: 'decrease_pool_size',
        currentSize: stats.maxPoolSize,
        newSize: Math.max(Math.ceil(stats.maxPoolSize * 0.8), 10),
        reason: 'Low utilization'
      });
    }

    return actions;
  }

  async executeRebalanceAction(poolName, action) {
    console.log(`Executing rebalance action for ${poolName}:`, action);

    // Note: Actual implementation would require careful coordination
    // to avoid disrupting active connections
    switch (action.type) {
      case 'increase_pool_size':
        console.log(`Would increase ${poolName} pool size from ${action.currentSize} to ${action.newSize}`);
        break;

      case 'decrease_pool_size':
        console.log(`Would decrease ${poolName} pool size from ${action.currentSize} to ${action.newSize}`);
        break;
    }
  }

  // Graceful shutdown of all connection pools
  async shutdownAllPools() {
    console.log('Shutting down all connection pools...');

    const shutdownPromises = [];

    for (const [poolName, poolInfo] of this.connectionPools.entries()) {
      shutdownPromises.push(
        poolInfo.manager.shutdown()
          .catch(error => console.error(`Error shutting down ${poolName} pool:`, error))
      );
    }

    await Promise.all(shutdownPromises);
    this.connectionPools.clear();

    console.log('All connection pools shut down successfully');
  }
}

SQL-Style Connection Pool Management with QueryLeaf

QueryLeaf provides familiar approaches to MongoDB connection pool configuration and monitoring:

-- QueryLeaf connection pool management with SQL-familiar syntax

-- Configure connection pool settings
CONFIGURE CONNECTION POOL production_pool WITH (
  min_connections = 10,
  max_connections = 100,
  connection_timeout = 10000,
  idle_timeout = 30000,
  wait_queue_timeout = 2500,

  -- Advanced settings
  retry_writes = true,
  retry_reads = true,
  compression = ['snappy', 'zlib'],
  load_balanced = true,

  -- Read preferences
  read_preference = 'secondaryPreferred',
  read_concern_level = 'majority',

  -- Write concern
  write_concern_w = 'majority',
  write_concern_j = true,
  write_concern_timeout = 5000
);

-- Monitor connection pool performance
SELECT 
  pool_name,
  active_connections,
  idle_connections,
  total_connections,

  -- Performance metrics
  avg_connection_wait_time_ms,
  max_connection_wait_time_ms,
  p95_connection_wait_time_ms,

  -- Query performance
  avg_query_execution_time_ms,
  queries_per_second,

  -- Health indicators
  pool_utilization_percent,
  error_rate_percent,
  health_score,

  -- Efficiency metrics
  connection_efficiency_percent,
  throughput_score,

  last_updated

FROM CONNECTION_POOL_STATS('production_pool')
WHERE timestamp >= NOW() - INTERVAL '1 hour';

-- Analyze connection pool trends
WITH pool_performance_trends AS (
  SELECT 
    DATE_TRUNC('minute', timestamp) as minute_bucket,

    -- Connection metrics
    AVG(active_connections) as avg_active_connections,
    MAX(active_connections) as max_active_connections,
    AVG(pool_utilization_percent) as avg_utilization,

    -- Performance metrics
    AVG(avg_connection_wait_time_ms) as avg_wait_time,
    AVG(avg_query_execution_time_ms) as avg_query_time,
    SUM(queries_per_second) as total_qps,

    -- Health metrics
    AVG(health_score) as avg_health_score,
    AVG(error_rate_percent) as avg_error_rate,
    COUNT(*) as measurement_count

  FROM CONNECTION_POOL_STATS('production_pool')
  WHERE timestamp >= NOW() - INTERVAL '24 hours'
  GROUP BY DATE_TRUNC('minute', timestamp)
),

performance_analysis AS (
  SELECT *,
    -- Trend analysis
    LAG(avg_utilization, 5) OVER (ORDER BY minute_bucket) as utilization_5min_ago,
    LAG(avg_wait_time, 10) OVER (ORDER BY minute_bucket) as wait_time_10min_ago,

    -- Performance scoring
    CASE 
      WHEN avg_health_score >= 90 THEN 'Excellent'
      WHEN avg_health_score >= 80 THEN 'Good'
      WHEN avg_health_score >= 60 THEN 'Fair'
      ELSE 'Poor'
    END as performance_grade,

    -- Utilization trends
    CASE 
      WHEN avg_utilization > LAG(avg_utilization, 5) OVER (ORDER BY minute_bucket) + 10 
        THEN 'Increasing'
      WHEN avg_utilization < LAG(avg_utilization, 5) OVER (ORDER BY minute_bucket) - 10 
        THEN 'Decreasing'
      ELSE 'Stable'
    END as utilization_trend

  FROM pool_performance_trends
)

SELECT 
  minute_bucket,
  avg_active_connections,
  max_active_connections,
  avg_utilization,
  avg_wait_time,
  avg_query_time,
  total_qps,
  performance_grade,
  utilization_trend,
  avg_health_score

FROM performance_analysis
WHERE minute_bucket >= NOW() - INTERVAL '4 hours'
ORDER BY minute_bucket DESC;

-- Connection pool optimization recommendations
WITH current_performance AS (
  SELECT 
    pool_name,
    active_connections,
    max_connections,
    pool_utilization_percent,
    avg_connection_wait_time_ms,
    error_rate_percent,
    health_score,
    queries_per_second

  FROM CONNECTION_POOL_STATS('production_pool')
  WHERE timestamp >= NOW() - INTERVAL '5 minutes'
  ORDER BY timestamp DESC
  LIMIT 1
),

optimization_analysis AS (
  SELECT *,
    -- Pool sizing recommendations
    CASE 
      WHEN pool_utilization_percent > 85 THEN 
        CONCAT('Increase max_connections from ', max_connections, ' to ', CEIL(max_connections * 1.3))
      WHEN pool_utilization_percent < 30 AND max_connections > 20 THEN 
        CONCAT('Decrease max_connections from ', max_connections, ' to ', GREATEST(CEIL(max_connections * 0.8), 20))
      ELSE 'Pool size appears optimal'
    END as pool_sizing_recommendation,

    -- Timeout recommendations
    CASE 
      WHEN avg_connection_wait_time_ms > 1000 THEN 'Consider increasing connection timeout or pool size'
      WHEN avg_connection_wait_time_ms < 50 THEN 'Connection timeouts are optimal'
      ELSE 'Connection timeouts are acceptable'
    END as timeout_recommendation,

    -- Performance recommendations
    CASE 
      WHEN error_rate_percent > 5 THEN 'Investigate connection errors - check server health and network'
      WHEN health_score < 70 THEN 'Pool performance needs attention - review metrics and configuration'
      WHEN queries_per_second > 1000 AND pool_utilization_percent > 80 THEN 'High throughput with high utilization - consider scaling'
      ELSE 'Performance appears satisfactory'
    END as performance_recommendation,

    -- Priority scoring
    CASE 
      WHEN pool_utilization_percent > 90 OR error_rate_percent > 10 THEN 'Critical'
      WHEN pool_utilization_percent > 75 OR avg_connection_wait_time_ms > 500 THEN 'High'
      WHEN health_score < 80 THEN 'Medium'
      ELSE 'Low'
    END as optimization_priority

  FROM current_performance
)

SELECT 
  pool_name,

  -- Current status
  CONCAT(active_connections, '/', max_connections) as connection_usage,
  ROUND(pool_utilization_percent, 1) as utilization_percent,
  ROUND(avg_connection_wait_time_ms, 1) as avg_wait_ms,
  ROUND(error_rate_percent, 2) as error_rate_percent,
  ROUND(health_score, 1) as health_score,

  -- Recommendations
  pool_sizing_recommendation,
  timeout_recommendation,
  performance_recommendation,
  optimization_priority,

  -- Action items
  CASE 
    WHEN optimization_priority = 'Critical' THEN 'Immediate action required'
    WHEN optimization_priority = 'High' THEN 'Schedule optimization within 24 hours'
    WHEN optimization_priority = 'Medium' THEN 'Plan optimization within 1 week'
    ELSE 'Monitor and review monthly'
  END as recommended_timeline,

  NOW() as analysis_timestamp

FROM optimization_analysis;

-- Automated pool health monitoring with alerts
CREATE ALERT CONNECTION_POOL_HEALTH_MONITOR
ON CONNECTION_POOL_STATS('production_pool')
WHEN (
  pool_utilization_percent > 90 OR
  avg_connection_wait_time_ms > 2000 OR
  error_rate_percent > 5 OR
  health_score < 70
)
NOTIFY ['[email protected]', '[email protected]']
WITH MESSAGE TEMPLATE '''
Connection Pool Alert: {{ pool_name }}

Current Status:
- Utilization: {{ pool_utilization_percent }}%
- Active Connections: {{ active_connections }}/{{ max_connections }}
- Average Wait Time: {{ avg_connection_wait_time_ms }}ms
- Error Rate: {{ error_rate_percent }}%
- Health Score: {{ health_score }}

Recommended Actions:
{{ pool_sizing_recommendation }}
{{ timeout_recommendation }}
{{ performance_recommendation }}

Dashboard: https://monitoring.company.com/mongodb/pools/{{ pool_name }}
'''
EVERY 5 MINUTES;

-- Historical connection pool analysis
SELECT 
  DATE(timestamp) as analysis_date,

  -- Daily aggregates
  AVG(pool_utilization_percent) as avg_daily_utilization,
  MAX(pool_utilization_percent) as peak_daily_utilization,
  AVG(avg_connection_wait_time_ms) as avg_daily_wait_time,
  MAX(active_connections) as peak_daily_connections,

  -- Performance indicators
  AVG(health_score) as avg_daily_health_score,
  MIN(health_score) as lowest_daily_health_score,
  AVG(queries_per_second) as avg_daily_qps,
  MAX(queries_per_second) as peak_daily_qps,

  -- Issue tracking
  COUNT(CASE WHEN error_rate_percent > 1 THEN 1 END) as error_incidents,
  COUNT(CASE WHEN pool_utilization_percent > 85 THEN 1 END) as high_utilization_incidents,

  -- Efficiency metrics
  AVG(connection_efficiency_percent) as avg_connection_efficiency,
  AVG(throughput_score) as avg_throughput_score

FROM CONNECTION_POOL_STATS('production_pool')
WHERE timestamp >= NOW() - INTERVAL '30 days'
GROUP BY DATE(timestamp)
ORDER BY analysis_date DESC;

-- QueryLeaf connection pooling provides:
-- 1. SQL-familiar pool configuration and management
-- 2. Comprehensive performance monitoring and analysis
-- 3. Intelligent optimization recommendations
-- 4. Automated health monitoring and alerting
-- 5. Historical trend analysis and capacity planning
-- 6. Integration with MongoDB's native pooling features
-- 7. Real-time performance metrics and diagnostics
-- 8. Automated scaling recommendations based on usage patterns
-- 9. Multi-tier pooling strategies for different workload types
-- 10. Enterprise-grade monitoring and operational visibility

Best Practices for MongoDB Connection Pooling

Pool Sizing Strategy

Optimal connection pool configuration for different application types:

  1. High-Traffic Web Applications: Large pools with aggressive timeouts for rapid response
  2. Batch Processing Systems: Moderate pools with longer timeouts for sustained throughput
  3. Analytics Applications: Smaller pools with secondary read preferences for reporting queries
  4. Microservices Architecture: Multiple specialized pools for different service patterns
  5. Real-time Applications: Priority-based pooling with guaranteed connection availability
  6. Background Services: Separate pools to prevent interference with user-facing operations

Performance Monitoring Guidelines

Essential metrics for production connection pool management:

  1. Utilization Metrics: Track active vs. available connections continuously
  2. Latency Monitoring: Monitor connection wait times and query execution performance
  3. Error Rate Analysis: Track connection failures and timeout patterns
  4. Resource Efficiency: Analyze connection reuse rates and pool effectiveness
  5. Capacity Planning: Use historical data to predict scaling requirements
  6. Health Scoring: Implement composite health metrics for proactive management

Conclusion

MongoDB connection pooling optimization requires sophisticated strategies that balance performance, resource utilization, and operational reliability. By implementing intelligent pooling algorithms, comprehensive monitoring systems, and automated optimization techniques, applications can achieve maximum throughput while maintaining efficient resource usage and operational stability.

Key connection pooling benefits include:

  • Intelligent Scaling: Automatic pool sizing based on demand patterns and performance metrics
  • Performance Optimization: Real-time monitoring and tuning for optimal query execution
  • Resource Efficiency: Optimal connection reuse and lifecycle management
  • Operational Visibility: Comprehensive metrics and alerting for proactive management
  • High Availability: Intelligent failover and connection recovery mechanisms
  • Enterprise Integration: Support for complex deployment architectures and monitoring systems

Whether you're building high-throughput web applications, data processing pipelines, analytics platforms, or distributed microservices, MongoDB's intelligent connection pooling with QueryLeaf's familiar management interface provides the foundation for scalable, efficient database operations. This combination enables you to leverage advanced connection management capabilities while maintaining familiar database administration patterns and operational procedures.

QueryLeaf Integration: QueryLeaf automatically translates SQL-familiar connection pool configuration into optimal MongoDB driver settings while providing comprehensive monitoring and optimization through SQL-style queries. Advanced pooling strategies, performance analysis, and automated tuning are seamlessly managed through familiar database administration interfaces, making sophisticated connection management both powerful and accessible.

The integration of intelligent connection pooling with SQL-style database operations makes MongoDB an ideal platform for applications requiring both high-performance database access and familiar connection management patterns, ensuring your database connections remain both efficient and reliable as they scale to meet demanding production requirements.

MongoDB Data Modeling Best Practices and Schema Design: Advanced Document Structure Optimization and Relationship Management for Scalable Applications

Modern applications require sophisticated data modeling strategies that can handle complex relationships, evolving schemas, and high-performance requirements while maintaining data consistency and query flexibility. Traditional relational modeling approaches often struggle with document-oriented data, nested structures, and the dynamic schema requirements of modern applications, leading to complex object-relational mapping, rigid schema constraints, and performance bottlenecks that limit application scalability and development velocity.

MongoDB provides comprehensive data modeling capabilities through flexible document structures, embedded relationships, and advanced schema design patterns that enable sophisticated data organization with optimal performance characteristics. Unlike traditional databases that enforce rigid table structures and require complex joins, MongoDB integrates data modeling directly into the document structure with native support for arrays, nested objects, and flexible schemas that adapt to application requirements.

The Traditional Relational Data Modeling Challenge

Conventional approaches to data modeling in relational systems face significant limitations when handling complex, hierarchical, and rapidly evolving data structures:

-- Traditional relational data modeling - rigid schema with complex relationship management

-- Basic user management with limited flexibility
CREATE TABLE users (
    user_id SERIAL PRIMARY KEY,
    username VARCHAR(255) UNIQUE NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,

    -- Basic profile information (limited structure)
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    date_of_birth DATE,
    phone_number VARCHAR(20),

    -- Address information (denormalized for simplicity)
    address_line_1 VARCHAR(255),
    address_line_2 VARCHAR(255),
    city VARCHAR(100),
    state VARCHAR(100),
    postal_code VARCHAR(20),
    country VARCHAR(100),

    -- Account metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_login TIMESTAMP,
    account_status VARCHAR(50) DEFAULT 'active',

    -- Basic preferences (very limited)
    preferred_language VARCHAR(10) DEFAULT 'en',
    timezone VARCHAR(50) DEFAULT 'UTC',

    -- Social media links (limited and rigid)
    facebook_url VARCHAR(255),
    twitter_url VARCHAR(255),
    linkedin_url VARCHAR(255),
    instagram_url VARCHAR(255)
);

-- Separate table for user profiles (normalized approach)
CREATE TABLE user_profiles (
    profile_id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES users(user_id) ON DELETE CASCADE,

    -- Extended profile information
    bio TEXT,
    website VARCHAR(255),
    company VARCHAR(255),
    job_title VARCHAR(255),

    -- Skills and interests (very basic approach)
    skills TEXT, -- Comma-separated values - not optimal
    interests TEXT, -- Comma-separated values - not optimal

    -- Professional information
    years_of_experience INTEGER,
    education_level VARCHAR(100),

    -- Contact preferences
    email_notifications BOOLEAN DEFAULT true,
    sms_notifications BOOLEAN DEFAULT false,
    marketing_emails BOOLEAN DEFAULT false,

    -- Profile metadata
    profile_completeness_percent DECIMAL(5,2) DEFAULT 0.0,
    profile_visibility VARCHAR(50) DEFAULT 'public',

    -- Profile customization (limited)
    theme VARCHAR(50) DEFAULT 'default',
    profile_picture_url VARCHAR(255),
    cover_photo_url VARCHAR(255)
);

-- User posts with basic relationship management
CREATE TABLE posts (
    post_id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES users(user_id) ON DELETE CASCADE,

    -- Post content
    title VARCHAR(500) NOT NULL,
    content TEXT NOT NULL,
    post_type VARCHAR(50) DEFAULT 'article',

    -- Post metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    published_at TIMESTAMP,

    -- Post status and visibility
    status VARCHAR(50) DEFAULT 'draft',
    visibility VARCHAR(50) DEFAULT 'public',

    -- SEO and categorization
    slug VARCHAR(500) UNIQUE,
    meta_description TEXT,
    featured_image_url VARCHAR(255),

    -- Engagement metrics (basic)
    view_count INTEGER DEFAULT 0,
    like_count INTEGER DEFAULT 0,
    comment_count INTEGER DEFAULT 0,
    share_count INTEGER DEFAULT 0,

    -- Content flags
    is_featured BOOLEAN DEFAULT false,
    is_pinned BOOLEAN DEFAULT false,
    allow_comments BOOLEAN DEFAULT true
);

-- Post categories (many-to-many relationship)
CREATE TABLE categories (
    category_id SERIAL PRIMARY KEY,
    category_name VARCHAR(255) UNIQUE NOT NULL,
    category_slug VARCHAR(255) UNIQUE NOT NULL,
    description TEXT,
    parent_category_id INTEGER REFERENCES categories(category_id),

    -- Category metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN DEFAULT true,
    sort_order INTEGER DEFAULT 0,

    -- Category appearance
    color VARCHAR(7), -- Hex color code
    icon VARCHAR(100) -- Icon identifier
);

-- Post-category relationships (junction table)
CREATE TABLE post_categories (
    post_id INTEGER REFERENCES posts(post_id) ON DELETE CASCADE,
    category_id INTEGER REFERENCES categories(category_id) ON DELETE CASCADE,

    PRIMARY KEY (post_id, category_id),

    -- Relationship metadata
    assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    assigned_by INTEGER REFERENCES users(user_id)
);

-- Comments with hierarchical structure (self-referencing)
CREATE TABLE comments (
    comment_id SERIAL PRIMARY KEY,
    post_id INTEGER REFERENCES posts(post_id) ON DELETE CASCADE,
    user_id INTEGER REFERENCES users(user_id) ON DELETE CASCADE,
    parent_comment_id INTEGER REFERENCES comments(comment_id) ON DELETE CASCADE,

    -- Comment content
    content TEXT NOT NULL,
    comment_type VARCHAR(50) DEFAULT 'text',

    -- Comment metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Comment status
    status VARCHAR(50) DEFAULT 'published',
    is_edited BOOLEAN DEFAULT false,
    is_pinned BOOLEAN DEFAULT false,

    -- Engagement
    like_count INTEGER DEFAULT 0,
    reply_count INTEGER DEFAULT 0,

    -- Moderation
    is_flagged BOOLEAN DEFAULT false,
    moderation_status VARCHAR(50) DEFAULT 'approved'
);

-- Tags for flexible categorization (many-to-many)
CREATE TABLE tags (
    tag_id SERIAL PRIMARY KEY,
    tag_name VARCHAR(255) UNIQUE NOT NULL,
    tag_slug VARCHAR(255) UNIQUE NOT NULL,
    description TEXT,

    -- Tag metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    usage_count INTEGER DEFAULT 0,
    is_trending BOOLEAN DEFAULT false,

    -- Tag appearance
    color VARCHAR(7)
);

-- Post-tag relationships
CREATE TABLE post_tags (
    post_id INTEGER REFERENCES posts(post_id) ON DELETE CASCADE,
    tag_id INTEGER REFERENCES tags(tag_id) ON DELETE CASCADE,

    PRIMARY KEY (post_id, tag_id),

    -- Relationship metadata
    tagged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    tagged_by INTEGER REFERENCES users(user_id),
    relevance_score DECIMAL(3,2) DEFAULT 1.0
);

-- Complex query to retrieve post with all relationships (performance issues)
WITH post_data AS (
    SELECT 
        p.post_id,
        p.title,
        p.content,
        p.created_at,
        p.status,
        p.view_count,
        p.like_count,
        p.comment_count,

        -- User information (requires join)
        u.username,
        u.email,
        up.bio,
        up.profile_picture_url,

        -- Categories (requires aggregation)
        STRING_AGG(DISTINCT c.category_name, ', ' ORDER BY c.category_name) as categories,

        -- Tags (requires aggregation)
        STRING_AGG(DISTINCT t.tag_name, ', ' ORDER BY t.tag_name) as tags

    FROM posts p
    JOIN users u ON p.user_id = u.user_id
    LEFT JOIN user_profiles up ON u.user_id = up.user_id
    LEFT JOIN post_categories pc ON p.post_id = pc.post_id
    LEFT JOIN categories c ON pc.category_id = c.category_id
    LEFT JOIN post_tags pt ON p.post_id = pt.post_id
    LEFT JOIN tags t ON pt.tag_id = t.tag_id

    WHERE p.status = 'published'
    GROUP BY 
        p.post_id, p.title, p.content, p.created_at, p.status, 
        p.view_count, p.like_count, p.comment_count,
        u.username, u.email, up.bio, up.profile_picture_url
),

comment_hierarchy AS (
    -- Recursive CTE for nested comments (complex and performance-intensive)
    WITH RECURSIVE comment_tree AS (
        SELECT 
            c.comment_id,
            c.post_id,
            c.content,
            c.created_at,
            c.parent_comment_id,
            u.username as commenter_username,
            up.profile_picture_url as commenter_picture,
            0 as depth,
            CAST(c.comment_id as TEXT) as path
        FROM comments c
        JOIN users u ON c.user_id = u.user_id
        LEFT JOIN user_profiles up ON u.user_id = up.user_id
        WHERE c.parent_comment_id IS NULL
        AND c.status = 'published'

        UNION ALL

        SELECT 
            c.comment_id,
            c.post_id,
            c.content,
            c.created_at,
            c.parent_comment_id,
            u.username,
            up.profile_picture_url,
            ct.depth + 1,
            ct.path || '.' || c.comment_id
        FROM comments c
        JOIN users u ON c.user_id = u.user_id
        LEFT JOIN user_profiles up ON u.user_id = up.user_id
        JOIN comment_tree ct ON c.parent_comment_id = ct.comment_id
        WHERE c.status = 'published'
        AND ct.depth < 5 -- Limit recursion depth
    )
    SELECT 
        post_id,
        JSON_AGG(
            JSON_BUILD_OBJECT(
                'comment_id', comment_id,
                'content', content,
                'created_at', created_at,
                'commenter_username', commenter_username,
                'commenter_picture', commenter_picture,
                'depth', depth,
                'path', path
            ) ORDER BY path
        ) as comments_json
    FROM comment_tree
    GROUP BY post_id
)

SELECT 
    pd.post_id,
    pd.title,
    pd.content,
    pd.created_at,
    pd.username as author_username,
    pd.bio as author_bio,
    pd.profile_picture_url as author_picture,
    pd.categories,
    pd.tags,
    pd.view_count,
    pd.like_count,
    pd.comment_count,

    -- Comments as JSON (complex aggregation)
    COALESCE(ch.comments_json, '[]'::json) as comments

FROM post_data pd
LEFT JOIN comment_hierarchy ch ON pd.post_id = ch.post_id
ORDER BY pd.created_at DESC;

-- Basic user activity analysis (multiple complex joins)
WITH user_activity AS (
    SELECT 
        u.user_id,
        u.username,
        u.email,
        u.created_at as user_created_at,

        -- Post statistics
        COUNT(DISTINCT p.post_id) as total_posts,
        COUNT(DISTINCT CASE WHEN p.status = 'published' THEN p.post_id END) as published_posts,
        SUM(p.view_count) as total_views,
        SUM(p.like_count) as total_likes,

        -- Comment statistics
        COUNT(DISTINCT c.comment_id) as total_comments,

        -- Category usage
        COUNT(DISTINCT pc.category_id) as categories_used,

        -- Tag usage
        COUNT(DISTINCT pt.tag_id) as tags_used,

        -- Activity timeline
        MAX(GREATEST(p.created_at, c.created_at)) as last_activity_at,

        -- Engagement metrics
        AVG(p.view_count) as avg_views_per_post,
        AVG(p.like_count) as avg_likes_per_post,
        AVG(p.comment_count) as avg_comments_per_post

    FROM users u
    LEFT JOIN posts p ON u.user_id = p.user_id
    LEFT JOIN comments c ON u.user_id = c.user_id
    LEFT JOIN post_categories pc ON p.post_id = pc.post_id
    LEFT JOIN post_tags pt ON p.post_id = pt.post_id

    WHERE u.account_status = 'active'
    GROUP BY u.user_id, u.username, u.email, u.created_at
),

engagement_analysis AS (
    SELECT 
        ua.*,

        -- Activity classification
        CASE 
            WHEN ua.total_posts > 50 AND ua.total_comments > 100 THEN 'highly_active'
            WHEN ua.total_posts > 10 AND ua.total_comments > 25 THEN 'moderately_active'
            WHEN ua.total_posts > 0 OR ua.total_comments > 0 THEN 'low_activity'
            ELSE 'inactive'
        END as activity_level,

        -- Content quality indicators
        CASE 
            WHEN ua.avg_views_per_post > 1000 AND ua.avg_likes_per_post > 50 THEN 'high_quality'
            WHEN ua.avg_views_per_post > 500 AND ua.avg_likes_per_post > 20 THEN 'good_quality'
            WHEN ua.avg_views_per_post > 100 THEN 'average_quality'
            ELSE 'low_engagement'
        END as content_quality,

        -- User tenure
        EXTRACT(DAYS FROM CURRENT_TIMESTAMP - ua.user_created_at) as days_since_signup,
        EXTRACT(DAYS FROM CURRENT_TIMESTAMP - ua.last_activity_at) as days_since_last_activity,

        -- Productivity metrics
        CASE 
            WHEN EXTRACT(DAYS FROM CURRENT_TIMESTAMP - ua.user_created_at) > 0 THEN
                ua.total_posts / EXTRACT(DAYS FROM CURRENT_TIMESTAMP - ua.user_created_at)::DECIMAL
            ELSE 0
        END as posts_per_day,

        -- Diversity metrics
        CASE 
            WHEN ua.total_posts > 0 THEN ua.categories_used / ua.total_posts::DECIMAL
            ELSE 0
        END as category_diversity,

        CASE 
            WHEN ua.total_posts > 0 THEN ua.tags_used / ua.total_posts::DECIMAL
            ELSE 0
        END as tag_diversity

    FROM user_activity ua
)

SELECT 
    ea.username,
    ea.activity_level,
    ea.content_quality,
    ea.total_posts,
    ea.published_posts,
    ROUND(ea.total_views, 0) as total_views,
    ROUND(ea.total_likes, 0) as total_likes,
    ea.total_comments,

    -- Engagement metrics
    ROUND(ea.avg_views_per_post, 1) as avg_views_per_post,
    ROUND(ea.avg_likes_per_post, 1) as avg_likes_per_post,
    ROUND(ea.avg_comments_per_post, 1) as avg_comments_per_post,

    -- Activity metrics
    ROUND(ea.posts_per_day, 3) as posts_per_day,
    ROUND(ea.category_diversity, 2) as category_diversity,
    ROUND(ea.tag_diversity, 2) as tag_diversity,

    -- Time metrics
    ea.days_since_signup,
    ea.days_since_last_activity,

    -- Recommendations
    CASE 
        WHEN ea.activity_level = 'inactive' AND ea.days_since_signup < 30 THEN 'new_user_onboarding'
        WHEN ea.activity_level = 'low_activity' AND ea.days_since_last_activity > 30 THEN 're_engagement_campaign'
        WHEN ea.content_quality = 'high_quality' THEN 'featured_contributor'
        WHEN ea.activity_level = 'highly_active' AND ea.content_quality != 'high_quality' THEN 'content_improvement_guidance'
        ELSE 'continue_monitoring'
    END as engagement_recommendation

FROM engagement_analysis ea
ORDER BY ea.total_views DESC, ea.total_posts DESC;

-- Problems with traditional relational data modeling:
-- 1. Rigid schema requiring extensive migrations for changes
-- 2. Complex joins across multiple tables for simple data retrieval
-- 3. Object-relational impedance mismatch for nested data structures
-- 4. Performance overhead from normalization and multiple table queries
-- 5. Difficulty modeling hierarchical and semi-structured data
-- 6. Limited flexibility for evolving application requirements
-- 7. Complex relationship management requiring junction tables
-- 8. Inefficient storage for sparse or optional data fields
-- 9. Challenging aggregation across related entities
-- 10. Maintenance complexity for schema evolution and data migration

MongoDB provides comprehensive data modeling capabilities with flexible document structures and embedded relationships:

// MongoDB Advanced Data Modeling - flexible document structures with optimized relationships
const { MongoClient, ObjectId } = require('mongodb');

// Comprehensive MongoDB Data Modeling Manager
class AdvancedDataModelingManager {
  constructor(mongoUri, modelingConfig = {}) {
    this.mongoUri = mongoUri;
    this.client = null;
    this.db = null;

    // Data modeling configuration
    this.config = {
      // Schema validation settings
      enableSchemaValidation: modelingConfig.enableSchemaValidation !== false,
      strictValidation: modelingConfig.strictValidation || false,
      validationLevel: modelingConfig.validationLevel || 'moderate',

      // Document design preferences
      embeddingStrategy: modelingConfig.embeddingStrategy || 'balanced', // balanced, aggressive, conservative
      referencingThreshold: modelingConfig.referencingThreshold || 100, // Size threshold for referencing
      denormalizationLevel: modelingConfig.denormalizationLevel || 'moderate',

      // Performance optimization
      enableIndexOptimization: modelingConfig.enableIndexOptimization !== false,
      enableAggregationOptimization: modelingConfig.enableAggregationOptimization || false,
      enableQueryPatternAnalysis: modelingConfig.enableQueryPatternAnalysis || false,

      // Relationship management
      cascadeDeletes: modelingConfig.cascadeDeletes || false,
      maintainReferentialIntegrity: modelingConfig.maintainReferentialIntegrity || false,
      enableRelationshipIndexing: modelingConfig.enableRelationshipIndexing !== false,

      // Schema evolution
      enableSchemaEvolution: modelingConfig.enableSchemaEvolution || false,
      backwardCompatibility: modelingConfig.backwardCompatibility !== false,
      versionedSchemas: modelingConfig.versionedSchemas || false
    };

    // Document schemas and relationships
    this.documentSchemas = new Map();
    this.relationshipMappings = new Map();
    this.validationRules = new Map();

    // Performance and optimization state
    this.queryPatterns = new Map();
    this.indexStrategies = new Map();
    this.optimizationRecommendations = [];

    this.initializeDataModeling();
  }

  async initializeDataModeling() {
    console.log('Initializing advanced MongoDB data modeling...');

    try {
      // Connect to MongoDB
      this.client = new MongoClient(this.mongoUri);
      await this.client.connect();
      this.db = this.client.db();

      // Setup comprehensive user schema with embedded relationships
      await this.defineUserSchema();

      // Setup post schema with flexible content structure
      await this.definePostSchema();

      // Setup optimized indexes for performance
      if (this.config.enableIndexOptimization) {
        await this.setupOptimizedIndexes();
      }

      // Initialize schema validation if enabled
      if (this.config.enableSchemaValidation) {
        await this.applySchemaValidation();
      }

      console.log('Advanced data modeling initialized successfully');

    } catch (error) {
      console.error('Error initializing data modeling:', error);
      throw error;
    }
  }

  async defineUserSchema() {
    console.log('Defining comprehensive user schema with embedded relationships...');

    try {
      const userSchema = {
        // Schema metadata
        schemaVersion: '1.0',
        schemaName: 'user_profile',
        lastUpdated: new Date(),

        // Document structure
        documentStructure: {
          // Core identification
          _id: 'ObjectId',
          userId: 'string', // Application-level ID
          username: 'string',
          email: 'string',

          // Personal information (embedded object)
          profile: {
            firstName: 'string',
            lastName: 'string',
            displayName: 'string',
            bio: 'string',
            dateOfBirth: 'date',
            phoneNumber: 'string',

            // Professional information
            company: 'string',
            jobTitle: 'string',
            yearsOfExperience: 'number',
            educationLevel: 'string',

            // Skills and interests (arrays for flexibility)
            skills: ['string'],
            interests: ['string'],
            languages: [
              {
                language: 'string',
                proficiency: 'string' // beginner, intermediate, advanced, native
              }
            ],

            // Social media links (flexible object)
            socialMedia: {
              facebook: 'string',
              twitter: 'string',
              linkedin: 'string',
              instagram: 'string',
              github: 'string',
              website: 'string'
            },

            // Profile media
            profilePicture: {
              url: 'string',
              thumbnailUrl: 'string',
              uploadedAt: 'date',
              fileSize: 'number',
              dimensions: {
                width: 'number',
                height: 'number'
              }
            },

            coverPhoto: {
              url: 'string',
              uploadedAt: 'date',
              fileSize: 'number'
            }
          },

          // Contact information (embedded for locality)
          contact: {
            addresses: [
              {
                type: 'string', // home, work, billing, shipping
                addressLine1: 'string',
                addressLine2: 'string',
                city: 'string',
                state: 'string',
                postalCode: 'string',
                country: 'string',
                isPrimary: 'boolean',
                coordinates: {
                  latitude: 'number',
                  longitude: 'number'
                }
              }
            ],

            phoneNumbers: [
              {
                type: 'string', // mobile, home, work
                number: 'string',
                countryCode: 'string',
                isPrimary: 'boolean',
                isVerified: 'boolean'
              }
            ],

            emailAddresses: [
              {
                email: 'string',
                type: 'string', // primary, work, personal
                isVerified: 'boolean',
                isPrimary: 'boolean'
              }
            ]
          },

          // Account settings and preferences (embedded)
          settings: {
            // Privacy settings
            privacy: {
              profileVisibility: 'string', // public, private, friends
              emailVisible: 'boolean',
              phoneVisible: 'boolean',
              searchable: 'boolean'
            },

            // Notification preferences
            notifications: {
              email: {
                posts: 'boolean',
                comments: 'boolean',
                mentions: 'boolean',
                messages: 'boolean',
                newsletter: 'boolean',
                marketing: 'boolean'
              },
              push: {
                posts: 'boolean',
                comments: 'boolean',
                mentions: 'boolean',
                messages: 'boolean'
              },
              sms: {
                security: 'boolean',
                important: 'boolean'
              }
            },

            // UI preferences
            interface: {
              theme: 'string', // light, dark, auto
              language: 'string',
              timezone: 'string',
              dateFormat: 'string',
              currency: 'string'
            },

            // Content preferences
            content: {
              defaultPostVisibility: 'string',
              autoSaveEnabled: 'boolean',
              contentLanguages: ['string']
            }
          },

          // Activity tracking (embedded for performance)
          activity: {
            // Account lifecycle
            createdAt: 'date',
            updatedAt: 'date',
            lastLoginAt: 'date',
            lastActiveAt: 'date',

            // Status information
            status: 'string', // active, inactive, suspended, deleted
            emailVerifiedAt: 'date',
            phoneVerifiedAt: 'date',

            // Statistics (denormalized for performance)
            stats: {
              totalPosts: 'number',
              publishedPosts: 'number',
              totalComments: 'number',
              totalLikes: 'number',
              totalViews: 'number',
              followersCount: 'number',
              followingCount: 'number',

              // Calculated metrics
              engagementRate: 'number',
              averagePostViews: 'number',
              profileCompleteness: 'number'
            },

            // Activity timeline (recent activities embedded)
            recentActivities: [
              {
                type: 'string', // login, post_created, comment_posted, profile_updated
                timestamp: 'date',
                details: 'object', // Flexible details object
                ipAddress: 'string',
                userAgent: 'string'
              }
            ]
          },

          // Authentication and security (embedded)
          authentication: {
            passwordHash: 'string',
            passwordSalt: 'string',
            lastPasswordChange: 'date',

            // Two-factor authentication
            twoFactorEnabled: 'boolean',
            twoFactorSecret: 'string',
            backupCodes: ['string'],

            // Session management
            activeSessions: [
              {
                sessionId: 'string',
                createdAt: 'date',
                lastActivityAt: 'date',
                ipAddress: 'string',
                userAgent: 'string',
                deviceInfo: 'object'
              }
            ],

            // Security events
            securityEvents: [
              {
                type: 'string', // login_attempt, password_change, suspicious_activity
                timestamp: 'date',
                details: 'object',
                resolved: 'boolean'
              }
            ]
          },

          // Content relationships (selective referencing for large collections)
          content: {
            // Recent posts (embedded for performance)
            recentPosts: [
              {
                postId: 'ObjectId',
                title: 'string',
                createdAt: 'date',
                status: 'string',
                viewCount: 'number',
                likeCount: 'number'
              }
            ],

            // Favorite posts (referenced due to potential size)
            favoritePostIds: ['ObjectId'],

            // Bookmarked content
            bookmarks: [
              {
                contentId: 'ObjectId',
                contentType: 'string', // post, comment, user
                bookmarkedAt: 'date',
                tags: ['string'],
                notes: 'string'
              }
            ]
          },

          // Social relationships (hybrid approach)
          social: {
            // Close relationships (embedded for performance)
            following: [
              {
                userId: 'ObjectId',
                username: 'string',
                followedAt: 'date',
                relationshipType: 'string' // friend, colleague, interest
              }
            ],

            // Large follower lists (referenced)
            followerIds: ['ObjectId'],

            // Social groups and communities
            groups: [
              {
                groupId: 'ObjectId',
                groupName: 'string',
                role: 'string', // member, moderator, admin
                joinedAt: 'date'
              }
            ]
          },

          // Flexible metadata for extensibility
          metadata: {
            customFields: 'object', // Application-specific fields
            tags: ['string'],
            categories: ['string'],
            source: 'string', // registration_source
            referrer: 'string'
          }
        },

        // Validation rules
        validationRules: {
          required: ['username', 'email', 'profile.firstName', 'profile.lastName'],
          unique: ['username', 'email', 'userId'],
          patterns: {
            email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
            username: /^[a-zA-Z0-9_]{3,30}$/
          },
          ranges: {
            'profile.yearsOfExperience': { min: 0, max: 70 },
            'activity.stats.profileCompleteness': { min: 0, max: 100 }
          }
        },

        // Index strategies for optimal performance
        indexStrategies: [
          { fields: { username: 1 }, unique: true },
          { fields: { email: 1 }, unique: true },
          { fields: { userId: 1 }, unique: true },
          { fields: { 'activity.lastActiveAt': -1 } },
          { fields: { 'activity.createdAt': -1 } },
          { fields: { 'profile.skills': 1 } },
          { fields: { 'metadata.tags': 1 } },

          // Compound indexes for common query patterns
          { fields: { 'activity.status': 1, 'activity.lastActiveAt': -1 } },
          { fields: { 'profile.company': 1, 'profile.jobTitle': 1 } },
          { fields: { 'settings.privacy.profileVisibility': 1, 'activity.stats.totalPosts': -1 } }
        ]
      };

      // Store schema definition
      this.documentSchemas.set('users', userSchema);

      console.log('User schema defined with embedded relationships and flexible structure');

    } catch (error) {
      console.error('Error defining user schema:', error);
      throw error;
    }
  }

  async definePostSchema() {
    console.log('Defining flexible post schema with content optimization...');

    try {
      const postSchema = {
        // Schema metadata
        schemaVersion: '1.0',
        schemaName: 'content_post',
        lastUpdated: new Date(),

        // Document structure optimized for content management
        documentStructure: {
          // Core identification
          _id: 'ObjectId',
          postId: 'string', // Application-level ID
          slug: 'string', // URL-friendly identifier

          // Author information (denormalized for performance)
          author: {
            userId: 'ObjectId',
            username: 'string',
            displayName: 'string',
            profilePicture: 'string',

            // Author stats (denormalized)
            totalPosts: 'number',
            followerCount: 'number',
            verified: 'boolean'
          },

          // Content structure (flexible for different content types)
          content: {
            // Basic content information
            title: 'string',
            subtitle: 'string',
            excerpt: 'string',
            body: 'string', // Main content
            contentType: 'string', // article, tutorial, review, announcement

            // Rich content elements
            media: [
              {
                type: 'string', // image, video, audio, embed
                url: 'string',
                thumbnailUrl: 'string',
                caption: 'string',
                altText: 'string',
                dimensions: {
                  width: 'number',
                  height: 'number'
                },
                fileSize: 'number',
                mimeType: 'string',
                duration: 'number', // For video/audio
                uploadedAt: 'date'
              }
            ],

            // Content structure and formatting
            sections: [
              {
                type: 'string', // paragraph, heading, list, code, quote
                content: 'string',
                level: 'number', // For headings
                language: 'string', // For code blocks
                order: 'number'
              }
            ],

            // SEO and metadata
            seo: {
              metaTitle: 'string',
              metaDescription: 'string',
              keywords: ['string'],
              canonicalUrl: 'string',
              openGraphImage: 'string',

              // Schema.org structured data
              structuredData: 'object'
            },

            // Content settings
            formatting: {
              readingTime: 'number', // Estimated reading time in minutes
              wordCount: 'number',
              language: 'string',
              rtlDirection: 'boolean'
            }
          },

          // Publication and status management
          publication: {
            // Status workflow
            status: 'string', // draft, review, published, archived, deleted
            visibility: 'string', // public, private, unlisted, password_protected
            password: 'string', // For password-protected posts

            // Publishing timeline
            createdAt: 'date',
            updatedAt: 'date',
            publishedAt: 'date',
            scheduledPublishAt: 'date',

            // Revision history (embedded for recent changes)
            revisions: [
              {
                version: 'number',
                changedAt: 'date',
                changedBy: 'ObjectId',
                changeType: 'string', // content, metadata, status
                changesSummary: 'string',
                previousTitle: 'string', // Track major changes
                previousContent: 'string' // Last few versions only
              }
            ],

            // Publishing settings
            allowComments: 'boolean',
            allowSharing: 'boolean',
            allowIndexing: 'boolean',
            requireApproval: 'boolean'
          },

          // Categorization and tagging (embedded for performance)
          taxonomy: {
            // Categories (hierarchical structure)
            categories: [
              {
                categoryId: 'ObjectId',
                name: 'string',
                slug: 'string',
                level: 'number', // For hierarchical categories
                parentCategory: 'string'
              }
            ],

            // Tags (flat structure for flexibility)
            tags: [
              {
                tag: 'string',
                relevanceScore: 'number',
                addedBy: 'ObjectId',
                addedAt: 'date'
              }
            ],

            // Custom taxonomies
            customFields: {
              difficulty: 'string', // For tutorials
              estimatedTime: 'number', // For how-to content
              targetAudience: 'string',
              prerequisites: ['string']
            }
          },

          // Engagement metrics (denormalized for performance)
          engagement: {
            // View statistics
            views: {
              total: 'number',
              unique: 'number',
              today: 'number',
              thisWeek: 'number',
              thisMonth: 'number',

              // View sources
              sources: {
                direct: 'number',
                social: 'number',
                search: 'number',
                referral: 'number'
              }
            },

            // Interaction statistics
            interactions: {
              likes: 'number',
              dislikes: 'number',
              shares: 'number',
              bookmarks: 'number',

              // Comment statistics
              comments: {
                total: 'number',
                approved: 'number',
                pending: 'number',
                spam: 'number'
              }
            },

            // Engagement metrics
            metrics: {
              engagementRate: 'number',
              averageTimeOnPage: 'number',
              bounceRate: 'number',
              socialShares: 'number'
            },

            // Top comments (embedded for performance)
            topComments: [
              {
                commentId: 'ObjectId',
                content: 'string',
                author: {
                  userId: 'ObjectId',
                  username: 'string',
                  profilePicture: 'string'
                },
                createdAt: 'date',
                likeCount: 'number',
                isHighlighted: 'boolean'
              }
            ]
          },

          // Comments (hybrid approach - recent embedded, full collection referenced)
          comments: {
            // Recent comments embedded for quick access
            recent: [
              {
                commentId: 'ObjectId',
                parentCommentId: 'ObjectId', // For threading
                content: 'string',

                // Author information (denormalized)
                author: {
                  userId: 'ObjectId',
                  username: 'string',
                  displayName: 'string',
                  profilePicture: 'string'
                },

                // Comment metadata
                createdAt: 'date',
                updatedAt: 'date',
                status: 'string', // approved, pending, spam, deleted

                // Comment engagement
                likeCount: 'number',
                replyCount: 'number',
                isEdited: 'boolean',
                isPinned: 'boolean',

                // Moderation
                flags: ['string'],
                moderationStatus: 'string'
              }
            ],

            // Statistics
            statistics: {
              totalComments: 'number',
              approvedComments: 'number',
              pendingComments: 'number',
              lastCommentAt: 'date'
            }
          },

          // Performance optimization data
          performance: {
            // Caching information
            lastCached: 'date',
            cacheVersion: 'string',

            // Search optimization
            searchTerms: ['string'], // Extracted keywords for search
            searchBoost: 'number', // Manual search ranking boost

            // Content analysis
            sentiment: {
              score: 'number', // -1 to 1
              magnitude: 'number',
              language: 'string'
            },

            readabilityScore: 'number',
            complexity: 'string' // simple, moderate, complex
          },

          // Flexible metadata
          metadata: {
            customFields: 'object',
            source: 'string', // web, mobile, api
            importedFrom: 'string',
            externalIds: 'object', // For integration with other systems

            // A/B testing
            experiments: [
              {
                experimentId: 'string',
                variant: 'string',
                startDate: 'date',
                endDate: 'date'
              }
            ]
          }
        },

        // Validation rules for data integrity
        validationRules: {
          required: ['content.title', 'author.userId', 'publication.status'],
          unique: ['slug', 'postId'],
          patterns: {
            slug: /^[a-z0-9-]+$/,
            'content.contentType': /^(article|tutorial|review|announcement|news)$/
          },
          ranges: {
            'content.formatting.readingTime': { min: 0, max: 300 },
            'engagement.metrics.engagementRate': { min: 0, max: 100 }
          }
        },

        // Index strategies optimized for content queries
        indexStrategies: [
          { fields: { slug: 1 }, unique: true },
          { fields: { postId: 1 }, unique: true },
          { fields: { 'author.userId': 1, 'publication.publishedAt': -1 } },
          { fields: { 'publication.status': 1, 'publication.publishedAt': -1 } },
          { fields: { 'taxonomy.categories.name': 1 } },
          { fields: { 'taxonomy.tags.tag': 1 } },

          // Text search index
          { fields: { 'content.title': 'text', 'content.body': 'text', 'taxonomy.tags.tag': 'text' } },

          // Performance optimization indexes
          { fields: { 'engagement.views.total': -1, 'publication.publishedAt': -1 } },
          { fields: { 'publication.visibility': 1, 'engagement.views.total': -1 } },
          { fields: { 'content.contentType': 1, 'publication.publishedAt': -1 } }
        ]
      };

      // Store schema definition
      this.documentSchemas.set('posts', postSchema);

      console.log('Post schema defined with flexible content structure and performance optimization');

    } catch (error) {
      console.error('Error defining post schema:', error);
      throw error;
    }
  }

  async createOptimizedUserProfile(userData, profileData = {}) {
    console.log(`Creating optimized user profile: ${userData.username}`);

    try {
      const userDocument = {
        // Core identification
        userId: userData.userId || new ObjectId().toString(),
        username: userData.username,
        email: userData.email,

        // Personal information (embedded)
        profile: {
          firstName: profileData.firstName || '',
          lastName: profileData.lastName || '',
          displayName: profileData.displayName || `${profileData.firstName} ${profileData.lastName}`.trim(),
          bio: profileData.bio || '',
          dateOfBirth: profileData.dateOfBirth ? new Date(profileData.dateOfBirth) : null,
          phoneNumber: profileData.phoneNumber || '',

          // Professional information
          company: profileData.company || '',
          jobTitle: profileData.jobTitle || '',
          yearsOfExperience: profileData.yearsOfExperience || 0,
          educationLevel: profileData.educationLevel || '',

          // Skills and interests
          skills: profileData.skills || [],
          interests: profileData.interests || [],
          languages: profileData.languages || [
            { language: 'English', proficiency: 'native' }
          ],

          // Social media links
          socialMedia: {
            facebook: profileData.socialMedia?.facebook || '',
            twitter: profileData.socialMedia?.twitter || '',
            linkedin: profileData.socialMedia?.linkedin || '',
            instagram: profileData.socialMedia?.instagram || '',
            github: profileData.socialMedia?.github || '',
            website: profileData.socialMedia?.website || ''
          },

          // Profile media
          profilePicture: profileData.profilePicture ? {
            url: profileData.profilePicture.url,
            thumbnailUrl: profileData.profilePicture.thumbnailUrl || profileData.profilePicture.url,
            uploadedAt: new Date(),
            fileSize: profileData.profilePicture.fileSize || 0,
            dimensions: profileData.profilePicture.dimensions || { width: 0, height: 0 }
          } : null
        },

        // Contact information
        contact: {
          addresses: profileData.addresses || [],
          phoneNumbers: profileData.phoneNumbers || [],
          emailAddresses: [
            {
              email: userData.email,
              type: 'primary',
              isVerified: false,
              isPrimary: true
            }
          ]
        },

        // Account settings with sensible defaults
        settings: {
          privacy: {
            profileVisibility: 'public',
            emailVisible: false,
            phoneVisible: false,
            searchable: true
          },

          notifications: {
            email: {
              posts: true,
              comments: true,
              mentions: true,
              messages: true,
              newsletter: false,
              marketing: false
            },
            push: {
              posts: true,
              comments: true,
              mentions: true,
              messages: true
            },
            sms: {
              security: true,
              important: false
            }
          },

          interface: {
            theme: 'light',
            language: 'en',
            timezone: 'UTC',
            dateFormat: 'MM/DD/YYYY',
            currency: 'USD'
          },

          content: {
            defaultPostVisibility: 'public',
            autoSaveEnabled: true,
            contentLanguages: ['en']
          }
        },

        // Activity tracking
        activity: {
          createdAt: new Date(),
          updatedAt: new Date(),
          lastLoginAt: new Date(),
          lastActiveAt: new Date(),

          status: 'active',
          emailVerifiedAt: null,
          phoneVerifiedAt: null,

          // Initialize statistics
          stats: {
            totalPosts: 0,
            publishedPosts: 0,
            totalComments: 0,
            totalLikes: 0,
            totalViews: 0,
            followersCount: 0,
            followingCount: 0,
            engagementRate: 0,
            averagePostViews: 0,
            profileCompleteness: this.calculateProfileCompleteness(profileData)
          },

          recentActivities: [
            {
              type: 'account_created',
              timestamp: new Date(),
              details: { source: 'registration' }
            }
          ]
        },

        // Authentication (placeholder - would be handled by auth system)
        authentication: {
          passwordHash: '', // Would be set by authentication system
          passwordSalt: '',
          lastPasswordChange: new Date(),
          twoFactorEnabled: false,
          activeSessions: [],
          securityEvents: []
        },

        // Initialize content relationships
        content: {
          recentPosts: [],
          favoritePostIds: [],
          bookmarks: []
        },

        // Initialize social relationships
        social: {
          following: [],
          followerIds: [],
          groups: []
        },

        // Metadata
        metadata: {
          customFields: profileData.customFields || {},
          tags: profileData.tags || [],
          categories: profileData.categories || [],
          source: profileData.source || 'direct_registration',
          referrer: profileData.referrer || ''
        }
      };

      // Insert user document
      const result = await this.db.collection('users').insertOne(userDocument);

      // Update activity statistics
      await this.updateUserStatistics(result.insertedId);

      return {
        success: true,
        userId: result.insertedId,
        userDocument: userDocument,
        profileCompleteness: userDocument.activity.stats.profileCompleteness
      };

    } catch (error) {
      console.error(`Error creating user profile for ${userData.username}:`, error);
      return {
        success: false,
        error: error.message,
        username: userData.username
      };
    }
  }

  async createOptimizedPost(postData, authorId) {
    console.log(`Creating optimized post: ${postData.title}`);

    try {
      // Get author information for denormalization
      const author = await this.db.collection('users').findOne(
        { _id: new ObjectId(authorId) },
        {
          projection: {
            username: 1,
            'profile.displayName': 1,
            'profile.profilePicture.url': 1,
            'activity.stats.totalPosts': 1,
            'activity.stats.followersCount': 1
          }
        }
      );

      if (!author) {
        throw new Error('Author not found');
      }

      const postDocument = {
        // Core identification
        postId: postData.postId || new ObjectId().toString(),
        slug: postData.slug || this.generateSlug(postData.title),

        // Author information (denormalized)
        author: {
          userId: new ObjectId(authorId),
          username: author.username,
          displayName: author.profile?.displayName || author.username,
          profilePicture: author.profile?.profilePicture?.url || '',
          totalPosts: author.activity?.stats?.totalPosts || 0,
          followerCount: author.activity?.stats?.followersCount || 0,
          verified: false // Would be determined by verification system
        },

        // Content structure
        content: {
          title: postData.title,
          subtitle: postData.subtitle || '',
          excerpt: postData.excerpt || this.generateExcerpt(postData.body),
          body: postData.body,
          contentType: postData.contentType || 'article',

          // Media content
          media: postData.media || [],

          // Content sections (for structured content)
          sections: this.parseContentSections(postData.body),

          // SEO optimization
          seo: {
            metaTitle: postData.seo?.metaTitle || postData.title,
            metaDescription: postData.seo?.metaDescription || postData.excerpt,
            keywords: postData.seo?.keywords || this.extractKeywords(postData.body),
            canonicalUrl: postData.seo?.canonicalUrl || '',
            openGraphImage: postData.featuredImage || ''
          },

          // Content formatting
          formatting: {
            readingTime: this.calculateReadingTime(postData.body),
            wordCount: this.calculateWordCount(postData.body),
            language: postData.language || 'en',
            rtlDirection: postData.rtlDirection || false
          }
        },

        // Publication settings
        publication: {
          status: postData.status || 'draft',
          visibility: postData.visibility || 'public',
          password: postData.password || '',

          createdAt: new Date(),
          updatedAt: new Date(),
          publishedAt: postData.status === 'published' ? new Date() : null,
          scheduledPublishAt: postData.scheduledPublishAt ? new Date(postData.scheduledPublishAt) : null,

          revisions: [
            {
              version: 1,
              changedAt: new Date(),
              changedBy: new ObjectId(authorId),
              changeType: 'content',
              changesSummary: 'Initial post creation'
            }
          ],

          allowComments: postData.allowComments !== false,
          allowSharing: postData.allowSharing !== false,
          allowIndexing: postData.allowIndexing !== false,
          requireApproval: postData.requireApproval || false
        },

        // Taxonomy
        taxonomy: {
          categories: (postData.categories || []).map(cat => ({
            categoryId: new ObjectId(),
            name: cat.name || cat,
            slug: this.generateSlug(cat.name || cat),
            level: cat.level || 1,
            parentCategory: cat.parent || ''
          })),

          tags: (postData.tags || []).map(tag => ({
            tag: typeof tag === 'string' ? tag : tag.name,
            relevanceScore: typeof tag === 'object' ? tag.relevance : 1.0,
            addedBy: new ObjectId(authorId),
            addedAt: new Date()
          })),

          customFields: postData.customFields || {}
        },

        // Initialize engagement metrics
        engagement: {
          views: {
            total: 0,
            unique: 0,
            today: 0,
            thisWeek: 0,
            thisMonth: 0,
            sources: {
              direct: 0,
              social: 0,
              search: 0,
              referral: 0
            }
          },

          interactions: {
            likes: 0,
            dislikes: 0,
            shares: 0,
            bookmarks: 0,
            comments: {
              total: 0,
              approved: 0,
              pending: 0,
              spam: 0
            }
          },

          metrics: {
            engagementRate: 0,
            averageTimeOnPage: 0,
            bounceRate: 0,
            socialShares: 0
          },

          topComments: []
        },

        // Initialize comments
        comments: {
          recent: [],
          statistics: {
            totalComments: 0,
            approvedComments: 0,
            pendingComments: 0,
            lastCommentAt: null
          }
        },

        // Performance data
        performance: {
          lastCached: null,
          cacheVersion: '1.0',
          searchTerms: this.extractSearchTerms(postData.title, postData.body),
          searchBoost: postData.searchBoost || 1.0,
          sentiment: this.analyzeSentiment(postData.body),
          readabilityScore: this.calculateReadabilityScore(postData.body),
          complexity: this.assessComplexity(postData.body)
        },

        // Metadata
        metadata: {
          customFields: postData.metadata || {},
          source: postData.source || 'web',
          importedFrom: postData.importedFrom || '',
          externalIds: postData.externalIds || {},
          experiments: postData.experiments || []
        }
      };

      // Insert post document
      const result = await this.db.collection('posts').insertOne(postDocument);

      // Update author statistics
      await this.updateAuthorStatistics(authorId, 'post_created');

      // Update user's recent posts
      await this.updateUserRecentPosts(authorId, result.insertedId, postDocument);

      return {
        success: true,
        postId: result.insertedId,
        postDocument: postDocument,
        readingTime: postDocument.content.formatting.readingTime,
        wordCount: postDocument.content.formatting.wordCount
      };

    } catch (error) {
      console.error(`Error creating post '${postData.title}':`, error);
      return {
        success: false,
        error: error.message,
        title: postData.title
      };
    }
  }

  async performAdvancedQuery(queryOptions) {
    console.log('Executing advanced MongoDB query with optimized document structure...');

    try {
      const {
        collection,
        filters = {},
        projection = {},
        sort = {},
        limit = 50,
        skip = 0,
        includeRelated = false
      } = queryOptions;

      // Build aggregation pipeline for complex queries
      const pipeline = [];

      // Match stage
      if (Object.keys(filters).length > 0) {
        pipeline.push({ $match: filters });
      }

      // Add related data if requested
      if (includeRelated && collection === 'posts') {
        pipeline.push(
          // Add full comment documents for recent comments
          {
            $lookup: {
              from: 'comments',
              localField: '_id',
              foreignField: 'postId',
              as: 'fullComments',
              pipeline: [
                { $match: { status: 'approved' } },
                { $sort: { createdAt: -1 } },
                { $limit: 10 }
              ]
            }
          },

          // Add author's full profile
          {
            $lookup: {
              from: 'users',
              localField: 'author.userId',
              foreignField: '_id',
              as: 'authorProfile',
              pipeline: [
                {
                  $project: {
                    username: 1,
                    'profile.displayName': 1,
                    'profile.bio': 1,
                    'profile.profilePicture': 1,
                    'activity.stats': 1
                  }
                }
              ]
            }
          }
        );
      }

      // Projection stage
      if (Object.keys(projection).length > 0) {
        pipeline.push({ $project: projection });
      }

      // Sort stage
      if (Object.keys(sort).length > 0) {
        pipeline.push({ $sort: sort });
      }

      // Pagination
      if (skip > 0) {
        pipeline.push({ $skip: skip });
      }

      if (limit > 0) {
        pipeline.push({ $limit: limit });
      }

      // Execute aggregation
      const results = await this.db.collection(collection).aggregate(pipeline).toArray();

      return {
        success: true,
        results: results,
        count: results.length,
        pipeline: pipeline
      };

    } catch (error) {
      console.error('Error executing advanced query:', error);
      return {
        success: false,
        error: error.message,
        queryOptions: queryOptions
      };
    }
  }

  // Utility methods for document processing and optimization

  calculateProfileCompleteness(profileData) {
    let score = 0;
    const maxScore = 100;

    // Basic information (40 points)
    if (profileData.firstName) score += 10;
    if (profileData.lastName) score += 10;
    if (profileData.bio) score += 10;
    if (profileData.profilePicture) score += 10;

    // Professional information (30 points)
    if (profileData.company) score += 10;
    if (profileData.jobTitle) score += 10;
    if (profileData.skills && profileData.skills.length > 0) score += 10;

    // Contact information (20 points)
    if (profileData.phoneNumber) score += 10;
    if (profileData.addresses && profileData.addresses.length > 0) score += 10;

    // Additional information (10 points)
    if (profileData.socialMedia && Object.values(profileData.socialMedia).some(url => url)) score += 10;

    return Math.min(score, maxScore);
  }

  generateSlug(title) {
    return title
      .toLowerCase()
      .replace(/[^a-z0-9\s-]/g, '')
      .replace(/\s+/g, '-')
      .replace(/-+/g, '-')
      .trim('-');
  }

  generateExcerpt(body, maxLength = 200) {
    const text = body.replace(/<[^>]*>/g, '').trim(); // Remove HTML tags
    return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
  }

  calculateReadingTime(text) {
    const wordsPerMinute = 200;
    const wordCount = this.calculateWordCount(text);
    return Math.ceil(wordCount / wordsPerMinute);
  }

  calculateWordCount(text) {
    const cleanText = text.replace(/<[^>]*>/g, '').trim(); // Remove HTML tags
    return cleanText.split(/\s+/).filter(word => word.length > 0).length;
  }

  extractKeywords(text, maxKeywords = 10) {
    // Simple keyword extraction - in production, use NLP libraries
    const words = text.toLowerCase().match(/\b\w{4,}\b/g) || [];
    const frequency = {};

    words.forEach(word => {
      frequency[word] = (frequency[word] || 0) + 1;
    });

    return Object.entries(frequency)
      .sort(([, a], [, b]) => b - a)
      .slice(0, maxKeywords)
      .map(([word]) => word);
  }

  extractSearchTerms(title, body) {
    const titleWords = title.toLowerCase().match(/\b\w{3,}\b/g) || [];
    const bodyWords = this.extractKeywords(body, 20);
    return [...new Set([...titleWords, ...bodyWords])];
  }

  parseContentSections(body) {
    // Simple section parsing - would be more sophisticated in production
    const sections = [];
    const lines = body.split('\n');
    let order = 0;

    lines.forEach(line => {
      const trimmed = line.trim();
      if (trimmed.startsWith('#')) {
        const level = trimmed.match(/^#+/)[0].length;
        sections.push({
          type: 'heading',
          content: trimmed.replace(/^#+\s*/, ''),
          level: level,
          order: order++
        });
      } else if (trimmed.startsWith('```')) {
        sections.push({
          type: 'code',
          content: trimmed.replace(/```(\w+)?/, ''),
          language: trimmed.match(/```(\w+)/)?.[1] || 'text',
          order: order++
        });
      } else if (trimmed.length > 0) {
        sections.push({
          type: 'paragraph',
          content: trimmed,
          order: order++
        });
      }
    });

    return sections;
  }

  analyzeSentiment(text) {
    // Placeholder sentiment analysis - use proper NLP library in production
    const positiveWords = ['good', 'great', 'excellent', 'amazing', 'wonderful', 'fantastic'];
    const negativeWords = ['bad', 'terrible', 'awful', 'horrible', 'disappointing'];

    const words = text.toLowerCase().split(/\s+/);
    let score = 0;

    words.forEach(word => {
      if (positiveWords.includes(word)) score += 0.1;
      if (negativeWords.includes(word)) score -= 0.1;
    });

    return {
      score: Math.max(-1, Math.min(1, score)),
      magnitude: Math.abs(score),
      language: 'en'
    };
  }

  calculateReadabilityScore(text) {
    // Simple readability calculation - use proper libraries in production
    const sentences = text.split(/[.!?]+/).filter(s => s.trim().length > 0);
    const words = text.split(/\s+/);
    const avgWordsPerSentence = words.length / sentences.length;

    // Simple scoring based on average sentence length
    if (avgWordsPerSentence < 15) return 90;
    if (avgWordsPerSentence < 20) return 70;
    if (avgWordsPerSentence < 25) return 50;
    return 30;
  }

  assessComplexity(text) {
    const wordCount = this.calculateWordCount(text);
    const readabilityScore = this.calculateReadabilityScore(text);

    if (wordCount < 500 && readabilityScore > 70) return 'simple';
    if (wordCount < 2000 && readabilityScore > 50) return 'moderate';
    return 'complex';
  }

  async updateUserStatistics(userId) {
    // Update user statistics after profile changes
    await this.db.collection('users').updateOne(
      { _id: new ObjectId(userId) },
      {
        $set: {
          'activity.updatedAt': new Date()
        }
      }
    );
  }

  async updateAuthorStatistics(authorId, action) {
    const updates = {};

    if (action === 'post_created') {
      updates['$inc'] = {
        'activity.stats.totalPosts': 1
      };
    }

    updates['$set'] = {
      'activity.updatedAt': new Date(),
      'activity.lastActiveAt': new Date()
    };

    await this.db.collection('users').updateOne(
      { _id: new ObjectId(authorId) },
      updates
    );
  }

  async updateUserRecentPosts(userId, postId, postDocument) {
    await this.db.collection('users').updateOne(
      { _id: new ObjectId(userId) },
      {
        $push: {
          'content.recentPosts': {
            $each: [
              {
                postId: postId,
                title: postDocument.content.title,
                createdAt: postDocument.publication.createdAt,
                status: postDocument.publication.status,
                viewCount: 0,
                likeCount: 0
              }
            ],
            $slice: -10 // Keep only the 10 most recent posts
          }
        }
      }
    );
  }

  async setupOptimizedIndexes() {
    console.log('Setting up optimized indexes for document collections...');

    try {
      // Apply indexes from schema definitions
      for (const [collectionName, schema] of this.documentSchemas.entries()) {
        const collection = this.db.collection(collectionName);

        for (const indexStrategy of schema.indexStrategies) {
          await collection.createIndex(indexStrategy.fields, {
            background: true,
            unique: indexStrategy.unique || false,
            sparse: indexStrategy.sparse || false,
            partialFilterExpression: indexStrategy.partialFilterExpression
          });
        }
      }

      console.log('Optimized indexes created successfully');

    } catch (error) {
      console.error('Error setting up optimized indexes:', error);
      throw error;
    }
  }

  async applySchemaValidation() {
    console.log('Applying schema validation rules...');

    try {
      // Apply validation for users collection
      await this.db.createCollection('users', {
        validator: {
          $jsonSchema: {
            bsonType: 'object',
            required: ['username', 'email'],
            properties: {
              username: {
                bsonType: 'string',
                pattern: '^[a-zA-Z0-9_]{3,30}$',
                description: 'Username must be 3-30 characters with only letters, numbers, and underscores'
              },
              email: {
                bsonType: 'string',
                pattern: '^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$',
                description: 'Valid email address required'
              },
              'profile.yearsOfExperience': {
                bsonType: 'int',
                minimum: 0,
                maximum: 70,
                description: 'Years of experience must be between 0 and 70'
              }
            }
          }
        },
        validationLevel: this.config.validationLevel,
        validationAction: this.config.strictValidation ? 'error' : 'warn'
      });

      // Apply validation for posts collection
      await this.db.createCollection('posts', {
        validator: {
          $jsonSchema: {
            bsonType: 'object',
            required: ['content.title', 'author.userId'],
            properties: {
              'content.title': {
                bsonType: 'string',
                minLength: 1,
                maxLength: 500,
                description: 'Post title is required and must be 1-500 characters'
              },
              'content.contentType': {
                bsonType: 'string',
                'enum': ['article', 'tutorial', 'review', 'announcement', 'news'],
                description: 'Content type must be one of the predefined values'
              },
              'publication.status': {
                bsonType: 'string',
                'enum': ['draft', 'review', 'published', 'archived', 'deleted'],
                description: 'Publication status must be one of the predefined values'
              }
            }
          }
        },
        validationLevel: this.config.validationLevel,
        validationAction: this.config.strictValidation ? 'error' : 'warn'
      });

      console.log('Schema validation rules applied successfully');

    } catch (error) {
      // Collections might already exist, which is fine
      if (!error.message.includes('already exists')) {
        console.error('Error applying schema validation:', error);
        throw error;
      }
    }
  }
}

// Benefits of MongoDB Advanced Data Modeling:
// - Flexible document structures that adapt to application requirements
// - Embedded relationships for optimal read performance and data locality
// - Denormalized data patterns for reduced join operations and improved query speed
// - Hierarchical data modeling with natural document nesting capabilities
// - Schema evolution support without complex migration procedures
// - Optimized indexing strategies for diverse query patterns
// - Rich data types including arrays, objects, and geospatial data
// - Query pattern optimization through strategic embedding and referencing
// - SQL-compatible operations through QueryLeaf integration
// - Production-ready data modeling patterns for scalable applications

module.exports = {
  AdvancedDataModelingManager
};

Understanding MongoDB Document Architecture

Advanced Schema Design and Relationship Optimization Patterns

Implement sophisticated data modeling workflows for enterprise MongoDB applications:

// Enterprise-grade data modeling with advanced relationship management capabilities
class EnterpriseDataModelingOrchestrator extends AdvancedDataModelingManager {
  constructor(mongoUri, enterpriseConfig) {
    super(mongoUri, enterpriseConfig);

    this.enterpriseConfig = {
      ...enterpriseConfig,
      enableAdvancedRelationships: true,
      enableDataGovernance: true,
      enablePerformanceOptimization: true,
      enableComplianceValidation: true,
      enableSchemaEvolution: true
    };

    this.setupEnterpriseCapabilities();
    this.initializeDataGovernance();
    this.setupAdvancedRelationshipManagement();
  }

  async implementAdvancedDataStrategy() {
    console.log('Implementing enterprise data modeling strategy...');

    const dataStrategy = {
      // Multi-tier data organization
      dataTiers: {
        operationalData: {
          embedding: 'aggressive',
          caching: 'memory',
          indexing: 'comprehensive',
          validation: 'strict'
        },
        analyticalData: {
          embedding: 'conservative',
          caching: 'disk',
          indexing: 'selective',
          validation: 'moderate'
        },
        archivalData: {
          embedding: 'minimal',
          caching: 'none',
          indexing: 'basic',
          validation: 'basic'
        }
      },

      // Advanced relationship management
      relationshipManagement: {
        dynamicReferencing: true,
        cascadingOperations: true,
        relationshipIndexing: true,
        crossCollectionValidation: true
      }
    };

    return await this.deployDataStrategy(dataStrategy);
  }

  async setupAdvancedDataGovernance() {
    console.log('Setting up enterprise data governance...');

    const governanceCapabilities = {
      // Data quality management
      dataQuality: {
        validationRules: true,
        dataCleansingPipelines: true,
        qualityMonitoring: true,
        anomalyDetection: true
      },

      // Compliance and auditing
      compliance: {
        dataLineage: true,
        auditTrails: true,
        privacyControls: true,
        retentionPolicies: true
      },

      // Schema governance
      schemaGovernance: {
        versionControl: true,
        changeApproval: true,
        backwardCompatibility: true,
        migrationAutomation: true
      }
    };

    return await this.deployDataGovernance(governanceCapabilities);
  }
}

SQL-Style Data Modeling with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB data modeling and schema operations:

-- QueryLeaf advanced data modeling operations with SQL-familiar syntax for MongoDB

-- Create comprehensive user profile schema with embedded relationships
CREATE DOCUMENT_SCHEMA user_profiles AS (
  -- Core identification
  user_id VARCHAR(24) PRIMARY KEY,
  username VARCHAR(255) UNIQUE NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL,

  -- Embedded personal information
  profile OBJECT(
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    display_name VARCHAR(200),
    bio TEXT,
    date_of_birth DATE,
    phone_number VARCHAR(20),

    -- Professional information (embedded object)
    professional OBJECT(
      company VARCHAR(255),
      job_title VARCHAR(255),
      years_experience INTEGER CHECK(years_experience >= 0 AND years_experience <= 70),
      education_level VARCHAR(100),
      skills ARRAY[VARCHAR(100)],
      languages ARRAY[OBJECT(
        language VARCHAR(50),
        proficiency VARCHAR(20) CHECK(proficiency IN ('beginner', 'intermediate', 'advanced', 'native'))
      )]
    ),

    -- Social media links (embedded object)
    social_media OBJECT(
      facebook VARCHAR(255),
      twitter VARCHAR(255),
      linkedin VARCHAR(255),
      instagram VARCHAR(255),
      github VARCHAR(255),
      website VARCHAR(255)
    ),

    -- Profile media (embedded object)
    profile_picture OBJECT(
      url VARCHAR(500),
      thumbnail_url VARCHAR(500),
      uploaded_at TIMESTAMP,
      file_size INTEGER,
      dimensions OBJECT(
        width INTEGER,
        height INTEGER
      )
    )
  ),

  -- Contact information (embedded array)
  contact OBJECT(
    addresses ARRAY[OBJECT(
      type VARCHAR(20) CHECK(type IN ('home', 'work', 'billing', 'shipping')),
      address_line_1 VARCHAR(255),
      address_line_2 VARCHAR(255),
      city VARCHAR(100),
      state VARCHAR(100),
      postal_code VARCHAR(20),
      country VARCHAR(100),
      is_primary BOOLEAN DEFAULT false,
      coordinates OBJECT(
        latitude DECIMAL(10, 7),
        longitude DECIMAL(10, 7)
      )
    )],

    phone_numbers ARRAY[OBJECT(
      type VARCHAR(20) CHECK(type IN ('mobile', 'home', 'work')),
      number VARCHAR(20),
      country_code VARCHAR(5),
      is_primary BOOLEAN DEFAULT false,
      is_verified BOOLEAN DEFAULT false
    )],

    email_addresses ARRAY[OBJECT(
      email VARCHAR(255),
      type VARCHAR(20) CHECK(type IN ('primary', 'work', 'personal')),
      is_verified BOOLEAN DEFAULT false,
      is_primary BOOLEAN DEFAULT false
    )]
  ),

  -- User settings (embedded object)
  settings OBJECT(
    privacy OBJECT(
      profile_visibility VARCHAR(20) CHECK(profile_visibility IN ('public', 'private', 'friends')) DEFAULT 'public',
      email_visible BOOLEAN DEFAULT false,
      phone_visible BOOLEAN DEFAULT false,
      searchable BOOLEAN DEFAULT true
    ),

    notifications OBJECT(
      email OBJECT(
        posts BOOLEAN DEFAULT true,
        comments BOOLEAN DEFAULT true,
        mentions BOOLEAN DEFAULT true,
        messages BOOLEAN DEFAULT true,
        newsletter BOOLEAN DEFAULT false,
        marketing BOOLEAN DEFAULT false
      ),
      push OBJECT(
        posts BOOLEAN DEFAULT true,
        comments BOOLEAN DEFAULT true,
        mentions BOOLEAN DEFAULT true,
        messages BOOLEAN DEFAULT true
      ),
      sms OBJECT(
        security BOOLEAN DEFAULT true,
        important BOOLEAN DEFAULT false
      )
    ),

    interface OBJECT(
      theme VARCHAR(20) CHECK(theme IN ('light', 'dark', 'auto')) DEFAULT 'light',
      language VARCHAR(10) DEFAULT 'en',
      timezone VARCHAR(50) DEFAULT 'UTC',
      date_format VARCHAR(20) DEFAULT 'MM/DD/YYYY',
      currency VARCHAR(3) DEFAULT 'USD'
    )
  ),

  -- Activity tracking (embedded object)
  activity OBJECT(
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_login_at TIMESTAMP,
    last_active_at TIMESTAMP,

    status VARCHAR(20) CHECK(status IN ('active', 'inactive', 'suspended', 'deleted')) DEFAULT 'active',
    email_verified_at TIMESTAMP,
    phone_verified_at TIMESTAMP,

    -- Denormalized statistics for performance
    stats OBJECT(
      total_posts INTEGER DEFAULT 0,
      published_posts INTEGER DEFAULT 0,
      total_comments INTEGER DEFAULT 0,
      total_likes INTEGER DEFAULT 0,
      total_views INTEGER DEFAULT 0,
      followers_count INTEGER DEFAULT 0,
      following_count INTEGER DEFAULT 0,
      engagement_rate DECIMAL(5,2) DEFAULT 0.0,
      average_post_views DECIMAL(10,2) DEFAULT 0.0,
      profile_completeness DECIMAL(5,2) DEFAULT 0.0
    ),

    -- Recent activities (embedded array with limited size)
    recent_activities ARRAY[OBJECT(
      type VARCHAR(50),
      timestamp TIMESTAMP,
      details OBJECT,
      ip_address VARCHAR(45),
      user_agent VARCHAR(500)
    )] -- Limited to last 50 activities
  ),

  -- Content relationships (selective embedding/referencing)
  content OBJECT(
    -- Recent posts embedded for performance
    recent_posts ARRAY[OBJECT(
      post_id VARCHAR(24),
      title VARCHAR(500),
      created_at TIMESTAMP,
      status VARCHAR(20),
      view_count INTEGER,
      like_count INTEGER
    )] -- Limited to last 10 posts

    -- Large collections referenced
    favorite_post_ids ARRAY[VARCHAR(24)],

    -- Bookmarks with metadata
    bookmarks ARRAY[OBJECT(
      content_id VARCHAR(24),
      content_type VARCHAR(20) CHECK(content_type IN ('post', 'comment', 'user')),
      bookmarked_at TIMESTAMP,
      tags ARRAY[VARCHAR(50)],
      notes TEXT
    )]
  ),

  -- Social relationships (hybrid approach)
  social OBJECT(
    -- Following relationships (embedded for moderate size)
    following ARRAY[OBJECT(
      user_id VARCHAR(24),
      username VARCHAR(255),
      followed_at TIMESTAMP,
      relationship_type VARCHAR(20) CHECK(relationship_type IN ('friend', 'colleague', 'interest'))
    )],

    -- Large follower lists referenced
    follower_ids ARRAY[VARCHAR(24)],

    -- Group memberships
    groups ARRAY[OBJECT(
      group_id VARCHAR(24),
      group_name VARCHAR(255),
      role VARCHAR(20) CHECK(role IN ('member', 'moderator', 'admin')),
      joined_at TIMESTAMP
    )]
  ),

  -- Flexible metadata for extensibility
  metadata OBJECT(
    custom_fields OBJECT,
    tags ARRAY[VARCHAR(50)],
    categories ARRAY[VARCHAR(50)],
    source VARCHAR(100),
    referrer VARCHAR(255)
  ),

  -- Indexes for optimal performance
  INDEX idx_username (username),
  INDEX idx_email (email),
  INDEX idx_status_last_active (activity.status, activity.last_active_at DESC),
  INDEX idx_skills (profile.professional.skills),
  INDEX idx_location (contact.addresses.city, contact.addresses.state),

  -- Text search index
  INDEX idx_text_search ON (
    username TEXT,
    profile.display_name TEXT,
    profile.bio TEXT,
    profile.professional.skills TEXT
  ),

  -- Compound indexes for common query patterns
  INDEX idx_visibility_stats (settings.privacy.profile_visibility, activity.stats.total_posts DESC),
  INDEX idx_company_role (profile.professional.company, profile.professional.job_title)
);

-- Advanced post schema with flexible content structure
CREATE DOCUMENT_SCHEMA content_posts AS (
  -- Core identification
  post_id VARCHAR(24) PRIMARY KEY,
  slug VARCHAR(500) UNIQUE NOT NULL,

  -- Author information (denormalized for performance)
  author OBJECT(
    user_id VARCHAR(24) NOT NULL,
    username VARCHAR(255) NOT NULL,
    display_name VARCHAR(200),
    profile_picture VARCHAR(500),
    total_posts INTEGER,
    follower_count INTEGER,
    verified BOOLEAN DEFAULT false
  ),

  -- Flexible content structure
  content OBJECT(
    title VARCHAR(500) NOT NULL,
    subtitle VARCHAR(500),
    excerpt TEXT,
    body TEXT NOT NULL,
    content_type VARCHAR(20) CHECK(content_type IN ('article', 'tutorial', 'review', 'announcement', 'news')) DEFAULT 'article',

    -- Rich media content
    media ARRAY[OBJECT(
      type VARCHAR(20) CHECK(type IN ('image', 'video', 'audio', 'embed')),
      url VARCHAR(1000),
      thumbnail_url VARCHAR(1000),
      caption TEXT,
      alt_text TEXT,
      dimensions OBJECT(
        width INTEGER,
        height INTEGER
      ),
      file_size INTEGER,
      mime_type VARCHAR(100),
      duration INTEGER, -- For video/audio
      uploaded_at TIMESTAMP
    )],

    -- Structured content sections
    sections ARRAY[OBJECT(
      type VARCHAR(20) CHECK(type IN ('paragraph', 'heading', 'list', 'code', 'quote')),
      content TEXT,
      level INTEGER, -- For headings
      language VARCHAR(20), -- For code blocks
      order_index INTEGER
    )],

    -- SEO and metadata
    seo OBJECT(
      meta_title VARCHAR(500),
      meta_description TEXT,
      keywords ARRAY[VARCHAR(100)],
      canonical_url VARCHAR(1000),
      open_graph_image VARCHAR(1000),
      structured_data OBJECT
    ),

    -- Content analysis
    formatting OBJECT(
      reading_time INTEGER, -- Minutes
      word_count INTEGER,
      language VARCHAR(10) DEFAULT 'en',
      rtl_direction BOOLEAN DEFAULT false
    )
  ),

  -- Publication management
  publication OBJECT(
    status VARCHAR(20) CHECK(status IN ('draft', 'review', 'published', 'archived', 'deleted')) DEFAULT 'draft',
    visibility VARCHAR(20) CHECK(visibility IN ('public', 'private', 'unlisted', 'password_protected')) DEFAULT 'public',
    password VARCHAR(255),

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    published_at TIMESTAMP,
    scheduled_publish_at TIMESTAMP,

    -- Revision tracking (limited to recent changes)
    revisions ARRAY[OBJECT(
      version INTEGER,
      changed_at TIMESTAMP,
      changed_by VARCHAR(24),
      change_type VARCHAR(20) CHECK(change_type IN ('content', 'metadata', 'status')),
      changes_summary TEXT,
      previous_title VARCHAR(500),
      previous_content TEXT
    )] -- Limited to last 10 revisions

    allow_comments BOOLEAN DEFAULT true,
    allow_sharing BOOLEAN DEFAULT true,
    allow_indexing BOOLEAN DEFAULT true,
    require_approval BOOLEAN DEFAULT false
  ),

  -- Categorization and tagging
  taxonomy OBJECT(
    categories ARRAY[OBJECT(
      category_id VARCHAR(24),
      name VARCHAR(255),
      slug VARCHAR(255),
      level INTEGER,
      parent_category VARCHAR(255)
    )],

    tags ARRAY[OBJECT(
      tag VARCHAR(100),
      relevance_score DECIMAL(3,2) DEFAULT 1.0,
      added_by VARCHAR(24),
      added_at TIMESTAMP
    )],

    custom_fields OBJECT(
      difficulty VARCHAR(20), -- For tutorials
      estimated_time INTEGER, -- For how-to content
      target_audience VARCHAR(100),
      prerequisites ARRAY[VARCHAR(100)]
    )
  ),

  -- Engagement metrics (denormalized for performance)
  engagement OBJECT(
    views OBJECT(
      total INTEGER DEFAULT 0,
      unique INTEGER DEFAULT 0,
      today INTEGER DEFAULT 0,
      this_week INTEGER DEFAULT 0,
      this_month INTEGER DEFAULT 0,
      sources OBJECT(
        direct INTEGER DEFAULT 0,
        social INTEGER DEFAULT 0,
        search INTEGER DEFAULT 0,
        referral INTEGER DEFAULT 0
      )
    ),

    interactions OBJECT(
      likes INTEGER DEFAULT 0,
      dislikes INTEGER DEFAULT 0,
      shares INTEGER DEFAULT 0,
      bookmarks INTEGER DEFAULT 0,
      comments OBJECT(
        total INTEGER DEFAULT 0,
        approved INTEGER DEFAULT 0,
        pending INTEGER DEFAULT 0,
        spam INTEGER DEFAULT 0
      )
    ),

    metrics OBJECT(
      engagement_rate DECIMAL(5,2) DEFAULT 0.0,
      average_time_on_page INTEGER DEFAULT 0, -- Seconds
      bounce_rate DECIMAL(5,2) DEFAULT 0.0,
      social_shares INTEGER DEFAULT 0
    ),

    -- Top comments embedded for quick access
    top_comments ARRAY[OBJECT(
      comment_id VARCHAR(24),
      content TEXT,
      author OBJECT(
        user_id VARCHAR(24),
        username VARCHAR(255),
        profile_picture VARCHAR(500)
      ),
      created_at TIMESTAMP,
      like_count INTEGER,
      is_highlighted BOOLEAN DEFAULT false
    )] -- Limited to top 5 comments
  ),

  -- Comment management (hybrid approach)
  comments OBJECT(
    -- Recent comments embedded
    recent ARRAY[OBJECT(
      comment_id VARCHAR(24),
      parent_comment_id VARCHAR(24),
      content TEXT,
      author OBJECT(
        user_id VARCHAR(24),
        username VARCHAR(255),
        display_name VARCHAR(200),
        profile_picture VARCHAR(500)
      ),
      created_at TIMESTAMP,
      updated_at TIMESTAMP,
      status VARCHAR(20) CHECK(status IN ('approved', 'pending', 'spam', 'deleted')) DEFAULT 'approved',
      like_count INTEGER DEFAULT 0,
      reply_count INTEGER DEFAULT 0,
      is_edited BOOLEAN DEFAULT false,
      is_pinned BOOLEAN DEFAULT false,
      flags ARRAY[VARCHAR(50)],
      moderation_status VARCHAR(20)
    )] -- Limited to last 20 comments

    statistics OBJECT(
      total_comments INTEGER DEFAULT 0,
      approved_comments INTEGER DEFAULT 0,
      pending_comments INTEGER DEFAULT 0,
      last_comment_at TIMESTAMP
    )
  ),

  -- Performance optimization
  performance OBJECT(
    last_cached TIMESTAMP,
    cache_version VARCHAR(10),
    search_terms ARRAY[VARCHAR(100)],
    search_boost DECIMAL(3,2) DEFAULT 1.0,

    sentiment OBJECT(
      score DECIMAL(3,2), -- -1 to 1
      magnitude DECIMAL(3,2),
      language VARCHAR(10)
    ),

    readability_score INTEGER,
    complexity VARCHAR(20) CHECK(complexity IN ('simple', 'moderate', 'complex'))
  ),

  -- Flexible metadata
  metadata OBJECT(
    custom_fields OBJECT,
    source VARCHAR(50) DEFAULT 'web',
    imported_from VARCHAR(100),
    external_ids OBJECT,

    experiments ARRAY[OBJECT(
      experiment_id VARCHAR(50),
      variant VARCHAR(50),
      start_date DATE,
      end_date DATE
    )]
  ),

  -- Optimized indexes for content queries
  INDEX idx_slug (slug),
  INDEX idx_author_published (author.user_id, publication.published_at DESC),
  INDEX idx_status_published (publication.status, publication.published_at DESC),
  INDEX idx_categories (taxonomy.categories.name),
  INDEX idx_tags (taxonomy.tags.tag),
  INDEX idx_engagement (engagement.views.total DESC, publication.published_at DESC),

  -- Text search index for content
  INDEX idx_content_search ON (
    content.title TEXT,
    content.body TEXT,
    taxonomy.tags.tag TEXT,
    taxonomy.categories.name TEXT
  ),

  -- Compound indexes for complex queries
  INDEX idx_visibility_engagement (publication.visibility, engagement.views.total DESC),
  INDEX idx_type_published (content.content_type, publication.published_at DESC),
  INDEX idx_author_stats (author.user_id, engagement.interactions.likes DESC)
);

-- Advanced data modeling analysis and optimization queries
WITH document_structure_analysis AS (
  SELECT 
    collection_name,
    COUNT(*) as total_documents,

    -- Document size analysis
    AVG(BSON_SIZE(document)) as avg_document_size_bytes,
    MAX(BSON_SIZE(document)) as max_document_size_bytes,
    MIN(BSON_SIZE(document)) as min_document_size_bytes,

    -- Embedded array analysis
    AVG(ARRAY_LENGTH(profile.professional.skills)) as avg_skills_count,
    AVG(ARRAY_LENGTH(contact.addresses)) as avg_addresses_count,
    AVG(ARRAY_LENGTH(social.following)) as avg_following_count,

    -- Nested object complexity
    AVG(OBJECT_DEPTH(profile)) as avg_profile_depth,
    AVG(OBJECT_DEPTH(settings)) as avg_settings_depth,
    AVG(OBJECT_DEPTH(activity)) as avg_activity_depth,

    -- Data completeness analysis
    COUNT(*) FILTER (WHERE profile.first_name IS NOT NULL) as profiles_with_first_name,
    COUNT(*) FILTER (WHERE profile.bio IS NOT NULL) as profiles_with_bio,
    COUNT(*) FILTER (WHERE profile.professional.company IS NOT NULL) as profiles_with_company,
    COUNT(*) FILTER (WHERE contact.addresses IS NOT NULL AND ARRAY_LENGTH(contact.addresses) > 0) as profiles_with_address,

    -- Activity patterns
    AVG(activity.stats.total_posts) as avg_posts_per_user,
    AVG(activity.stats.profile_completeness) as avg_profile_completeness,

    -- Relationship analysis
    AVG(ARRAY_LENGTH(content.favorite_post_ids)) as avg_favorites_per_user,
    AVG(ARRAY_LENGTH(social.follower_ids)) as avg_followers_per_user

  FROM USER_PROFILES
  GROUP BY collection_name
),

performance_optimization_analysis AS (
  SELECT 
    dsa.*,

    -- Document size categorization
    CASE 
      WHEN dsa.avg_document_size_bytes < 16384 THEN 'optimal_size' -- < 16KB
      WHEN dsa.avg_document_size_bytes < 65536 THEN 'good_size'     -- < 64KB
      WHEN dsa.avg_document_size_bytes < 262144 THEN 'large_size'   -- < 256KB
      ELSE 'very_large_size'                                        -- >= 256KB
    END as document_size_category,

    -- Embedding effectiveness
    CASE 
      WHEN dsa.avg_skills_count > 20 THEN 'consider_referencing_skills'
      WHEN dsa.avg_following_count > 1000 THEN 'consider_referencing_following'
      WHEN dsa.avg_addresses_count > 5 THEN 'consider_referencing_addresses'
      ELSE 'embedding_appropriate'
    END as embedding_recommendation,

    -- Data completeness scoring
    ROUND(
      (dsa.profiles_with_first_name * 100.0 / dsa.total_documents + 
       dsa.profiles_with_bio * 100.0 / dsa.total_documents + 
       dsa.profiles_with_company * 100.0 / dsa.total_documents + 
       dsa.profiles_with_address * 100.0 / dsa.total_documents) / 4, 
      2
    ) as overall_data_completeness_percent,

    -- Performance indicators
    CASE 
      WHEN dsa.avg_profile_depth > 4 THEN 'consider_flattening_structure'
      WHEN dsa.max_document_size_bytes > 1048576 THEN 'critical_size_optimization_needed' -- > 1MB
      WHEN dsa.avg_followers_per_user > 10000 THEN 'implement_follower_pagination'
      ELSE 'structure_optimized'
    END as structure_optimization_recommendation,

    -- Index strategy recommendations
    ARRAY[
      CASE WHEN dsa.profiles_with_company * 100.0 / dsa.total_documents > 60 
           THEN 'Add index on profile.professional.company' END,
      CASE WHEN dsa.avg_skills_count > 3 
           THEN 'Optimize skills array indexing' END,
      CASE WHEN dsa.profiles_with_address * 100.0 / dsa.total_documents > 70 
           THEN 'Add geospatial index for addresses' END,
      CASE WHEN dsa.avg_posts_per_user > 50 
           THEN 'Consider post relationship optimization' END
    ]::TEXT[] as indexing_recommendations

  FROM document_structure_analysis dsa
),

content_modeling_analysis AS (
  SELECT 
    'content_posts' as collection_name,
    COUNT(*) as total_posts,

    -- Content structure analysis
    AVG(BSON_SIZE(content)) as avg_content_size_bytes,
    AVG(content.formatting.word_count) as avg_word_count,
    AVG(content.formatting.reading_time) as avg_reading_time_minutes,
    AVG(ARRAY_LENGTH(content.media)) as avg_media_items,

    -- Taxonomy analysis
    AVG(ARRAY_LENGTH(taxonomy.categories)) as avg_categories_per_post,
    AVG(ARRAY_LENGTH(taxonomy.tags)) as avg_tags_per_post,

    -- Engagement patterns
    AVG(engagement.views.total) as avg_total_views,
    AVG(engagement.interactions.likes) as avg_likes,
    AVG(engagement.interactions.comments.total) as avg_comments,

    -- Comment embedding analysis
    AVG(ARRAY_LENGTH(comments.recent)) as avg_embedded_comments,
    MAX(ARRAY_LENGTH(comments.recent)) as max_embedded_comments,

    -- Content type distribution
    COUNT(*) FILTER (WHERE content.content_type = 'article') as article_count,
    COUNT(*) FILTER (WHERE content.content_type = 'tutorial') as tutorial_count,
    COUNT(*) FILTER (WHERE content.content_type = 'review') as review_count,

    -- Publication patterns
    COUNT(*) FILTER (WHERE publication.status = 'published') as published_posts,
    COUNT(*) FILTER (WHERE publication.status = 'draft') as draft_posts,

    -- Performance metrics
    AVG(performance.readability_score) as avg_readability_score,
    COUNT(*) FILTER (WHERE performance.complexity = 'simple') as simple_content,
    COUNT(*) FILTER (WHERE performance.complexity = 'moderate') as moderate_content,
    COUNT(*) FILTER (WHERE performance.complexity = 'complex') as complex_content

  FROM CONTENT_POSTS
  WHERE publication.created_at >= CURRENT_TIMESTAMP - INTERVAL '30 days'
)

SELECT 
  poa.collection_name,
  poa.total_documents,
  poa.document_size_category,

  -- Size metrics
  ROUND(poa.avg_document_size_bytes / 1024.0, 2) as avg_size_kb,
  ROUND(poa.max_document_size_bytes / 1024.0, 2) as max_size_kb,

  -- Structure analysis
  ROUND(poa.avg_profile_depth, 1) as avg_nesting_depth,
  poa.embedding_recommendation,
  poa.structure_optimization_recommendation,

  -- Data quality
  ROUND(poa.overall_data_completeness_percent, 1) as data_completeness_percent,
  ROUND(poa.avg_profile_completeness, 1) as avg_profile_completeness,

  -- Relationship metrics
  ROUND(poa.avg_skills_count, 1) as avg_skills_per_user,
  ROUND(poa.avg_following_count, 1) as avg_following_per_user,
  ROUND(poa.avg_followers_per_user, 1) as avg_followers_per_user,

  -- Performance recommendations
  ARRAY_REMOVE(poa.indexing_recommendations, NULL) as optimization_recommendations,

  -- Data modeling assessment
  CASE 
    WHEN poa.document_size_category = 'very_large_size' THEN 'critical_optimization_needed'
    WHEN poa.embedding_recommendation != 'embedding_appropriate' THEN 'relationship_optimization_needed'
    WHEN poa.overall_data_completeness_percent < 60 THEN 'data_quality_improvement_needed'
    ELSE 'data_model_optimized'
  END as overall_assessment,

  -- Specific action items
  ARRAY[
    CASE WHEN poa.avg_document_size_bytes > 262144 
         THEN 'Split large documents or reference large arrays' END,
    CASE WHEN poa.overall_data_completeness_percent < 50 
         THEN 'Implement data validation and user onboarding improvements' END,
    CASE WHEN poa.avg_followers_per_user > 5000 
         THEN 'Implement follower pagination and lazy loading' END,
    CASE WHEN poa.max_document_size_bytes > 1048576 
         THEN 'URGENT: Address oversized documents immediately' END
  ]::TEXT[] as action_items,

  -- Performance impact
  CASE 
    WHEN poa.document_size_category IN ('large_size', 'very_large_size') THEN 'high_performance_impact'
    WHEN poa.embedding_recommendation != 'embedding_appropriate' THEN 'medium_performance_impact'
    ELSE 'low_performance_impact'
  END as performance_impact

FROM performance_optimization_analysis poa

UNION ALL

-- Content analysis results
SELECT 
  cma.collection_name,
  cma.total_posts as total_documents,

  CASE 
    WHEN cma.avg_content_size_bytes < 32768 THEN 'optimal_size'
    WHEN cma.avg_content_size_bytes < 131072 THEN 'good_size' 
    WHEN cma.avg_content_size_bytes < 524288 THEN 'large_size'
    ELSE 'very_large_size'
  END as document_size_category,

  ROUND(cma.avg_content_size_bytes / 1024.0, 2) as avg_size_kb,
  0 as max_size_kb, -- Placeholder for union compatibility

  0 as avg_nesting_depth, -- Placeholder

  CASE 
    WHEN cma.avg_media_items > 10 THEN 'consider_referencing_media'
    WHEN cma.max_embedded_comments > 50 THEN 'optimize_comment_embedding'
    ELSE 'embedding_appropriate'
  END as embedding_recommendation,

  CASE 
    WHEN cma.avg_content_size_bytes > 524288 THEN 'split_large_content'
    WHEN cma.avg_embedded_comments > 25 THEN 'implement_comment_pagination'
    ELSE 'structure_optimized'
  END as structure_optimization_recommendation,

  ROUND((cma.published_posts * 100.0 / cma.total_posts), 1) as data_completeness_percent,
  ROUND(cma.avg_readability_score, 1) as avg_profile_completeness,

  ROUND(cma.avg_categories_per_post, 1) as avg_skills_per_user,
  ROUND(cma.avg_tags_per_post, 1) as avg_following_per_user,
  ROUND(cma.avg_total_views, 0) as avg_followers_per_user,

  ARRAY[
    CASE WHEN cma.avg_word_count > 3000 THEN 'Consider content length optimization' END,
    CASE WHEN cma.avg_media_items > 5 THEN 'Optimize media storage and delivery' END,
    CASE WHEN cma.complex_content > cma.total_posts * 0.3 THEN 'Improve content readability' END
  ]::TEXT[] as optimization_recommendations,

  CASE 
    WHEN cma.avg_content_size_bytes > 524288 THEN 'critical_optimization_needed'
    WHEN cma.avg_embedded_comments > 25 THEN 'relationship_optimization_needed'
    ELSE 'data_model_optimized'
  END as overall_assessment,

  ARRAY[
    CASE WHEN cma.avg_content_size_bytes > 262144 THEN 'Optimize content storage and caching' END,
    CASE WHEN cma.max_embedded_comments > 50 THEN 'Implement comment pagination' END
  ]::TEXT[] as action_items,

  CASE 
    WHEN cma.avg_content_size_bytes > 262144 THEN 'high_performance_impact'
    ELSE 'low_performance_impact'
  END as performance_impact

FROM content_modeling_analysis cma
ORDER BY performance_impact DESC, total_documents DESC;

-- QueryLeaf provides comprehensive MongoDB data modeling capabilities:
-- 1. Flexible document schema design with embedded and referenced relationships
-- 2. Advanced validation rules and constraints for data integrity
-- 3. Optimized indexing strategies for diverse query patterns
-- 4. Performance-focused embedding and referencing decisions
-- 5. Schema evolution support with backward compatibility
-- 6. Data quality analysis and optimization recommendations
-- 7. SQL-familiar syntax for complex MongoDB data operations
-- 8. Enterprise-grade data governance and compliance features
-- 9. Automated performance optimization and monitoring
-- 10. Production-ready data modeling patterns for scalable applications

Best Practices for Production Data Modeling

Document Design Strategy and Performance Optimization

Essential principles for effective MongoDB data modeling in production environments:

  1. Embedding vs. Referencing Strategy: Design optimal data relationships based on access patterns, update frequency, and document size constraints
  2. Schema Evolution Planning: Implement flexible schemas that can evolve with application requirements while maintaining backward compatibility
  3. Performance-First Design: Optimize document structures for common query patterns and minimize the need for complex aggregations
  4. Data Integrity Management: Establish validation rules, referential integrity patterns, and data quality monitoring procedures
  5. Indexing Strategy: Design comprehensive indexing strategies that support diverse query patterns while minimizing storage overhead
  6. Scalability Considerations: Plan for growth patterns and design document structures that scale efficiently with data volume

Enterprise Data Governance

Implement comprehensive data governance for enterprise-scale applications:

  1. Data Quality Framework: Establish automated data validation, cleansing pipelines, and quality monitoring systems
  2. Schema Governance: Implement version control, change approval processes, and automated migration procedures for schema evolution
  3. Compliance Integration: Ensure data modeling patterns meet regulatory requirements and industry standards
  4. Performance Monitoring: Monitor query performance, document size growth, and relationship efficiency continuously
  5. Data Lifecycle Management: Design retention policies, archival strategies, and data purging procedures
  6. Documentation Standards: Maintain comprehensive documentation for schemas, relationships, and optimization decisions

Conclusion

MongoDB data modeling provides comprehensive document design capabilities that enable sophisticated relationship management, flexible schema evolution, and performance-optimized data structures through embedded documents, selective referencing, and intelligent denormalization strategies. The native document model and rich data types ensure that applications can represent complex data relationships naturally while maintaining optimal query performance.

Key MongoDB Data Modeling benefits include:

  • Flexible Document Structures: Rich document model with native support for arrays, embedded objects, and hierarchical data organization
  • Optimized Relationships: Strategic embedding and referencing patterns that balance performance, consistency, and maintainability
  • Schema Evolution: Dynamic schema capabilities that adapt to changing requirements without complex migration procedures
  • Performance Optimization: Document design patterns that minimize query complexity and maximize read/write efficiency
  • Data Integrity: Comprehensive validation rules, constraints, and referential integrity patterns for production data quality
  • SQL Accessibility: Familiar SQL-style data modeling operations through QueryLeaf for accessible document design

Whether you're designing user management systems, content platforms, e-commerce applications, or analytical systems, MongoDB data modeling with QueryLeaf's familiar SQL interface provides the foundation for sophisticated, scalable document-oriented applications.

QueryLeaf Integration: QueryLeaf automatically optimizes MongoDB data modeling operations while providing SQL-familiar syntax for schema design, relationship management, and validation rules. Advanced document structures, embedding strategies, and performance optimization are seamlessly handled through familiar SQL constructs, making sophisticated data modeling accessible to SQL-oriented development teams.

The combination of MongoDB's flexible document capabilities with SQL-style modeling operations makes it an ideal platform for applications requiring both complex data relationships and familiar database design patterns, ensuring your data architecture can evolve efficiently while maintaining performance and consistency as application complexity and data volume grow.

MongoDB GridFS Large File Storage and Management: Advanced Distributed File Systems and Binary Data Operations for Enterprise Applications

Modern applications require sophisticated file storage capabilities that can handle large binary files, multimedia content, and document management while providing distributed access, version control, and efficient streaming. Traditional file system approaches struggle with scalability, metadata management, and integration with database operations, leading to complex architecture with separate storage systems, synchronization challenges, and operational overhead that complicates application development and deployment.

MongoDB GridFS provides comprehensive large file storage through distributed binary data management, efficient chunk-based storage, integrated metadata handling, and streaming capabilities that enable seamless file operations within database transactions. Unlike traditional file systems that require separate storage infrastructure and complex synchronization, GridFS integrates file storage directly into MongoDB with automatic chunking, replica set distribution, and transactional consistency.

The Traditional Large File Storage Challenge

Conventional approaches to large file storage in application architectures face significant limitations:

-- Traditional file storage management - complex infrastructure with limited integration capabilities

-- Basic file metadata tracking table with minimal functionality
CREATE TABLE file_metadata (
    file_id SERIAL PRIMARY KEY,
    file_name VARCHAR(255) NOT NULL,
    file_path TEXT NOT NULL,
    file_type VARCHAR(100),
    mime_type VARCHAR(100),

    -- Basic file information (limited metadata)
    file_size_bytes BIGINT,
    created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    modified_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by VARCHAR(100),

    -- Storage location tracking (manual management)
    storage_location VARCHAR(200),
    storage_server VARCHAR(100),
    storage_partition VARCHAR(50),

    -- Basic versioning (very limited)
    version_number INTEGER DEFAULT 1,
    is_current_version BOOLEAN DEFAULT true,
    parent_file_id INTEGER REFERENCES file_metadata(file_id),

    -- Access control (basic)
    access_permissions VARCHAR(50) DEFAULT 'private',
    owner_user_id INTEGER,

    -- File status
    file_status VARCHAR(50) DEFAULT 'active',
    checksum VARCHAR(64),

    -- Backup and replication tracking
    backup_status VARCHAR(50) DEFAULT 'pending',
    last_backup_time TIMESTAMP,
    replication_status VARCHAR(50) DEFAULT 'single'
);

-- File chunk storage simulation (very basic)
CREATE TABLE file_chunks (
    chunk_id SERIAL PRIMARY KEY,
    file_id INTEGER REFERENCES file_metadata(file_id),
    chunk_number INTEGER NOT NULL,
    chunk_size_bytes INTEGER NOT NULL,

    -- Chunk storage (can't actually store binary data efficiently)
    chunk_data TEXT, -- Base64 encoded - very inefficient
    chunk_checksum VARCHAR(64),

    -- Storage tracking
    storage_location VARCHAR(200),
    compression_applied BOOLEAN DEFAULT false,
    compression_ratio DECIMAL(5,2),

    UNIQUE(file_id, chunk_number)
);

-- Manual file upload processing function (very limited functionality)
CREATE OR REPLACE FUNCTION process_file_upload(
    file_name_param VARCHAR(255),
    file_path_param TEXT,
    file_size_param BIGINT,
    chunk_size_param INTEGER DEFAULT 1048576 -- 1MB chunks
) RETURNS TABLE (
    upload_success BOOLEAN,
    file_id INTEGER,
    total_chunks INTEGER,
    processing_time_seconds INTEGER,
    error_message TEXT
) AS $$
DECLARE
    new_file_id INTEGER;
    total_chunks_count INTEGER;
    chunk_counter INTEGER := 1;
    processing_start TIMESTAMP;
    processing_end TIMESTAMP;
    upload_error TEXT := '';
    upload_result BOOLEAN := true;
    simulated_chunk_data TEXT;
BEGIN
    processing_start := clock_timestamp();

    BEGIN
        -- Calculate total chunks needed
        total_chunks_count := CEILING(file_size_param::DECIMAL / chunk_size_param);

        -- Create file metadata record
        INSERT INTO file_metadata (
            file_name, file_path, file_size_bytes, 
            storage_location, checksum
        )
        VALUES (
            file_name_param, file_path_param, file_size_param,
            '/storage/files/' || EXTRACT(YEAR FROM CURRENT_DATE) || '/' || 
            EXTRACT(MONTH FROM CURRENT_DATE) || '/',
            MD5(file_name_param || file_size_param::TEXT) -- Basic checksum
        )
        RETURNING file_metadata.file_id INTO new_file_id;

        -- Simulate chunk processing (very basic)
        WHILE chunk_counter <= total_chunks_count LOOP
            -- Calculate chunk size for this chunk
            DECLARE
                current_chunk_size INTEGER;
            BEGIN
                IF chunk_counter = total_chunks_count THEN
                    current_chunk_size := file_size_param - ((chunk_counter - 1) * chunk_size_param);
                ELSE
                    current_chunk_size := chunk_size_param;
                END IF;

                -- Simulate chunk data (can't actually handle binary data efficiently)
                simulated_chunk_data := 'chunk_' || chunk_counter || '_data_placeholder';

                -- Insert chunk record
                INSERT INTO file_chunks (
                    file_id, chunk_number, chunk_size_bytes, 
                    chunk_data, chunk_checksum, storage_location
                )
                VALUES (
                    new_file_id, chunk_counter, current_chunk_size,
                    simulated_chunk_data,
                    MD5(simulated_chunk_data),
                    '/storage/chunks/' || new_file_id || '/' || chunk_counter
                );

                chunk_counter := chunk_counter + 1;

                -- Simulate processing time
                PERFORM pg_sleep(0.01);
            END;
        END LOOP;

        -- Update file status
        UPDATE file_metadata 
        SET file_status = 'available',
            modified_date = clock_timestamp()
        WHERE file_id = new_file_id;

    EXCEPTION WHEN OTHERS THEN
        upload_result := false;
        upload_error := SQLERRM;

        -- Cleanup on failure
        DELETE FROM file_chunks WHERE file_id = new_file_id;
        DELETE FROM file_metadata WHERE file_id = new_file_id;
    END;

    processing_end := clock_timestamp();

    RETURN QUERY SELECT 
        upload_result,
        new_file_id,
        total_chunks_count,
        EXTRACT(SECONDS FROM processing_end - processing_start)::INTEGER,
        CASE WHEN NOT upload_result THEN upload_error ELSE NULL END;

END;
$$ LANGUAGE plpgsql;

-- Basic file download function (very limited streaming capabilities)
CREATE OR REPLACE FUNCTION download_file_chunks(file_id_param INTEGER)
RETURNS TABLE (
    chunk_number INTEGER,
    chunk_size_bytes INTEGER,
    chunk_data TEXT,
    download_order INTEGER
) AS $$
BEGIN
    -- Simple chunk retrieval (no streaming, no optimization)
    RETURN QUERY
    SELECT 
        fc.chunk_number,
        fc.chunk_size_bytes,
        fc.chunk_data,
        fc.chunk_number as download_order
    FROM file_chunks fc
    WHERE fc.file_id = file_id_param
    ORDER BY fc.chunk_number;

    -- Update download statistics (basic tracking)
    UPDATE file_metadata 
    SET modified_date = CURRENT_TIMESTAMP
    WHERE file_id = file_id_param;
END;
$$ LANGUAGE plpgsql;

-- Execute file upload simulation
SELECT * FROM process_file_upload('large_document.pdf', '/uploads/large_document.pdf', 50000000, 1048576);

-- Basic file management and cleanup
WITH file_storage_analysis AS (
    SELECT 
        fm.file_id,
        fm.file_name,
        fm.file_size_bytes,
        fm.created_date,
        fm.file_status,
        COUNT(fc.chunk_id) as total_chunks,
        SUM(fc.chunk_size_bytes) as total_chunk_size,

        -- Storage efficiency calculation (basic)
        CASE 
            WHEN fm.file_size_bytes > 0 THEN
                (SUM(fc.chunk_size_bytes)::DECIMAL / fm.file_size_bytes) * 100
            ELSE 0
        END as storage_efficiency_percent,

        -- Age analysis
        EXTRACT(DAYS FROM CURRENT_TIMESTAMP - fm.created_date) as file_age_days,

        -- Basic categorization
        CASE 
            WHEN fm.file_size_bytes > 100 * 1024 * 1024 THEN 'large'
            WHEN fm.file_size_bytes > 10 * 1024 * 1024 THEN 'medium'
            ELSE 'small'
        END as file_size_category

    FROM file_metadata fm
    LEFT JOIN file_chunks fc ON fm.file_id = fc.file_id
    WHERE fm.created_date >= CURRENT_DATE - INTERVAL '30 days'
    GROUP BY fm.file_id, fm.file_name, fm.file_size_bytes, fm.created_date, fm.file_status
)
SELECT 
    fsa.file_name,
    fsa.file_size_category,
    ROUND(fsa.file_size_bytes / 1024.0 / 1024.0, 2) as file_size_mb,
    fsa.total_chunks,
    ROUND(fsa.storage_efficiency_percent, 1) as storage_efficiency_percent,
    fsa.file_age_days,
    fsa.file_status,

    -- Storage recommendations (very basic)
    CASE 
        WHEN fsa.storage_efficiency_percent < 95 THEN 'check_chunk_integrity'
        WHEN fsa.file_age_days > 365 AND fsa.file_status = 'active' THEN 'consider_archiving'
        WHEN fsa.total_chunks = 0 THEN 'missing_chunks'
        ELSE 'normal'
    END as recommendation

FROM file_storage_analysis fsa
ORDER BY fsa.file_size_bytes DESC, fsa.created_date DESC;

-- Basic file cleanup (manual process)
WITH old_files AS (
    SELECT file_id, file_name, file_size_bytes
    FROM file_metadata
    WHERE created_date < CURRENT_DATE - INTERVAL '2 years'
    AND file_status = 'archived'
),
cleanup_chunks AS (
    DELETE FROM file_chunks
    WHERE file_id IN (SELECT file_id FROM old_files)
    RETURNING file_id, chunk_size_bytes
),
cleanup_files AS (
    DELETE FROM file_metadata
    WHERE file_id IN (SELECT file_id FROM old_files)
    RETURNING file_id, file_size_bytes
)
SELECT 
    COUNT(DISTINCT cf.file_id) as files_cleaned,
    SUM(cf.file_size_bytes) as total_space_freed_bytes,
    ROUND(SUM(cf.file_size_bytes) / 1024.0 / 1024.0 / 1024.0, 2) as space_freed_gb,
    COUNT(cc.file_id) as chunks_cleaned
FROM cleanup_files cf
LEFT JOIN cleanup_chunks cc ON cf.file_id = cc.file_id;

-- Problems with traditional file storage approaches:
-- 1. Inefficient binary data handling in relational databases
-- 2. Manual chunk management with no automatic optimization
-- 3. Limited streaming capabilities and poor performance for large files
-- 4. No built-in replication or distributed storage features
-- 5. Basic metadata management with limited search capabilities
-- 6. Complex backup and recovery procedures for file data
-- 7. No transactional consistency between file operations and database operations
-- 8. Limited scalability for high-volume file storage requirements
-- 9. No built-in compression or space optimization features
-- 10. Manual versioning and access control management

MongoDB GridFS provides comprehensive large file storage with advanced binary data management:

// MongoDB GridFS Advanced File Storage - comprehensive binary data management with streaming capabilities
const { MongoClient, GridFSBucket } = require('mongodb');
const fs = require('fs');
const { createReadStream, createWriteStream } = require('fs');
const { pipeline } = require('stream');
const { promisify } = require('util');
const crypto = require('crypto');
const { EventEmitter } = require('events');

// Comprehensive MongoDB GridFS File Manager
class AdvancedGridFSFileManager extends EventEmitter {
  constructor(connectionString, gridFSConfig = {}) {
    super();
    this.connectionString = connectionString;
    this.client = null;
    this.db = null;
    this.gridFSBuckets = new Map();

    // Advanced GridFS configuration
    this.config = {
      // Bucket configuration
      defaultBucket: gridFSConfig.defaultBucket || 'fs',
      customBuckets: gridFSConfig.customBuckets || {},
      chunkSizeBytes: gridFSConfig.chunkSizeBytes || 261120, // 255KB default

      // File management settings
      enableMetadataIndexing: gridFSConfig.enableMetadataIndexing !== false,
      enableVersionControl: gridFSConfig.enableVersionControl || false,
      enableCompression: gridFSConfig.enableCompression || false,
      enableEncryption: gridFSConfig.enableEncryption || false,

      // Storage optimization
      enableAutomaticCleanup: gridFSConfig.enableAutomaticCleanup || false,
      enableDeduplication: gridFSConfig.enableDeduplication || false,
      enableThumbnailGeneration: gridFSConfig.enableThumbnailGeneration || false,

      // Performance configuration
      enableParallelUploads: gridFSConfig.enableParallelUploads || false,
      maxConcurrentUploads: gridFSConfig.maxConcurrentUploads || 5,
      enableStreamingOptimization: gridFSConfig.enableStreamingOptimization || false,

      // Access control and security
      enableAccessControl: gridFSConfig.enableAccessControl || false,
      defaultPermissions: gridFSConfig.defaultPermissions || 'private',
      enableAuditLogging: gridFSConfig.enableAuditLogging || false,

      // Backup and replication
      enableBackupIntegration: gridFSConfig.enableBackupIntegration || false,
      enableReplicationMonitoring: gridFSConfig.enableReplicationMonitoring || false,

      // File processing
      enableContentAnalysis: gridFSConfig.enableContentAnalysis || false,
      enableVirusScan: gridFSConfig.enableVirusScan || false,
      enableFormatValidation: gridFSConfig.enableFormatValidation || false
    };

    // File management state
    this.activeUploads = new Map();
    this.activeDownloads = new Map();
    this.fileOperations = new Map();
    this.uploadQueue = [];

    // Performance metrics
    this.metrics = {
      totalFilesStored: 0,
      totalBytesStored: 0,
      averageUploadSpeed: 0,
      averageDownloadSpeed: 0,
      storageEfficiency: 0
    };

    this.initializeGridFS();
  }

  async initializeGridFS() {
    console.log('Initializing advanced GridFS file management...');

    try {
      // Connect to MongoDB
      this.client = new MongoClient(this.connectionString);
      await this.client.connect();
      this.db = this.client.db();

      // Initialize default GridFS bucket
      this.initializeBucket(this.config.defaultBucket);

      // Initialize custom buckets
      for (const [bucketName, bucketConfig] of Object.entries(this.config.customBuckets)) {
        this.initializeBucket(bucketName, bucketConfig);
      }

      // Setup metadata indexing
      if (this.config.enableMetadataIndexing) {
        await this.setupMetadataIndexing();
      }

      // Setup file processing pipeline
      await this.setupFileProcessingPipeline();

      // Initialize monitoring and metrics
      await this.setupMonitoringAndMetrics();

      console.log('Advanced GridFS file management initialized successfully');

    } catch (error) {
      console.error('Error initializing GridFS:', error);
      throw error;
    }
  }

  initializeBucket(bucketName, bucketConfig = {}) {
    const bucket = new GridFSBucket(this.db, {
      bucketName: bucketName,
      chunkSizeBytes: bucketConfig.chunkSizeBytes || this.config.chunkSizeBytes
    });

    this.gridFSBuckets.set(bucketName, {
      bucket: bucket,
      config: bucketConfig,
      stats: {
        totalFiles: 0,
        totalBytes: 0,
        averageFileSize: 0,
        lastActivity: new Date()
      }
    });

    console.log(`Initialized GridFS bucket: ${bucketName}`);
  }

  async setupMetadataIndexing() {
    console.log('Setting up metadata indexing for GridFS...');

    try {
      // Create indexes on files collection for efficient queries
      for (const [bucketName, bucketInfo] of this.gridFSBuckets.entries()) {
        const filesCollection = this.db.collection(`${bucketName}.files`);
        const chunksCollection = this.db.collection(`${bucketName}.chunks`);

        // Files collection indexes
        await filesCollection.createIndex(
          { filename: 1, uploadDate: -1 },
          { background: true }
        );

        await filesCollection.createIndex(
          { 'metadata.contentType': 1, uploadDate: -1 },
          { background: true }
        );

        await filesCollection.createIndex(
          { 'metadata.tags': 1 },
          { background: true }
        );

        await filesCollection.createIndex(
          { length: -1, uploadDate: -1 },
          { background: true }
        );

        // Chunks collection optimization
        await chunksCollection.createIndex(
          { files_id: 1, n: 1 },
          { unique: true, background: true }
        );
      }

    } catch (error) {
      console.error('Error setting up metadata indexing:', error);
      throw error;
    }
  }

  async uploadFile(filePath, options = {}) {
    console.log(`Starting file upload: ${filePath}`);

    const uploadId = this.generateUploadId();
    const startTime = Date.now();

    try {
      // Validate file exists and get stats
      const fileStats = await fs.promises.stat(filePath);

      // Prepare upload configuration
      const uploadConfig = {
        uploadId: uploadId,
        filePath: filePath,
        fileName: options.filename || path.basename(filePath),
        bucketName: options.bucket || this.config.defaultBucket,
        contentType: options.contentType || this.detectContentType(filePath),

        // File metadata
        metadata: {
          originalPath: filePath,
          fileSize: fileStats.size,
          uploadedBy: options.uploadedBy || 'system',
          uploadDate: new Date(),
          contentType: options.contentType || this.detectContentType(filePath),

          // Custom metadata
          tags: options.tags || [],
          category: options.category || 'general',
          permissions: options.permissions || this.config.defaultPermissions,

          // Processing configuration
          processOnUpload: options.processOnUpload || false,
          generateThumbnail: options.generateThumbnail || false,
          enableCompression: options.enableCompression || this.config.enableCompression,

          // Checksums for integrity
          checksums: {}
        },

        // Upload progress tracking
        progress: {
          bytesUploaded: 0,
          totalBytes: fileStats.size,
          percentComplete: 0,
          uploadSpeed: 0,
          estimatedTimeRemaining: 0
        }
      };

      // Get GridFS bucket
      const bucketInfo = this.gridFSBuckets.get(uploadConfig.bucketName);
      if (!bucketInfo) {
        throw new Error(`GridFS bucket not found: ${uploadConfig.bucketName}`);
      }

      // Store upload state
      this.activeUploads.set(uploadId, uploadConfig);

      // Calculate file checksum before upload
      if (this.config.enableDeduplication) {
        uploadConfig.metadata.checksums.md5 = await this.calculateFileChecksum(filePath, 'md5');
        uploadConfig.metadata.checksums.sha256 = await this.calculateFileChecksum(filePath, 'sha256');

        // Check for duplicate files
        const duplicate = await this.findDuplicateFile(uploadConfig.metadata.checksums.sha256, uploadConfig.bucketName);
        if (duplicate && options.skipDuplicates) {
          this.emit('duplicateDetected', {
            uploadId: uploadId,
            duplicateFileId: duplicate._id,
            fileName: uploadConfig.fileName
          });

          return {
            success: true,
            uploadId: uploadId,
            fileId: duplicate._id,
            isDuplicate: true,
            fileName: uploadConfig.fileName,
            fileSize: duplicate.length
          };
        }
      }

      // Create upload stream
      const uploadStream = bucketInfo.bucket.openUploadStream(uploadConfig.fileName, {
        chunkSizeBytes: bucketInfo.config.chunkSizeBytes || this.config.chunkSizeBytes,
        metadata: uploadConfig.metadata
      });

      // Create read stream from file
      const fileReadStream = createReadStream(filePath);

      // Track upload progress
      const progressTracker = this.createProgressTracker(uploadId, uploadConfig);

      // Pipeline streams with error handling
      const pipelineAsync = promisify(pipeline);

      await pipelineAsync(
        fileReadStream,
        progressTracker,
        uploadStream
      );

      // Update upload completion
      const endTime = Date.now();
      const duration = endTime - startTime;
      const fileId = uploadStream.id;

      uploadConfig.fileId = fileId;
      uploadConfig.status = 'completed';
      uploadConfig.duration = duration;
      uploadConfig.uploadSpeed = (fileStats.size / 1024 / 1024) / (duration / 1000); // MB/s

      // Update bucket statistics
      bucketInfo.stats.totalFiles++;
      bucketInfo.stats.totalBytes += fileStats.size;
      bucketInfo.stats.averageFileSize = bucketInfo.stats.totalBytes / bucketInfo.stats.totalFiles;
      bucketInfo.stats.lastActivity = new Date();

      // Post-processing
      if (uploadConfig.metadata.processOnUpload) {
        await this.processUploadedFile(fileId, uploadConfig);
      }

      // Update system metrics
      this.updateMetrics(uploadConfig);

      // Cleanup
      this.activeUploads.delete(uploadId);

      this.emit('uploadCompleted', {
        uploadId: uploadId,
        fileId: fileId,
        fileName: uploadConfig.fileName,
        fileSize: fileStats.size,
        duration: duration,
        uploadSpeed: uploadConfig.uploadSpeed
      });

      console.log(`File upload completed: ${uploadConfig.fileName} (${fileId})`);

      return {
        success: true,
        uploadId: uploadId,
        fileId: fileId,
        fileName: uploadConfig.fileName,
        fileSize: fileStats.size,
        duration: duration,
        uploadSpeed: uploadConfig.uploadSpeed,
        bucketName: uploadConfig.bucketName
      };

    } catch (error) {
      console.error(`File upload failed for ${uploadId}:`, error);

      // Update upload state
      const uploadConfig = this.activeUploads.get(uploadId);
      if (uploadConfig) {
        uploadConfig.status = 'failed';
        uploadConfig.error = error.message;
      }

      this.emit('uploadFailed', {
        uploadId: uploadId,
        fileName: options.filename || path.basename(filePath),
        error: error.message
      });

      return {
        success: false,
        uploadId: uploadId,
        error: error.message
      };
    }
  }

  createProgressTracker(uploadId, uploadConfig) {
    const { Transform } = require('stream');

    return new Transform({
      transform(chunk, encoding, callback) {
        // Update progress
        uploadConfig.progress.bytesUploaded += chunk.length;
        uploadConfig.progress.percentComplete = 
          (uploadConfig.progress.bytesUploaded / uploadConfig.progress.totalBytes) * 100;

        // Calculate upload speed
        const currentTime = Date.now();
        const timeElapsed = (currentTime - uploadConfig.startTime) / 1000; // seconds
        uploadConfig.progress.uploadSpeed = 
          (uploadConfig.progress.bytesUploaded / 1024 / 1024) / timeElapsed; // MB/s

        // Estimate time remaining
        const remainingBytes = uploadConfig.progress.totalBytes - uploadConfig.progress.bytesUploaded;
        uploadConfig.progress.estimatedTimeRemaining = 
          remainingBytes / (uploadConfig.progress.uploadSpeed * 1024 * 1024);

        // Emit progress update
        this.emit('uploadProgress', {
          uploadId: uploadId,
          progress: uploadConfig.progress
        });

        callback(null, chunk);
      }.bind(this)
    });
  }

  async downloadFile(fileId, downloadPath, options = {}) {
    console.log(`Starting file download: ${fileId} -> ${downloadPath}`);

    const downloadId = this.generateDownloadId();
    const startTime = Date.now();

    try {
      // Get bucket
      const bucketName = options.bucket || this.config.defaultBucket;
      const bucketInfo = this.gridFSBuckets.get(bucketName);
      if (!bucketInfo) {
        throw new Error(`GridFS bucket not found: ${bucketName}`);
      }

      // Get file metadata
      const fileMetadata = await this.getFileMetadata(fileId, bucketName);
      if (!fileMetadata) {
        throw new Error(`File not found: ${fileId}`);
      }

      // Prepare download configuration
      const downloadConfig = {
        downloadId: downloadId,
        fileId: fileId,
        downloadPath: downloadPath,
        bucketName: bucketName,
        fileSize: fileMetadata.length,

        // Download progress tracking
        progress: {
          bytesDownloaded: 0,
          totalBytes: fileMetadata.length,
          percentComplete: 0,
          downloadSpeed: 0,
          estimatedTimeRemaining: 0
        }
      };

      // Store download state
      this.activeDownloads.set(downloadId, downloadConfig);

      // Create download stream
      const downloadStream = bucketInfo.bucket.openDownloadStream(fileId);

      // Create write stream to file
      const fileWriteStream = createWriteStream(downloadPath);

      // Track download progress
      const progressTracker = this.createDownloadProgressTracker(downloadId, downloadConfig);

      // Pipeline streams
      const pipelineAsync = promisify(pipeline);

      await pipelineAsync(
        downloadStream,
        progressTracker,
        fileWriteStream
      );

      // Update download completion
      const endTime = Date.now();
      const duration = endTime - startTime;
      downloadConfig.duration = duration;
      downloadConfig.downloadSpeed = (fileMetadata.length / 1024 / 1024) / (duration / 1000); // MB/s

      // Cleanup
      this.activeDownloads.delete(downloadId);

      this.emit('downloadCompleted', {
        downloadId: downloadId,
        fileId: fileId,
        fileName: fileMetadata.filename,
        fileSize: fileMetadata.length,
        duration: duration,
        downloadSpeed: downloadConfig.downloadSpeed
      });

      console.log(`File download completed: ${fileMetadata.filename} (${fileId})`);

      return {
        success: true,
        downloadId: downloadId,
        fileId: fileId,
        fileName: fileMetadata.filename,
        fileSize: fileMetadata.length,
        duration: duration,
        downloadSpeed: downloadConfig.downloadSpeed
      };

    } catch (error) {
      console.error(`File download failed for ${downloadId}:`, error);

      // Cleanup
      this.activeDownloads.delete(downloadId);

      this.emit('downloadFailed', {
        downloadId: downloadId,
        fileId: fileId,
        error: error.message
      });

      return {
        success: false,
        downloadId: downloadId,
        fileId: fileId,
        error: error.message
      };
    }
  }

  createDownloadProgressTracker(downloadId, downloadConfig) {
    const { Transform } = require('stream');

    return new Transform({
      transform(chunk, encoding, callback) {
        // Update progress
        downloadConfig.progress.bytesDownloaded += chunk.length;
        downloadConfig.progress.percentComplete = 
          (downloadConfig.progress.bytesDownloaded / downloadConfig.progress.totalBytes) * 100;

        // Calculate download speed
        const currentTime = Date.now();
        const timeElapsed = (currentTime - downloadConfig.startTime) / 1000; // seconds
        downloadConfig.progress.downloadSpeed = 
          (downloadConfig.progress.bytesDownloaded / 1024 / 1024) / timeElapsed; // MB/s

        // Emit progress update
        this.emit('downloadProgress', {
          downloadId: downloadId,
          progress: downloadConfig.progress
        });

        callback(null, chunk);
      }.bind(this)
    });
  }

  async getFileMetadata(fileId, bucketName = null) {
    console.log(`Getting file metadata: ${fileId}`);

    try {
      bucketName = bucketName || this.config.defaultBucket;
      const filesCollection = this.db.collection(`${bucketName}.files`);

      const fileMetadata = await filesCollection.findOne({ _id: fileId });
      return fileMetadata;

    } catch (error) {
      console.error(`Error getting file metadata for ${fileId}:`, error);
      throw error;
    }
  }

  async searchFiles(searchCriteria, options = {}) {
    console.log('Searching files with criteria:', searchCriteria);

    try {
      const bucketName = options.bucket || this.config.defaultBucket;
      const filesCollection = this.db.collection(`${bucketName}.files`);

      // Build search query
      const query = {};

      // Text search on filename
      if (searchCriteria.filename) {
        query.filename = { $regex: searchCriteria.filename, $options: 'i' };
      }

      // Content type filter
      if (searchCriteria.contentType) {
        query['metadata.contentType'] = searchCriteria.contentType;
      }

      // Size range filter
      if (searchCriteria.sizeRange) {
        query.length = {};
        if (searchCriteria.sizeRange.min) {
          query.length.$gte = searchCriteria.sizeRange.min;
        }
        if (searchCriteria.sizeRange.max) {
          query.length.$lte = searchCriteria.sizeRange.max;
        }
      }

      // Date range filter
      if (searchCriteria.dateRange) {
        query.uploadDate = {};
        if (searchCriteria.dateRange.from) {
          query.uploadDate.$gte = new Date(searchCriteria.dateRange.from);
        }
        if (searchCriteria.dateRange.to) {
          query.uploadDate.$lte = new Date(searchCriteria.dateRange.to);
        }
      }

      // Tags filter
      if (searchCriteria.tags) {
        query['metadata.tags'] = { $in: searchCriteria.tags };
      }

      // Category filter
      if (searchCriteria.category) {
        query['metadata.category'] = searchCriteria.category;
      }

      // Execute search with pagination
      const limit = options.limit || 50;
      const skip = options.skip || 0;
      const sort = options.sort || { uploadDate: -1 };

      const files = await filesCollection
        .find(query)
        .sort(sort)
        .limit(limit)
        .skip(skip)
        .toArray();

      // Get total count for pagination
      const totalCount = await filesCollection.countDocuments(query);

      return {
        success: true,
        files: files.map(file => ({
          fileId: file._id,
          filename: file.filename,
          length: file.length,
          uploadDate: file.uploadDate,
          contentType: file.metadata?.contentType,
          tags: file.metadata?.tags || [],
          category: file.metadata?.category,
          checksums: file.metadata?.checksums || {}
        })),
        totalCount: totalCount,
        currentPage: Math.floor(skip / limit) + 1,
        totalPages: Math.ceil(totalCount / limit)
      };

    } catch (error) {
      console.error('Error searching files:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async deleteFile(fileId, bucketName = null) {
    console.log(`Deleting file: ${fileId}`);

    try {
      bucketName = bucketName || this.config.defaultBucket;
      const bucketInfo = this.gridFSBuckets.get(bucketName);
      if (!bucketInfo) {
        throw new Error(`GridFS bucket not found: ${bucketName}`);
      }

      // Get file metadata before deletion
      const fileMetadata = await this.getFileMetadata(fileId, bucketName);
      if (!fileMetadata) {
        throw new Error(`File not found: ${fileId}`);
      }

      // Delete file from GridFS
      await bucketInfo.bucket.delete(fileId);

      // Update bucket statistics
      bucketInfo.stats.totalFiles = Math.max(0, bucketInfo.stats.totalFiles - 1);
      bucketInfo.stats.totalBytes = Math.max(0, bucketInfo.stats.totalBytes - fileMetadata.length);
      if (bucketInfo.stats.totalFiles > 0) {
        bucketInfo.stats.averageFileSize = bucketInfo.stats.totalBytes / bucketInfo.stats.totalFiles;
      }
      bucketInfo.stats.lastActivity = new Date();

      this.emit('fileDeleted', {
        fileId: fileId,
        fileName: fileMetadata.filename,
        fileSize: fileMetadata.length,
        bucketName: bucketName
      });

      console.log(`File deleted successfully: ${fileMetadata.filename} (${fileId})`);

      return {
        success: true,
        fileId: fileId,
        fileName: fileMetadata.filename,
        fileSize: fileMetadata.length
      };

    } catch (error) {
      console.error(`Error deleting file ${fileId}:`, error);
      return {
        success: false,
        fileId: fileId,
        error: error.message
      };
    }
  }

  async getStorageStatistics(bucketName = null) {
    console.log(`Getting storage statistics${bucketName ? ' for bucket: ' + bucketName : ''}`);

    try {
      const statistics = {};

      if (bucketName) {
        // Get statistics for specific bucket
        const bucketInfo = this.gridFSBuckets.get(bucketName);
        if (!bucketInfo) {
          throw new Error(`GridFS bucket not found: ${bucketName}`);
        }

        statistics[bucketName] = await this.calculateBucketStatistics(bucketName, bucketInfo);
      } else {
        // Get statistics for all buckets
        for (const [name, bucketInfo] of this.gridFSBuckets.entries()) {
          statistics[name] = await this.calculateBucketStatistics(name, bucketInfo);
        }
      }

      // Calculate system-wide statistics
      const systemStats = {
        totalBuckets: this.gridFSBuckets.size,
        totalFiles: Object.values(statistics).reduce((sum, bucket) => sum + bucket.fileCount, 0),
        totalBytes: Object.values(statistics).reduce((sum, bucket) => sum + bucket.totalBytes, 0),
        averageFileSize: 0,
        storageEfficiency: this.metrics.storageEfficiency
      };

      if (systemStats.totalFiles > 0) {
        systemStats.averageFileSize = systemStats.totalBytes / systemStats.totalFiles;
      }

      return {
        success: true,
        bucketStatistics: statistics,
        systemStatistics: systemStats,
        retrievalTime: new Date()
      };

    } catch (error) {
      console.error('Error getting storage statistics:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async calculateBucketStatistics(bucketName, bucketInfo) {
    const filesCollection = this.db.collection(`${bucketName}.files`);
    const chunksCollection = this.db.collection(`${bucketName}.chunks`);

    // Basic file statistics
    const fileStats = await filesCollection.aggregate([
      {
        $group: {
          _id: null,
          fileCount: { $sum: 1 },
          totalBytes: { $sum: '$length' },
          averageFileSize: { $avg: '$length' },
          largestFile: { $max: '$length' },
          smallestFile: { $min: '$length' }
        }
      }
    ]).toArray();

    // Content type distribution
    const contentTypeStats = await filesCollection.aggregate([
      {
        $group: {
          _id: '$metadata.contentType',
          count: { $sum: 1 },
          totalBytes: { $sum: '$length' }
        }
      },
      { $sort: { count: -1 } },
      { $limit: 10 }
    ]).toArray();

    // Chunk statistics
    const chunkStats = await chunksCollection.aggregate([
      {
        $group: {
          _id: null,
          totalChunks: { $sum: 1 },
          averageChunkSize: { $avg: { $binarySize: '$data' } }
        }
      }
    ]).toArray();

    const baseStats = fileStats[0] || {
      fileCount: 0,
      totalBytes: 0,
      averageFileSize: 0,
      largestFile: 0,
      smallestFile: 0
    };

    return {
      fileCount: baseStats.fileCount,
      totalBytes: baseStats.totalBytes,
      averageFileSize: Math.round(baseStats.averageFileSize || 0),
      largestFile: baseStats.largestFile,
      smallestFile: baseStats.smallestFile,
      contentTypes: contentTypeStats,
      totalChunks: chunkStats[0]?.totalChunks || 0,
      averageChunkSize: Math.round(chunkStats[0]?.averageChunkSize || 0),
      storageEfficiency: this.calculateStorageEfficiency(bucketName)
    };
  }

  // Utility methods

  generateUploadId() {
    return `upload_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  generateDownloadId() {
    return `download_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  detectContentType(filePath) {
    const path = require('path');
    const ext = path.extname(filePath).toLowerCase();

    const mimeTypes = {
      '.pdf': 'application/pdf',
      '.doc': 'application/msword',
      '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      '.jpg': 'image/jpeg',
      '.jpeg': 'image/jpeg',
      '.png': 'image/png',
      '.gif': 'image/gif',
      '.mp4': 'video/mp4',
      '.mp3': 'audio/mpeg',
      '.zip': 'application/zip',
      '.txt': 'text/plain',
      '.json': 'application/json',
      '.xml': 'application/xml'
    };

    return mimeTypes[ext] || 'application/octet-stream';
  }

  async calculateFileChecksum(filePath, algorithm = 'sha256') {
    return new Promise((resolve, reject) => {
      const hash = crypto.createHash(algorithm);
      const stream = createReadStream(filePath);

      stream.on('data', (data) => {
        hash.update(data);
      });

      stream.on('end', () => {
        resolve(hash.digest('hex'));
      });

      stream.on('error', (error) => {
        reject(error);
      });
    });
  }

  async findDuplicateFile(checksum, bucketName) {
    const filesCollection = this.db.collection(`${bucketName}.files`);
    return await filesCollection.findOne({
      'metadata.checksums.sha256': checksum
    });
  }

  calculateStorageEfficiency(bucketName) {
    // Simplified storage efficiency calculation
    // In a real implementation, this would analyze compression ratios, deduplication, etc.
    return 85.0; // Placeholder
  }

  updateMetrics(uploadConfig) {
    this.metrics.totalFilesStored++;
    this.metrics.totalBytesStored += uploadConfig.progress.totalBytes;

    // Update average upload speed
    const totalUploads = this.metrics.totalFilesStored;
    this.metrics.averageUploadSpeed = 
      ((this.metrics.averageUploadSpeed * (totalUploads - 1)) + uploadConfig.uploadSpeed) / totalUploads;
  }

  async setupFileProcessingPipeline() {
    // Setup file processing pipeline for thumbnails, content analysis, etc.
    console.log('Setting up file processing pipeline...');
  }

  async setupMonitoringAndMetrics() {
    // Setup monitoring and metrics collection
    console.log('Setting up monitoring and metrics...');
  }

  async processUploadedFile(fileId, uploadConfig) {
    // Process uploaded file (thumbnails, analysis, etc.)
    console.log(`Processing uploaded file: ${fileId}`);
  }

  async shutdown() {
    console.log('Shutting down GridFS file manager...');

    try {
      // Wait for active uploads/downloads to complete
      if (this.activeUploads.size > 0) {
        console.log(`Waiting for ${this.activeUploads.size} uploads to complete...`);
      }

      if (this.activeDownloads.size > 0) {
        console.log(`Waiting for ${this.activeDownloads.size} downloads to complete...`);
      }

      // Close MongoDB connection
      if (this.client) {
        await this.client.close();
      }

      console.log('GridFS file manager shutdown complete');

    } catch (error) {
      console.error('Error during shutdown:', error);
    }
  }
}

// Benefits of MongoDB GridFS Advanced File Storage:
// - Efficient binary data storage with automatic chunking and compression
// - Integrated metadata management with full-text search capabilities
// - Streaming upload and download with progress tracking and optimization
// - Built-in replication and distributed storage through MongoDB replica sets
// - Transactional consistency between file operations and database operations
// - Advanced file processing pipeline with thumbnail generation and content analysis
// - Comprehensive version control and access management capabilities
// - SQL-compatible file operations through QueryLeaf integration
// - Enterprise-grade security, encryption, and audit logging
// - Production-ready scalability with automatic load balancing and optimization

module.exports = {
  AdvancedGridFSFileManager
};

Understanding MongoDB GridFS Architecture

Advanced File Storage Design and Implementation Patterns

Implement comprehensive GridFS workflows for enterprise file management:

// Enterprise-grade GridFS with advanced distributed file management capabilities
class EnterpriseGridFSManager extends AdvancedGridFSFileManager {
  constructor(connectionString, enterpriseConfig) {
    super(connectionString, enterpriseConfig);

    this.enterpriseConfig = {
      ...enterpriseConfig,
      enableDistributedProcessing: true,
      enableContentDeliveryNetwork: true,
      enableAdvancedSecurity: true,
      enableComplianceAuditing: true,
      enableGlobalReplication: true
    };

    this.setupEnterpriseCapabilities();
    this.initializeDistributedProcessing();
    this.setupContentDeliveryNetwork();
  }

  async implementAdvancedFileStrategy() {
    console.log('Implementing enterprise file management strategy...');

    const fileStrategy = {
      // Multi-tier storage strategy
      storageTiers: {
        hotStorage: {
          criteria: 'accessed_within_30_days',
          chunkSize: 261120,
          compressionLevel: 6,
          replicationFactor: 3
        },
        coldStorage: {
          criteria: 'accessed_30_to_90_days_ago',
          chunkSize: 1048576,
          compressionLevel: 9,
          replicationFactor: 2
        },
        archiveStorage: {
          criteria: 'accessed_more_than_90_days_ago',
          chunkSize: 4194304,
          compressionLevel: 9,
          replicationFactor: 1
        }
      },

      // Content delivery optimization
      contentDelivery: {
        enableGlobalDistribution: true,
        enableEdgeCaching: true,
        enableImageOptimization: true,
        enableVideoTranscoding: true
      },

      // Advanced processing
      fileProcessing: {
        enableMachineLearning: true,
        enableContentRecognition: true,
        enableAutomaticTagging: true,
        enableThreatDetection: true
      }
    };

    return await this.deployEnterpriseStrategy(fileStrategy);
  }

  async setupAdvancedSecurity() {
    console.log('Setting up enterprise security for file operations...');

    const securityConfig = {
      // File encryption
      encryptionAtRest: true,
      encryptionInTransit: true,
      encryptionKeyRotation: true,

      // Access control
      roleBasedAccess: true,
      attributeBasedAccess: true,
      dynamicPermissions: true,

      // Threat protection
      malwareScanning: true,
      contentFiltering: true,
      dataLossPrevention: true
    };

    return await this.deploySecurityFramework(securityConfig);
  }
}

SQL-Style GridFS Operations with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB GridFS operations:

-- QueryLeaf advanced GridFS operations with SQL-familiar syntax for MongoDB

-- Configure GridFS bucket with comprehensive settings
CREATE GRIDFS_BUCKET media_files 
WITH chunk_size_bytes = 261120,
     enable_compression = true,
     compression_level = 6,
     enable_encryption = true,
     enable_metadata_indexing = true,
     enable_version_control = true,
     enable_thumbnail_generation = true,
     enable_content_analysis = true,

     -- Storage optimization
     enable_deduplication = true,
     enable_automatic_cleanup = true,
     storage_tier_management = true,

     -- Access control
     default_permissions = 'private',
     enable_access_logging = true,
     enable_audit_trail = true,

     -- Performance settings
     max_concurrent_uploads = 10,
     enable_parallel_processing = true,
     enable_streaming_optimization = true,

     -- Backup and replication
     enable_backup_integration = true,
     cross_region_replication = true,
     replication_factor = 3;

-- Advanced file upload with comprehensive metadata and processing
WITH file_uploads AS (
  SELECT 
    file_id,
    filename,
    file_size_bytes,
    content_type,
    upload_timestamp,
    upload_duration_seconds,
    upload_speed_mbps,

    -- Processing results
    compression_applied,
    compression_ratio,
    thumbnail_generated,
    content_analysis_completed,
    virus_scan_status,

    -- Metadata extraction
    JSON_EXTRACT(metadata, '$.originalPath') as original_path,
    JSON_EXTRACT(metadata, '$.uploadedBy') as uploaded_by,
    JSON_EXTRACT(metadata, '$.tags') as file_tags,
    JSON_EXTRACT(metadata, '$.category') as file_category,
    JSON_EXTRACT(metadata, '$.permissions') as access_permissions,

    -- File integrity
    JSON_EXTRACT(metadata, '$.checksums.md5') as md5_checksum,
    JSON_EXTRACT(metadata, '$.checksums.sha256') as sha256_checksum,

    -- Processing pipeline results
    JSON_EXTRACT(metadata, '$.processingResults') as processing_results,
    JSON_EXTRACT(metadata, '$.thumbnailPath') as thumbnail_path,
    JSON_EXTRACT(metadata, '$.contentAnalysis') as content_analysis

  FROM GRIDFS_FILES('media_files')
  WHERE upload_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
),

upload_performance AS (
  SELECT 
    file_category,
    content_type,
    COUNT(*) as total_uploads,
    SUM(file_size_bytes) as total_bytes_uploaded,
    AVG(upload_duration_seconds) as avg_upload_time,
    AVG(upload_speed_mbps) as avg_upload_speed,
    PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY upload_duration_seconds) as p95_upload_time,

    -- Processing performance
    COUNT(*) FILTER (WHERE compression_applied = true) as compressed_files,
    AVG(compression_ratio) FILTER (WHERE compression_applied = true) as avg_compression_ratio,
    COUNT(*) FILTER (WHERE thumbnail_generated = true) as thumbnails_generated,
    COUNT(*) FILTER (WHERE content_analysis_completed = true) as content_analyzed,
    COUNT(*) FILTER (WHERE virus_scan_status = 'clean') as clean_files,
    COUNT(*) FILTER (WHERE virus_scan_status = 'threat_detected') as threat_files,

    -- File size distribution
    AVG(file_size_bytes) as avg_file_size_bytes,
    MAX(file_size_bytes) as largest_file_bytes,
    MIN(file_size_bytes) as smallest_file_bytes,

    -- Storage efficiency
    SUM(file_size_bytes) as original_total_bytes,
    SUM(CASE WHEN compression_applied THEN 
          file_size_bytes * (1 - compression_ratio) 
        ELSE file_size_bytes 
    END) as stored_total_bytes

  FROM file_uploads
  GROUP BY file_category, content_type
),

storage_analysis AS (
  SELECT 
    bucket_name,
    DATE_TRUNC('hour', upload_timestamp) as upload_hour,

    -- Upload volume analysis
    COUNT(*) as files_uploaded,
    SUM(file_size_bytes) as bytes_uploaded,
    AVG(upload_speed_mbps) as avg_hourly_upload_speed,

    -- Content type distribution
    COUNT(*) FILTER (WHERE content_type LIKE 'image/%') as image_files,
    COUNT(*) FILTER (WHERE content_type LIKE 'video/%') as video_files,
    COUNT(*) FILTER (WHERE content_type LIKE 'audio/%') as audio_files,
    COUNT(*) FILTER (WHERE content_type = 'application/pdf') as pdf_files,
    COUNT(*) FILTER (WHERE content_type NOT IN ('image/%', 'video/%', 'audio/%', 'application/pdf')) as other_files,

    -- Processing success rates
    COUNT(*) FILTER (WHERE virus_scan_status = 'clean') as safe_files,
    COUNT(*) FILTER (WHERE content_analysis_completed = true) as analyzed_files,
    COUNT(*) FILTER (WHERE thumbnail_generated = true) as thumbnail_files,

    -- Storage optimization metrics
    AVG(CASE WHEN compression_applied THEN compression_ratio ELSE 0 END) as avg_compression_ratio,
    SUM(CASE WHEN compression_applied THEN 
          file_size_bytes * compression_ratio 
        ELSE 0 
    END) as total_space_saved_bytes

  FROM file_uploads
  GROUP BY bucket_name, DATE_TRUNC('hour', upload_timestamp)
)

SELECT 
  up.file_category,
  up.content_type,
  up.total_uploads,

  -- Upload performance metrics
  ROUND(up.total_bytes_uploaded / 1024.0 / 1024.0, 2) as total_uploaded_mb,
  ROUND(up.avg_upload_time, 2) as avg_upload_time_seconds,
  ROUND(up.avg_upload_speed, 2) as avg_upload_speed_mbps,
  ROUND(up.p95_upload_time, 2) as p95_upload_time_seconds,

  -- Processing efficiency
  up.compressed_files,
  ROUND((up.compressed_files * 100.0) / up.total_uploads, 1) as compression_rate_percent,
  ROUND(up.avg_compression_ratio * 100, 1) as avg_compression_percent,
  up.thumbnails_generated,
  ROUND((up.thumbnails_generated * 100.0) / up.total_uploads, 1) as thumbnail_rate_percent,

  -- Content analysis results
  up.content_analyzed,
  ROUND((up.content_analyzed * 100.0) / up.total_uploads, 1) as analysis_rate_percent,

  -- Security metrics
  up.clean_files,
  up.threat_files,
  CASE 
    WHEN up.threat_files > 0 THEN 'security_issues_detected'
    ELSE 'all_files_clean'
  END as security_status,

  -- File size statistics
  ROUND(up.avg_file_size_bytes / 1024.0 / 1024.0, 2) as avg_file_size_mb,
  ROUND(up.largest_file_bytes / 1024.0 / 1024.0, 2) as largest_file_mb,
  ROUND(up.smallest_file_bytes / 1024.0, 2) as smallest_file_kb,

  -- Storage optimization
  ROUND(up.original_total_bytes / 1024.0 / 1024.0, 2) as original_storage_mb,
  ROUND(up.stored_total_bytes / 1024.0 / 1024.0, 2) as actual_storage_mb,
  ROUND(((up.original_total_bytes - up.stored_total_bytes) / up.original_total_bytes) * 100, 1) as storage_savings_percent,

  -- Performance assessment
  CASE 
    WHEN up.avg_upload_speed > 50 THEN 'excellent'
    WHEN up.avg_upload_speed > 20 THEN 'good'
    WHEN up.avg_upload_speed > 10 THEN 'acceptable'
    ELSE 'needs_optimization'
  END as upload_performance_rating,

  -- Processing health
  CASE 
    WHEN up.threat_files > 0 THEN 'security_review_required'
    WHEN (up.thumbnails_generated * 100.0 / up.total_uploads) < 80 AND up.content_type LIKE 'image/%' THEN 'thumbnail_generation_issues'
    WHEN (up.content_analyzed * 100.0 / up.total_uploads) < 90 THEN 'content_analysis_issues'
    ELSE 'processing_healthy'
  END as processing_health_status,

  -- Optimization recommendations
  ARRAY[
    CASE WHEN up.avg_upload_speed < 10 THEN 'Optimize network bandwidth or chunk size' END,
    CASE WHEN up.avg_compression_ratio < 0.3 AND up.content_type LIKE 'image/%' THEN 'Review image compression settings' END,
    CASE WHEN (up.thumbnails_generated * 100.0 / up.total_uploads) < 50 AND up.content_type LIKE 'image/%' THEN 'Fix thumbnail generation pipeline' END,
    CASE WHEN up.threat_files > 0 THEN 'Review security scanning configuration' END,
    CASE WHEN up.p95_upload_time > 300 THEN 'Optimize upload processing for large files' END
  ]::TEXT[] as optimization_recommendations

FROM upload_performance up
ORDER BY up.total_bytes_uploaded DESC, up.total_uploads DESC;

-- Advanced file search and retrieval with comprehensive filtering
WITH file_search_results AS (
  SELECT 
    file_id,
    filename,
    content_type,
    file_size_bytes,
    upload_timestamp,

    -- Metadata extraction
    JSON_EXTRACT(metadata, '$.category') as category,
    JSON_EXTRACT(metadata, '$.tags') as tags,
    JSON_EXTRACT(metadata, '$.uploadedBy') as uploaded_by,
    JSON_EXTRACT(metadata, '$.permissions') as permissions,
    JSON_EXTRACT(metadata, '$.contentAnalysis.description') as content_description,
    JSON_EXTRACT(metadata, '$.contentAnalysis.keywords') as content_keywords,
    JSON_EXTRACT(metadata, '$.processingResults.thumbnailAvailable') as has_thumbnail,
    JSON_EXTRACT(metadata, '$.processingResults.textExtracted') as has_text_content,

    -- File access patterns
    download_count,
    last_accessed,
    access_frequency_score,

    -- Storage tier information
    storage_tier,
    CASE storage_tier
      WHEN 'hot' THEN 1
      WHEN 'warm' THEN 2  
      WHEN 'cold' THEN 3
      WHEN 'archive' THEN 4
      ELSE 5
    END as tier_priority,

    -- File age and usage
    EXTRACT(DAYS FROM (CURRENT_TIMESTAMP - upload_timestamp)) as file_age_days,
    EXTRACT(DAYS FROM (CURRENT_TIMESTAMP - last_accessed)) as days_since_last_access

  FROM GRIDFS_FILES('media_files')
  WHERE 
    -- Content type filters
    (content_type IN ('image/jpeg', 'image/png', 'application/pdf', 'video/mp4') OR content_type LIKE '%/%')

    -- Size filters
    AND file_size_bytes BETWEEN 1024 AND 1073741824  -- 1KB to 1GB

    -- Date range filters
    AND upload_timestamp >= CURRENT_TIMESTAMP - INTERVAL '90 days'

    -- Category and tag filters
    AND (JSON_EXTRACT(metadata, '$.category') IS NOT NULL)
    AND (JSON_EXTRACT(metadata, '$.tags') IS NOT NULL)
),

file_analytics AS (
  SELECT 
    fsr.*,

    -- Content analysis scoring
    CASE 
      WHEN fsr.content_description IS NOT NULL AND fsr.content_keywords IS NOT NULL THEN 'fully_analyzed'
      WHEN fsr.content_description IS NOT NULL OR fsr.content_keywords IS NOT NULL THEN 'partially_analyzed'
      ELSE 'not_analyzed'
    END as analysis_completeness,

    -- Access pattern classification
    CASE 
      WHEN fsr.access_frequency_score > 0.8 THEN 'frequently_accessed'
      WHEN fsr.access_frequency_score > 0.4 THEN 'moderately_accessed'
      WHEN fsr.access_frequency_score > 0.1 THEN 'rarely_accessed'
      ELSE 'never_accessed'
    END as access_pattern,

    -- Storage optimization opportunities
    CASE 
      WHEN fsr.days_since_last_access > 90 AND fsr.storage_tier IN ('hot', 'warm') THEN 'candidate_for_cold_storage'
      WHEN fsr.days_since_last_access > 365 AND fsr.storage_tier != 'archive' THEN 'candidate_for_archive'
      WHEN fsr.access_frequency_score > 0.6 AND fsr.storage_tier IN ('cold', 'archive') THEN 'candidate_for_hot_storage'
      ELSE 'appropriate_storage_tier'
    END as storage_optimization,

    -- File health assessment
    CASE 
      WHEN fsr.has_thumbnail = false AND fsr.content_type LIKE 'image/%' THEN 'missing_thumbnail'
      WHEN fsr.has_text_content = false AND fsr.content_type = 'application/pdf' THEN 'text_extraction_needed'
      WHEN fsr.analysis_completeness = 'not_analyzed' AND fsr.file_age_days > 7 THEN 'analysis_overdue'
      ELSE 'healthy'
    END as file_health_status

  FROM file_search_results fsr
),

usage_patterns AS (
  SELECT 
    content_type,
    category,
    access_pattern,
    storage_tier,
    COUNT(*) as file_count,
    SUM(file_size_bytes) as total_bytes,
    AVG(download_count) as avg_downloads,
    AVG(access_frequency_score) as avg_access_score,

    -- Storage tier distribution
    COUNT(*) FILTER (WHERE storage_tier = 'hot') as hot_tier_count,
    COUNT(*) FILTER (WHERE storage_tier = 'warm') as warm_tier_count,
    COUNT(*) FILTER (WHERE storage_tier = 'cold') as cold_tier_count,
    COUNT(*) FILTER (WHERE storage_tier = 'archive') as archive_tier_count,

    -- Health metrics
    COUNT(*) FILTER (WHERE file_health_status = 'healthy') as healthy_files,
    COUNT(*) FILTER (WHERE file_health_status != 'healthy') as unhealthy_files,

    -- Optimization opportunities
    COUNT(*) FILTER (WHERE storage_optimization LIKE 'candidate_for_%') as optimization_candidates

  FROM file_analytics
  GROUP BY content_type, category, access_pattern, storage_tier
)

SELECT 
  fa.file_id,
  fa.filename,
  fa.content_type,
  ROUND(fa.file_size_bytes / 1024.0 / 1024.0, 2) as file_size_mb,
  fa.category,
  fa.tags,
  fa.uploaded_by,

  -- Access and usage information
  fa.download_count,
  fa.access_pattern,
  fa.days_since_last_access,
  ROUND(fa.access_frequency_score, 3) as access_score,

  -- Storage and optimization
  fa.storage_tier,
  fa.storage_optimization,
  fa.file_health_status,

  -- Content analysis
  fa.analysis_completeness,
  CASE WHEN fa.has_thumbnail THEN 'yes' ELSE 'no' END as thumbnail_available,
  CASE WHEN fa.has_text_content THEN 'yes' ELSE 'no' END as text_content_available,

  -- File management recommendations
  ARRAY[
    CASE WHEN fa.storage_optimization LIKE 'candidate_for_%' THEN 
           'Move to ' || REPLACE(REPLACE(fa.storage_optimization, 'candidate_for_', ''), '_storage', ' storage')
         END,
    CASE WHEN fa.file_health_status = 'missing_thumbnail' THEN 'Generate thumbnail' END,
    CASE WHEN fa.file_health_status = 'text_extraction_needed' THEN 'Extract text content' END,
    CASE WHEN fa.file_health_status = 'analysis_overdue' THEN 'Run content analysis' END,
    CASE WHEN fa.days_since_last_access > 180 AND fa.download_count = 0 THEN 'Consider deletion' END
  ]::TEXT[] as recommendations,

  -- Priority scoring for operations
  CASE 
    WHEN fa.file_health_status != 'healthy' THEN 'high'
    WHEN fa.storage_optimization LIKE 'candidate_for_%' THEN 'medium' 
    WHEN fa.analysis_completeness = 'not_analyzed' THEN 'medium'
    ELSE 'low'
  END as maintenance_priority,

  -- Search relevance scoring
  (
    CASE WHEN fa.access_frequency_score > 0.5 THEN 2 ELSE 0 END +
    CASE WHEN fa.analysis_completeness = 'fully_analyzed' THEN 1 ELSE 0 END +
    CASE WHEN fa.file_health_status = 'healthy' THEN 1 ELSE 0 END +
    CASE WHEN fa.storage_tier = 'hot' THEN 1 ELSE 0 END
  ) as relevance_score

FROM file_analytics fa
WHERE 
  -- Apply additional search filters
  fa.file_size_mb BETWEEN 1 AND 100  -- 1MB to 100MB
  AND fa.file_age_days <= 60  -- Files from last 60 days
  AND fa.analysis_completeness != 'not_analyzed'  -- Only analyzed files

ORDER BY 
  -- Primary sort by maintenance priority, then relevance
  CASE fa.maintenance_priority 
    WHEN 'high' THEN 1 
    WHEN 'medium' THEN 2 
    ELSE 3 
  END,
  relevance_score DESC,
  fa.access_frequency_score DESC,
  fa.upload_timestamp DESC
LIMIT 100;

-- GridFS storage tier management and optimization
CREATE VIEW gridfs_storage_optimization AS
WITH current_storage_state AS (
  SELECT 
    storage_tier,
    COUNT(*) as file_count,
    SUM(file_size_bytes) as total_bytes,
    AVG(file_size_bytes) as avg_file_size,
    AVG(access_frequency_score) as avg_access_frequency,
    AVG(EXTRACT(DAYS FROM (CURRENT_TIMESTAMP - last_accessed))) as avg_days_since_access,

    -- Cost analysis (simplified model)
    SUM(file_size_bytes) * CASE storage_tier
      WHEN 'hot' THEN 0.023    -- $0.023 per GB/month
      WHEN 'warm' THEN 0.0125  -- $0.0125 per GB/month  
      WHEN 'cold' THEN 0.004   -- $0.004 per GB/month
      WHEN 'archive' THEN 0.001 -- $0.001 per GB/month
      ELSE 0.023
    END / 1024.0 / 1024.0 / 1024.0 as estimated_monthly_cost_usd,

    -- Performance characteristics  
    AVG(CASE storage_tier
      WHEN 'hot' THEN 10      -- 10ms avg access time
      WHEN 'warm' THEN 100    -- 100ms avg access time
      WHEN 'cold' THEN 1000   -- 1s avg access time  
      WHEN 'archive' THEN 15000 -- 15s avg access time
      ELSE 1000
    END) as avg_access_time_ms

  FROM GRIDFS_FILES('media_files')
  WHERE upload_timestamp >= CURRENT_TIMESTAMP - INTERVAL '365 days'
  GROUP BY storage_tier
),

optimization_opportunities AS (
  SELECT 
    file_id,
    filename,
    storage_tier,
    file_size_bytes,
    access_frequency_score,
    EXTRACT(DAYS FROM (CURRENT_TIMESTAMP - last_accessed)) as days_since_access,
    download_count,

    -- Current cost
    file_size_bytes * CASE storage_tier
      WHEN 'hot' THEN 0.023
      WHEN 'warm' THEN 0.0125  
      WHEN 'cold' THEN 0.004
      WHEN 'archive' THEN 0.001
      ELSE 0.023
    END / 1024.0 / 1024.0 / 1024.0 as current_monthly_cost_usd,

    -- Recommended tier based on access patterns
    CASE 
      WHEN access_frequency_score > 0.7 OR days_since_access <= 7 THEN 'hot'
      WHEN access_frequency_score > 0.3 OR days_since_access <= 30 THEN 'warm'
      WHEN access_frequency_score > 0.1 OR days_since_access <= 90 THEN 'cold'
      ELSE 'archive'
    END as recommended_tier,

    -- Potential savings calculation
    CASE 
      WHEN access_frequency_score > 0.7 OR days_since_access <= 7 THEN 0.023
      WHEN access_frequency_score > 0.3 OR days_since_access <= 30 THEN 0.0125
      WHEN access_frequency_score > 0.1 OR days_since_access <= 90 THEN 0.004
      ELSE 0.001
    END as recommended_cost_per_gb

  FROM GRIDFS_FILES('media_files')
  WHERE upload_timestamp >= CURRENT_TIMESTAMP - INTERVAL '365 days'
)

SELECT 
  css.storage_tier as current_tier,
  css.file_count,
  ROUND(css.total_bytes / 1024.0 / 1024.0 / 1024.0, 2) as storage_gb,
  ROUND(css.avg_file_size / 1024.0 / 1024.0, 2) as avg_file_size_mb,
  ROUND(css.avg_access_frequency, 3) as avg_access_frequency,
  ROUND(css.avg_days_since_access, 1) as avg_days_since_access,
  ROUND(css.estimated_monthly_cost_usd, 2) as current_monthly_cost_usd,
  ROUND(css.avg_access_time_ms, 0) as avg_access_time_ms,

  -- Optimization analysis
  (SELECT COUNT(*) 
   FROM optimization_opportunities oo 
   WHERE oo.storage_tier = css.storage_tier 
   AND oo.recommended_tier != oo.storage_tier) as files_needing_optimization,

  (SELECT SUM(ABS(oo.current_monthly_cost_usd - 
                   (oo.file_size_bytes * oo.recommended_cost_per_gb / 1024.0 / 1024.0 / 1024.0)))
   FROM optimization_opportunities oo 
   WHERE oo.storage_tier = css.storage_tier 
   AND oo.recommended_tier != oo.storage_tier) as potential_monthly_savings_usd,

  -- Tier health assessment
  CASE 
    WHEN css.avg_access_frequency < 0.1 AND css.storage_tier = 'hot' THEN 'overprovisioned'
    WHEN css.avg_access_frequency > 0.6 AND css.storage_tier IN ('cold', 'archive') THEN 'underprovisioned' 
    WHEN css.avg_days_since_access > 90 AND css.storage_tier IN ('hot', 'warm') THEN 'tier_too_hot'
    WHEN css.avg_days_since_access < 30 AND css.storage_tier IN ('cold', 'archive') THEN 'tier_too_cold'
    ELSE 'appropriately_tiered'
  END as tier_health_status,

  -- Recommendations
  CASE 
    WHEN css.avg_access_frequency < 0.1 AND css.storage_tier = 'hot' THEN 'Move files to cold or archive storage'
    WHEN css.avg_access_frequency > 0.6 AND css.storage_tier IN ('cold', 'archive') THEN 'Move files to hot storage'
    WHEN css.avg_days_since_access > 180 AND css.storage_tier != 'archive' THEN 'Consider archiving old files'
    ELSE 'Current tiering appears appropriate'
  END as optimization_recommendation

FROM current_storage_state css
ORDER BY css.estimated_monthly_cost_usd DESC;

-- QueryLeaf provides comprehensive GridFS capabilities:
-- 1. SQL-familiar syntax for MongoDB GridFS bucket configuration and management
-- 2. Advanced file upload and download operations with progress tracking
-- 3. Comprehensive metadata management and content analysis integration
-- 4. Intelligent storage tier management with cost optimization
-- 5. File search and retrieval with advanced filtering and relevance scoring
-- 6. Performance monitoring and optimization recommendations
-- 7. Enterprise security and compliance features built-in
-- 8. Automated file processing pipelines with thumbnail generation
-- 9. Storage efficiency analysis with deduplication and compression
-- 10. Production-ready file management with scalable architecture

Best Practices for Production GridFS Deployment

File Storage Architecture Design Principles

Essential principles for effective MongoDB GridFS production deployment:

  1. Bucket Design Strategy: Organize files into logical buckets based on content type, access patterns, and retention requirements
  2. Chunk Size Optimization: Configure appropriate chunk sizes based on file types and access patterns for optimal performance
  3. Metadata Management: Design comprehensive metadata schemas for efficient searching, categorization, and content management
  4. Storage Tier Strategy: Implement intelligent storage tiering based on file access frequency and business requirements
  5. Security Integration: Establish comprehensive access controls, encryption, and audit logging for enterprise security
  6. Performance Monitoring: Monitor upload/download performance, storage efficiency, and system resource utilization

Enterprise File Management

Design GridFS systems for enterprise-scale file operations:

  1. Content Processing Pipeline: Implement automated file processing for thumbnails, content analysis, and format optimization
  2. Disaster Recovery: Design backup strategies and cross-region replication for business continuity
  3. Compliance Management: Ensure file operations meet regulatory requirements and data retention policies
  4. API Integration: Build RESTful APIs and SDK integrations for seamless application development
  5. Monitoring and Alerting: Implement comprehensive monitoring for storage usage, performance, and operational health
  6. Capacity Planning: Monitor growth patterns and plan storage capacity and performance requirements

Conclusion

MongoDB GridFS provides comprehensive large file storage capabilities that enable sophisticated binary data management, efficient streaming operations, and integrated metadata handling through distributed chunk-based storage, automatic replication, and transactional consistency. The native file management tools and streaming interfaces ensure that applications can handle large files efficiently with minimal infrastructure complexity.

Key MongoDB GridFS benefits include:

  • Efficient Binary Storage: Advanced chunk-based storage with compression, deduplication, and intelligent space optimization
  • Integrated Metadata Management: Comprehensive metadata handling with full-text search, tagging, and content analysis capabilities
  • Streaming Operations: High-performance upload and download streaming with progress tracking and parallel processing
  • Distributed Architecture: Built-in replication and distributed storage through MongoDB's replica set technology
  • Transaction Integration: Full transactional consistency between file operations and database operations within MongoDB
  • SQL Accessibility: Familiar SQL-style file management operations through QueryLeaf for accessible binary data operations

Whether you're building document management systems, media streaming platforms, enterprise content repositories, or distributed file storage solutions, MongoDB GridFS with QueryLeaf's familiar SQL interface provides the foundation for sophisticated, scalable file operations.

QueryLeaf Integration: QueryLeaf automatically optimizes MongoDB GridFS operations while providing SQL-familiar syntax for file storage, retrieval, and management. Advanced file processing, content analysis, and storage optimization are seamlessly handled through familiar SQL constructs, making sophisticated binary data management accessible to SQL-oriented development teams.

The combination of MongoDB GridFS's robust file storage capabilities with SQL-style file operations makes it an ideal platform for applications requiring both large file handling and familiar database management patterns, ensuring your file storage infrastructure can scale efficiently while maintaining operational simplicity and developer productivity.

MongoDB Index Optimization and Query Performance Analysis: Advanced Database Performance Tuning and Query Optimization for High-Performance Applications

High-performance database applications require sophisticated indexing strategies and comprehensive query optimization techniques that can handle complex query patterns, large data volumes, and evolving access requirements while maintaining optimal response times. Traditional database optimization approaches often struggle with dynamic workloads, compound query patterns, and the complexity of managing multiple index strategies across diverse data access patterns, leading to suboptimal performance, excessive resource consumption, and operational challenges in production environments.

MongoDB provides comprehensive index optimization capabilities through advanced indexing strategies, sophisticated query analysis tools, and intelligent performance monitoring features that enable database administrators and developers to achieve optimal query performance with minimal resource overhead. Unlike traditional databases that require complex index tuning procedures and manual optimization workflows, MongoDB integrates performance analysis directly into the database with automated index recommendations, real-time query analysis, and built-in optimization guidance.

The Traditional Query Performance Challenge

Conventional approaches to database query optimization in relational systems face significant limitations in performance analysis and index management:

-- Traditional PostgreSQL query optimization - manual index management with limited analysis capabilities

-- Basic index tracking table with minimal functionality
CREATE TABLE index_usage_stats (
    index_id SERIAL PRIMARY KEY,
    schema_name VARCHAR(100) NOT NULL,
    table_name VARCHAR(100) NOT NULL,
    index_name VARCHAR(100) NOT NULL,
    index_type VARCHAR(50),

    -- Basic usage statistics (very limited visibility)
    index_scans BIGINT DEFAULT 0,
    tuples_read BIGINT DEFAULT 0,
    tuples_fetched BIGINT DEFAULT 0,

    -- Size information (manual tracking)
    index_size_bytes BIGINT,
    table_size_bytes BIGINT,

    -- Basic metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_analyzed TIMESTAMP,
    is_unique BOOLEAN DEFAULT false,
    is_partial BOOLEAN DEFAULT false,

    -- Simple effectiveness metrics
    scan_ratio DECIMAL(10,4),
    selectivity_estimate DECIMAL(10,4)
);

-- Query performance tracking table (basic functionality)
CREATE TABLE query_performance_log (
    query_id SERIAL PRIMARY KEY,
    query_hash VARCHAR(64),
    query_text TEXT,

    -- Basic execution metrics
    execution_time_ms INTEGER,
    rows_examined BIGINT,
    rows_returned BIGINT,
    execution_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Resource usage (limited tracking)
    cpu_usage_ms INTEGER,
    memory_usage_kb INTEGER,
    disk_reads INTEGER,

    -- Connection information
    database_name VARCHAR(100),
    username VARCHAR(100),
    application_name VARCHAR(100),

    -- Basic query plan information (very limited)
    query_plan_hash VARCHAR(64),
    index_usage TEXT[], -- Simple array of index names

    -- Performance classification
    performance_category VARCHAR(50) DEFAULT 'unknown'
);

-- Manual query plan analysis function (very basic capabilities)
CREATE OR REPLACE FUNCTION analyze_query_performance(
    query_text_param TEXT,
    execution_count INTEGER DEFAULT 1
) RETURNS TABLE (
    avg_execution_time_ms INTEGER,
    total_rows_examined BIGINT,
    total_rows_returned BIGINT,
    selectivity_ratio DECIMAL(10,4),
    suggested_indexes TEXT[],
    performance_rating VARCHAR(20)
) AS $$
DECLARE
    total_execution_time INTEGER := 0;
    total_examined BIGINT := 0;
    total_returned BIGINT := 0;
    execution_counter INTEGER := 0;
    current_execution_time INTEGER;
    current_examined BIGINT;
    current_returned BIGINT;
    plan_info TEXT;
BEGIN
    -- Simulate multiple query executions for analysis
    WHILE execution_counter < execution_count LOOP
        -- Execute EXPLAIN ANALYZE (simplified simulation)
        BEGIN
            -- This would be an actual EXPLAIN ANALYZE in reality
            EXECUTE 'EXPLAIN ANALYZE ' || query_text_param INTO plan_info;

            -- Extract basic metrics (very simplified parsing)
            current_execution_time := (random() * 1000 + 10)::INTEGER; -- Simulated execution time
            current_examined := (random() * 10000 + 100)::BIGINT; -- Simulated rows examined
            current_returned := (random() * 1000 + 10)::BIGINT; -- Simulated rows returned

            total_execution_time := total_execution_time + current_execution_time;
            total_examined := total_examined + current_examined;
            total_returned := total_returned + current_returned;

            -- Log query performance
            INSERT INTO query_performance_log (
                query_text,
                execution_time_ms,
                rows_examined,
                rows_returned,
                query_plan_hash
            ) VALUES (
                query_text_param,
                current_execution_time,
                current_examined,
                current_returned,
                md5(plan_info)
            );

        EXCEPTION WHEN OTHERS THEN
            -- Basic error handling
            current_execution_time := 9999; -- Error indicator
            current_examined := 0;
            current_returned := 0;
        END;

        execution_counter := execution_counter + 1;
    END LOOP;

    -- Calculate average metrics
    RETURN QUERY SELECT 
        (total_execution_time / execution_count)::INTEGER,
        total_examined,
        total_returned,
        CASE 
            WHEN total_examined > 0 THEN (total_returned::DECIMAL / total_examined)
            ELSE 0
        END,

        -- Very basic index suggestions (limited analysis)
        CASE 
            WHEN total_execution_time > 1000 THEN ARRAY['Consider adding indexes on WHERE clause columns']
            WHEN total_examined > total_returned * 10 THEN ARRAY['Add indexes to improve selectivity']
            ELSE ARRAY['Performance appears acceptable']
        END::TEXT[],

        -- Simple performance rating
        CASE 
            WHEN total_execution_time < 100 THEN 'excellent'
            WHEN total_execution_time < 500 THEN 'good'
            WHEN total_execution_time < 1000 THEN 'acceptable'
            ELSE 'poor'
        END;

END;
$$ LANGUAGE plpgsql;

-- Execute query performance analysis (basic functionality)
SELECT * FROM analyze_query_performance('SELECT * FROM users WHERE email = ''[email protected]'' AND created_at > ''2023-01-01''', 5);

-- Index effectiveness monitoring (limited capabilities)
WITH index_effectiveness AS (
    SELECT 
        ius.schema_name,
        ius.table_name,
        ius.index_name,
        ius.index_type,
        ius.index_scans,
        ius.tuples_read,
        ius.tuples_fetched,
        ius.index_size_bytes,

        -- Basic effectiveness calculations
        CASE 
            WHEN ius.index_scans > 0 AND ius.tuples_read > 0 THEN
                ius.tuples_fetched::DECIMAL / ius.tuples_read
            ELSE 0
        END as fetch_ratio,

        CASE 
            WHEN ius.table_size_bytes > 0 AND ius.index_size_bytes > 0 THEN
                (ius.index_size_bytes::DECIMAL / ius.table_size_bytes) * 100
            ELSE 0
        END as size_overhead_percent,

        -- Usage frequency analysis
        CASE 
            WHEN ius.index_scans = 0 THEN 'unused'
            WHEN ius.index_scans < 10 THEN 'rarely_used'
            WHEN ius.index_scans < 100 THEN 'moderately_used'
            ELSE 'frequently_used'
        END as usage_category

    FROM index_usage_stats ius
    WHERE ius.last_analyzed >= CURRENT_DATE - INTERVAL '7 days'
),

query_patterns AS (
    SELECT 
        qpl.database_name,
        qpl.query_hash,
        COUNT(*) as execution_count,
        AVG(qpl.execution_time_ms) as avg_execution_time,
        MAX(qpl.execution_time_ms) as max_execution_time,
        AVG(qpl.rows_examined) as avg_rows_examined,
        AVG(qpl.rows_returned) as avg_rows_returned,

        -- Performance trend analysis (very basic)
        CASE 
            WHEN COUNT(*) > 100 AND AVG(qpl.execution_time_ms) > 500 THEN 'high_impact_slow'
            WHEN COUNT(*) > 1000 THEN 'high_frequency'
            WHEN AVG(qpl.execution_time_ms) > 1000 THEN 'slow_query'
            ELSE 'normal'
        END as query_pattern_type,

        -- Index usage analysis from query logs
        STRING_AGG(DISTINCT unnest(qpl.index_usage), ', ') as indexes_used,

        -- Execution time trends
        PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY qpl.execution_time_ms) as p95_execution_time

    FROM query_performance_log qpl
    WHERE qpl.execution_timestamp >= CURRENT_DATE - INTERVAL '7 days'
    GROUP BY qpl.database_name, qpl.query_hash
)

SELECT 
    ie.schema_name,
    ie.table_name,
    ie.index_name,
    ie.index_type,
    ie.usage_category,

    -- Index effectiveness metrics
    ie.index_scans,
    ROUND(ie.fetch_ratio, 4) as selectivity_ratio,
    ROUND(ie.size_overhead_percent, 2) as size_overhead_percent,

    -- Size analysis
    ROUND(ie.index_size_bytes / 1024.0 / 1024.0, 2) as index_size_mb,

    -- Related query patterns
    COUNT(qp.query_hash) as related_query_patterns,
    COALESCE(AVG(qp.avg_execution_time), 0) as avg_query_time_using_index,
    COALESCE(AVG(qp.avg_rows_examined), 0) as avg_rows_examined,

    -- Index recommendations (very basic logic)
    CASE 
        WHEN ie.usage_category = 'unused' AND ie.index_size_bytes > 100*1024*1024 THEN 'consider_dropping'
        WHEN ie.fetch_ratio < 0.1 AND ie.index_scans > 0 THEN 'poor_selectivity'
        WHEN ie.usage_category = 'frequently_used' AND ie.fetch_ratio > 0.8 THEN 'high_performance'
        WHEN ie.size_overhead_percent > 50 THEN 'review_necessity'
        ELSE 'monitor'
    END as recommendation,

    -- Performance impact assessment
    CASE 
        WHEN ie.usage_category IN ('frequently_used', 'moderately_used') AND ie.fetch_ratio > 0.5 THEN 'positive_impact'
        WHEN ie.usage_category = 'unused' THEN 'no_impact'
        WHEN ie.fetch_ratio < 0.1 THEN 'negative_impact'
        ELSE 'unclear_impact'
    END as performance_impact

FROM index_effectiveness ie
LEFT JOIN query_patterns qp ON qp.indexes_used LIKE '%' || ie.index_name || '%'
GROUP BY 
    ie.schema_name, ie.table_name, ie.index_name, ie.index_type, 
    ie.usage_category, ie.index_scans, ie.fetch_ratio, 
    ie.size_overhead_percent, ie.index_size_bytes
ORDER BY 
    ie.index_scans DESC, 
    ie.fetch_ratio DESC,
    ie.index_size_bytes DESC;

-- Query optimization recommendations (very limited analysis)
WITH slow_queries AS (
    SELECT 
        query_hash,
        query_text,
        COUNT(*) as execution_count,
        AVG(execution_time_ms) as avg_time,
        MAX(execution_time_ms) as max_time,
        AVG(rows_examined) as avg_examined,
        AVG(rows_returned) as avg_returned,

        -- Basic pattern detection
        CASE 
            WHEN query_text ILIKE '%WHERE%=%' THEN 'equality_filter'
            WHEN query_text ILIKE '%WHERE%>%' OR query_text ILIKE '%WHERE%<%' THEN 'range_filter'
            WHEN query_text ILIKE '%ORDER BY%' THEN 'sorting'
            WHEN query_text ILIKE '%GROUP BY%' THEN 'aggregation'
            ELSE 'unknown_pattern'
        END as query_pattern

    FROM query_performance_log
    WHERE execution_time_ms > 500  -- Focus on slow queries
    AND execution_timestamp >= CURRENT_DATE - INTERVAL '24 hours'
    GROUP BY query_hash, query_text
    HAVING COUNT(*) >= 5  -- Frequently executed slow queries
)

SELECT 
    sq.query_hash,
    LEFT(sq.query_text, 100) || '...' as query_preview,
    sq.execution_count,
    ROUND(sq.avg_time, 0) as avg_execution_ms,
    sq.max_time as max_execution_ms,
    ROUND(sq.avg_examined, 0) as avg_rows_examined,
    ROUND(sq.avg_returned, 0) as avg_rows_returned,
    sq.query_pattern,

    -- Selectivity analysis
    CASE 
        WHEN sq.avg_examined > 0 THEN 
            ROUND((sq.avg_returned / sq.avg_examined) * 100, 2)
        ELSE 0
    END as selectivity_percent,

    -- Impact assessment
    ROUND(sq.execution_count * sq.avg_time, 0) as total_time_impact_ms,

    -- Basic optimization suggestions (very limited)
    CASE 
        WHEN sq.query_pattern = 'equality_filter' AND sq.avg_examined > sq.avg_returned * 10 THEN 
            'Add single-column index on equality filter columns'
        WHEN sq.query_pattern = 'range_filter' AND sq.avg_time > 1000 THEN 
            'Consider range-optimized index or query rewrite'
        WHEN sq.query_pattern = 'sorting' AND sq.avg_time > 800 THEN 
            'Add index supporting ORDER BY clause'
        WHEN sq.query_pattern = 'aggregation' AND sq.avg_examined > 10000 THEN 
            'Consider partial index or pre-aggregated data'
        WHEN sq.avg_examined > sq.avg_returned * 100 THEN 
            'Review query selectivity and indexing strategy'
        ELSE 'Manual analysis required'
    END as optimization_suggestion,

    -- Priority assessment
    CASE 
        WHEN sq.execution_count > 100 AND sq.avg_time > 1000 THEN 'high'
        WHEN sq.execution_count > 50 OR sq.avg_time > 2000 THEN 'medium'
        ELSE 'low'
    END as optimization_priority

FROM slow_queries sq
ORDER BY 
    CASE 
        WHEN sq.execution_count > 100 AND sq.avg_time > 1000 THEN 1
        WHEN sq.execution_count > 50 OR sq.avg_time > 2000 THEN 2
        ELSE 3
    END,
    (sq.execution_count * sq.avg_time) DESC;

-- Problems with traditional query optimization approaches:
-- 1. Manual index management with no automated recommendations
-- 2. Limited query plan analysis and optimization guidance
-- 3. Basic performance metrics with no comprehensive analysis
-- 4. No real-time query performance monitoring
-- 5. Minimal index effectiveness assessment
-- 6. Complex manual tuning procedures requiring deep database expertise
-- 7. No support for compound index optimization strategies
-- 8. Limited visibility into query execution patterns and resource usage
-- 9. Basic alerting with no proactive optimization suggestions
-- 10. No integration with application performance monitoring systems

MongoDB provides comprehensive index optimization with advanced query performance analysis capabilities:

// MongoDB Advanced Index Optimization and Query Performance Analysis
const { MongoClient } = require('mongodb');
const { EventEmitter } = require('events');

// Comprehensive MongoDB Performance Optimizer
class AdvancedPerformanceOptimizer extends EventEmitter {
  constructor(mongoUri, optimizationConfig = {}) {
    super();
    this.mongoUri = mongoUri;
    this.client = null;
    this.db = null;

    // Advanced optimization configuration
    this.config = {
      // Performance analysis configuration
      enableQueryProfiling: optimizationConfig.enableQueryProfiling !== false,
      profilingSampleRate: optimizationConfig.profilingSampleRate || 0.1,
      slowQueryThresholdMs: optimizationConfig.slowQueryThresholdMs || 100,

      // Index optimization settings
      enableAutomaticIndexRecommendations: optimizationConfig.enableAutomaticIndexRecommendations !== false,
      enableIndexUsageAnalysis: optimizationConfig.enableIndexUsageAnalysis !== false,
      enableCompoundIndexOptimization: optimizationConfig.enableCompoundIndexOptimization || false,

      // Monitoring and alerting
      enablePerformanceMonitoring: optimizationConfig.enablePerformanceMonitoring !== false,
      enableRealTimeAnalysis: optimizationConfig.enableRealTimeAnalysis || false,
      enablePerformanceAlerting: optimizationConfig.enablePerformanceAlerting || false,

      // Analysis parameters
      analysisWindowHours: optimizationConfig.analysisWindowHours || 24,
      minQueryExecutions: optimizationConfig.minQueryExecutions || 10,
      indexUsageThreshold: optimizationConfig.indexUsageThreshold || 0.1,

      // Resource optimization
      enableResourceOptimization: optimizationConfig.enableResourceOptimization || false,
      enableQueryPlanCaching: optimizationConfig.enableQueryPlanCaching !== false,
      enableConnectionPoolOptimization: optimizationConfig.enableConnectionPoolOptimization || false
    };

    // Performance tracking and analysis state
    this.queryPatterns = new Map();
    this.indexUsageStats = new Map();
    this.performanceMetrics = new Map();
    this.optimizationRecommendations = [];

    // Query execution tracking
    this.queryExecutionHistory = [];
    this.slowQueryLog = [];
    this.indexEffectivenessCache = new Map();

    this.initializePerformanceOptimizer();
  }

  async initializePerformanceOptimizer() {
    console.log('Initializing advanced MongoDB performance optimizer...');

    try {
      // Connect to MongoDB
      this.client = new MongoClient(this.mongoUri, {
        // Optimized connection settings
        maxPoolSize: 20,
        minPoolSize: 5,
        maxIdleTimeMS: 30000,
        serverSelectionTimeoutMS: 5000,
        heartbeatFrequencyMS: 10000
      });

      await this.client.connect();
      this.db = this.client.db();

      // Setup performance monitoring infrastructure
      await this.setupPerformanceInfrastructure();

      // Enable query profiling if configured
      if (this.config.enableQueryProfiling) {
        await this.enableQueryProfiling();
      }

      // Start real-time monitoring if enabled
      if (this.config.enableRealTimeAnalysis) {
        await this.startRealTimeMonitoring();
      }

      // Initialize index analysis
      if (this.config.enableIndexUsageAnalysis) {
        await this.initializeIndexAnalysis();
      }

      console.log('Advanced performance optimizer initialized successfully');

    } catch (error) {
      console.error('Error initializing performance optimizer:', error);
      throw error;
    }
  }

  async setupPerformanceInfrastructure() {
    console.log('Setting up performance monitoring infrastructure...');

    try {
      // Create collections for performance tracking
      const collections = {
        queryPerformanceLog: this.db.collection('query_performance_log'),
        indexUsageStats: this.db.collection('index_usage_stats'),
        performanceMetrics: this.db.collection('performance_metrics'),
        optimizationRecommendations: this.db.collection('optimization_recommendations'),
        queryPatterns: this.db.collection('query_patterns')
      };

      // Create indexes for performance collections
      await collections.queryPerformanceLog.createIndex(
        { timestamp: -1, executionTimeMs: -1 },
        { background: true, expireAfterSeconds: 7 * 24 * 60 * 60 } // 7 days retention
      );

      await collections.indexUsageStats.createIndex(
        { collection: 1, indexName: 1, timestamp: -1 },
        { background: true }
      );

      await collections.performanceMetrics.createIndex(
        { metricType: 1, timestamp: -1 },
        { background: true, expireAfterSeconds: 30 * 24 * 60 * 60 } // 30 days retention
      );

      this.collections = collections;

    } catch (error) {
      console.error('Error setting up performance infrastructure:', error);
      throw error;
    }
  }

  async enableQueryProfiling() {
    console.log('Enabling MongoDB query profiling...');

    try {
      // Set profiling level based on configuration
      await this.db.admin().command({
        profile: 2, // Profile all operations
        slowms: this.config.slowQueryThresholdMs,
        sampleRate: this.config.profilingSampleRate
      });

      console.log(`Query profiling enabled with ${this.config.slowQueryThresholdMs}ms threshold and ${this.config.profilingSampleRate} sample rate`);

    } catch (error) {
      console.error('Error enabling query profiling:', error);
      // Don't throw - profiling is optional
    }
  }

  async analyzeQueryPerformance(timeRangeHours = 24) {
    console.log(`Analyzing query performance for the last ${timeRangeHours} hours...`);

    try {
      const analysisStartTime = new Date(Date.now() - (timeRangeHours * 60 * 60 * 1000));

      // Analyze profiler data for slow queries and patterns
      const slowQueries = await this.analyzeSlowQueries(analysisStartTime);
      const queryPatterns = await this.analyzeQueryPatterns(analysisStartTime);
      const indexUsageAnalysis = await this.analyzeIndexUsage(analysisStartTime);

      // Generate performance insights
      const performanceInsights = {
        analysisTimestamp: new Date(),
        timeRangeHours: timeRangeHours,

        // Query performance summary
        queryPerformanceSummary: {
          totalQueries: slowQueries.totalQueries,
          slowQueries: slowQueries.slowQueryCount,
          averageExecutionTime: slowQueries.averageExecutionTime,
          p95ExecutionTime: slowQueries.p95ExecutionTime,
          p99ExecutionTime: slowQueries.p99ExecutionTime,

          // Query type distribution
          queryTypeDistribution: queryPatterns.queryTypeDistribution,

          // Resource usage patterns
          resourceUsage: {
            totalExaminedDocuments: slowQueries.totalExaminedDocuments,
            totalReturnedDocuments: slowQueries.totalReturnedDocuments,
            averageSelectivityRatio: slowQueries.averageSelectivityRatio
          }
        },

        // Index effectiveness analysis
        indexEffectiveness: {
          totalIndexes: indexUsageAnalysis.totalIndexes,
          activelyUsedIndexes: indexUsageAnalysis.activelyUsedIndexes,
          unusedIndexes: indexUsageAnalysis.unusedIndexes,
          inefficientIndexes: indexUsageAnalysis.inefficientIndexes,

          // Index usage patterns
          indexUsagePatterns: indexUsageAnalysis.usagePatterns,

          // Index performance metrics
          averageIndexSelectivity: indexUsageAnalysis.averageSelectivity,
          indexSizeOverhead: indexUsageAnalysis.totalIndexSizeBytes
        },

        // Performance bottlenecks
        performanceBottlenecks: await this.identifyPerformanceBottlenecks(slowQueries, queryPatterns, indexUsageAnalysis),

        // Optimization opportunities
        optimizationOpportunities: await this.generateOptimizationRecommendations(slowQueries, queryPatterns, indexUsageAnalysis)
      };

      // Store performance analysis results
      await this.collections.performanceMetrics.insertOne({
        metricType: 'comprehensive_analysis',
        timestamp: new Date(),
        analysisResults: performanceInsights
      });

      this.emit('performanceAnalysisCompleted', performanceInsights);

      return {
        success: true,
        analysisResults: performanceInsights
      };

    } catch (error) {
      console.error('Error analyzing query performance:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async analyzeSlowQueries(startTime) {
    console.log('Analyzing slow query patterns...');

    try {
      // Query the profiler collection for slow queries
      const profilerCollection = this.db.collection('system.profile');

      const slowQueryAggregation = [
        {
          $match: {
            ts: { $gte: startTime },
            op: { $in: ['query', 'getmore'] }, // Focus on read operations
            millis: { $gte: this.config.slowQueryThresholdMs }
          }
        },
        {
          $addFields: {
            // Normalize query shape for pattern analysis
            queryShape: {
              $function: {
                body: function(command) {
                  // Simplified query shape normalization
                  if (!command || !command.find) return 'unknown';

                  const filter = command.find.filter || {};
                  const sort = command.find.sort || {};
                  const projection = command.find.projection || {};

                  // Create shape by replacing values with type indicators
                  const shapeFilter = Object.keys(filter).reduce((acc, key) => {
                    acc[key] = typeof filter[key];
                    return acc;
                  }, {});

                  return JSON.stringify({
                    filter: shapeFilter,
                    sort: Object.keys(sort),
                    projection: Object.keys(projection)
                  });
                },
                args: ['$command'],
                lang: 'js'
              }
            },

            // Extract collection name
            targetCollection: {
              $ifNull: ['$command.find', '$command.collection']
            },

            // Calculate selectivity ratio
            selectivityRatio: {
              $cond: [
                { $and: [{ $gt: ['$docsExamined', 0] }, { $gt: ['$nreturned', 0] }] },
                { $divide: ['$nreturned', '$docsExamined'] },
                0
              ]
            }
          }
        },
        {
          $group: {
            _id: {
              queryShape: '$queryShape',
              collection: '$targetCollection'
            },

            // Execution statistics
            executionCount: { $sum: 1 },
            totalExecutionTime: { $sum: '$millis' },
            averageExecutionTime: { $avg: '$millis' },
            maxExecutionTime: { $max: '$millis' },
            minExecutionTime: { $min: '$millis' },

            // Document examination statistics
            totalDocsExamined: { $sum: '$docsExamined' },
            totalDocsReturned: { $sum: '$nreturned' },
            averageSelectivity: { $avg: '$selectivityRatio' },

            // Index usage tracking
            indexesUsed: { $addToSet: '$planSummary' },

            // Resource usage
            totalKeysExamined: { $sum: '$keysExamined' },

            // Sample query for reference
            sampleQuery: { $first: '$command' },
            sampleTimestamp: { $first: '$ts' }
          }
        },
        {
          $addFields: {
            // Calculate performance impact
            performanceImpact: {
              $multiply: ['$executionCount', '$averageExecutionTime']
            },

            // Assess query efficiency
            queryEfficiency: {
              $cond: [
                { $gt: ['$averageSelectivity', 0.1] },
                'efficient',
                { $cond: [{ $gt: ['$averageSelectivity', 0.01] }, 'moderate', 'inefficient'] }
              ]
            }
          }
        },
        {
          $sort: { performanceImpact: -1 }
        },
        {
          $limit: 100 // Top 100 slow query patterns
        }
      ];

      const slowQueryResults = await profilerCollection.aggregate(slowQueryAggregation).toArray();

      // Calculate summary statistics
      const totalQueries = slowQueryResults.reduce((sum, query) => sum + query.executionCount, 0);
      const totalExecutionTime = slowQueryResults.reduce((sum, query) => sum + query.totalExecutionTime, 0);
      const allExecutionTimes = slowQueryResults.flatMap(query => Array(query.executionCount).fill(query.averageExecutionTime));

      // Calculate percentiles
      allExecutionTimes.sort((a, b) => a - b);
      const p95Index = Math.floor(allExecutionTimes.length * 0.95);
      const p99Index = Math.floor(allExecutionTimes.length * 0.99);

      return {
        slowQueryPatterns: slowQueryResults,
        totalQueries: totalQueries,
        slowQueryCount: slowQueryResults.length,
        averageExecutionTime: totalQueries > 0 ? totalExecutionTime / totalQueries : 0,
        p95ExecutionTime: allExecutionTimes[p95Index] || 0,
        p99ExecutionTime: allExecutionTimes[p99Index] || 0,
        totalExaminedDocuments: slowQueryResults.reduce((sum, query) => sum + query.totalDocsExamined, 0),
        totalReturnedDocuments: slowQueryResults.reduce((sum, query) => sum + query.totalDocsReturned, 0),
        averageSelectivityRatio: slowQueryResults.length > 0 
          ? slowQueryResults.reduce((sum, query) => sum + (query.averageSelectivity || 0), 0) / slowQueryResults.length 
          : 0
      };

    } catch (error) {
      console.error('Error analyzing slow queries:', error);
      throw error;
    }
  }

  async analyzeQueryPatterns(startTime) {
    console.log('Analyzing query execution patterns...');

    try {
      const profilerCollection = this.db.collection('system.profile');

      // Analyze query type distribution and patterns
      const queryPatternAggregation = [
        {
          $match: {
            ts: { $gte: startTime },
            op: { $in: ['query', 'getmore', 'update', 'delete', 'insert'] }
          }
        },
        {
          $addFields: {
            // Categorize query operations
            queryCategory: {
              $switch: {
                branches: [
                  {
                    case: { $eq: ['$op', 'query'] },
                    then: {
                      $cond: [
                        { $ifNull: ['$command.find.sort', false] },
                        'sorted_query',
                        { $cond: [
                          { $gt: [{ $size: { $objectToArray: { $ifNull: ['$command.find.filter', {}] } } }, 0] },
                          'filtered_query',
                          'full_scan'
                        ]}
                      ]
                    }
                  },
                  { case: { $eq: ['$op', 'update'] }, then: 'update_operation' },
                  { case: { $eq: ['$op', 'delete'] }, then: 'delete_operation' },
                  { case: { $eq: ['$op', 'insert'] }, then: 'insert_operation' }
                ],
                default: 'other_operation'
              }
            },

            // Analyze query complexity
            queryComplexity: {
              $switch: {
                branches: [
                  {
                    case: { $and: [
                      { $eq: ['$op', 'query'] },
                      { $gt: [{ $size: { $objectToArray: { $ifNull: ['$command.find.filter', {}] } } }, 5] }
                    ]},
                    then: 'complex'
                  },
                  {
                    case: { $and: [
                      { $eq: ['$op', 'query'] },
                      { $gt: [{ $size: { $objectToArray: { $ifNull: ['$command.find.filter', {}] } } }, 2] }
                    ]},
                    then: 'moderate'
                  }
                ],
                default: 'simple'
              }
            }
          }
        },
        {
          $group: {
            _id: {
              collection: { $ifNull: ['$command.find', '$command.collection', '$ns'] },
              queryCategory: '$queryCategory',
              queryComplexity: '$queryComplexity'
            },

            // Pattern statistics
            executionCount: { $sum: 1 },
            averageExecutionTime: { $avg: '$millis' },
            totalExecutionTime: { $sum: '$millis' },

            // Resource usage patterns
            averageDocsExamined: { $avg: '$docsExamined' },
            averageDocsReturned: { $avg: '$nreturned' },

            // Index usage patterns
            commonIndexes: { $addToSet: '$planSummary' },

            // Performance characteristics
            maxExecutionTime: { $max: '$millis' },
            minExecutionTime: { $min: '$millis' }
          }
        },
        {
          $sort: { totalExecutionTime: -1 }
        }
      ];

      const queryPatternResults = await profilerCollection.aggregate(queryPatternAggregation).toArray();

      // Calculate query type distribution
      const queryTypeDistribution = queryPatternResults.reduce((distribution, pattern) => {
        const category = pattern._id.queryCategory;
        if (!distribution[category]) {
          distribution[category] = {
            count: 0,
            totalTime: 0,
            avgTime: 0
          };
        }

        distribution[category].count += pattern.executionCount;
        distribution[category].totalTime += pattern.totalExecutionTime;
        distribution[category].avgTime = distribution[category].totalTime / distribution[category].count;

        return distribution;
      }, {});

      return {
        queryPatterns: queryPatternResults,
        queryTypeDistribution: queryTypeDistribution,
        totalPatterns: queryPatternResults.length
      };

    } catch (error) {
      console.error('Error analyzing query patterns:', error);
      throw error;
    }
  }

  async analyzeIndexUsage(startTime) {
    console.log('Analyzing index usage effectiveness...');

    try {
      // Get all collections for comprehensive index analysis
      const collections = await this.db.listCollections().toArray();
      const indexAnalysisResults = [];

      for (const collectionInfo of collections) {
        if (collectionInfo.type === 'collection') {
          const collection = this.db.collection(collectionInfo.name);

          // Get index information
          const indexes = await collection.indexes();

          // Analyze each index
          for (const index of indexes) {
            try {
              // Get index usage statistics
              const indexStats = await collection.aggregate([
                { $indexStats: {} },
                { $match: { name: index.name } }
              ]).toArray();

              const indexStat = indexStats[0];

              if (indexStat) {
                // Calculate index effectiveness metrics
                const indexAnalysis = {
                  collection: collectionInfo.name,
                  indexName: index.name,
                  indexKeys: index.key,
                  indexType: this.determineIndexType(index),

                  // Usage statistics
                  usageCount: indexStat.accesses?.ops || 0,
                  lastUsed: indexStat.accesses?.since || null,

                  // Size and storage information
                  indexSize: index.size || 0,

                  // Effectiveness calculations
                  usageEffectiveness: this.calculateIndexEffectiveness(indexStat, index),

                  // Index health assessment
                  healthStatus: this.assessIndexHealth(indexStat, index),

                  // Optimization opportunities
                  optimizationOpportunities: await this.identifyIndexOptimizations(collection, index, indexStat)
                };

                indexAnalysisResults.push(indexAnalysis);
              }

            } catch (indexError) {
              console.warn(`Error analyzing index ${index.name} on ${collectionInfo.name}:`, indexError.message);
            }
          }
        }
      }

      // Calculate summary statistics
      const totalIndexes = indexAnalysisResults.length;
      const activelyUsedIndexes = indexAnalysisResults.filter(index => index.usageCount > 0).length;
      const unusedIndexes = indexAnalysisResults.filter(index => index.usageCount === 0);
      const inefficientIndexes = indexAnalysisResults.filter(index => 
        index.healthStatus === 'inefficient' || index.usageEffectiveness < 0.1
      );

      // Analyze usage patterns
      const usagePatterns = this.analyzeIndexUsagePatterns(indexAnalysisResults);

      return {
        indexAnalysisResults: indexAnalysisResults,
        totalIndexes: totalIndexes,
        activelyUsedIndexes: activelyUsedIndexes,
        unusedIndexes: unusedIndexes,
        inefficientIndexes: inefficientIndexes,
        usagePatterns: usagePatterns,
        averageSelectivity: this.calculateAverageIndexSelectivity(indexAnalysisResults),
        totalIndexSizeBytes: indexAnalysisResults.reduce((total, index) => total + (index.indexSize || 0), 0)
      };

    } catch (error) {
      console.error('Error analyzing index usage:', error);
      throw error;
    }
  }

  async generateOptimizationRecommendations(slowQueries, queryPatterns, indexUsage) {
    console.log('Generating performance optimization recommendations...');

    try {
      const recommendations = [];

      // Analyze slow queries for index recommendations
      for (const slowQuery of slowQueries.slowQueryPatterns) {
        if (slowQuery.averageSelectivity < 0.1 && slowQuery.executionCount > this.config.minQueryExecutions) {
          recommendations.push({
            type: 'index_recommendation',
            priority: 'high',
            collection: slowQuery._id.collection,
            issue: 'Low selectivity query pattern with high execution frequency',
            recommendation: await this.generateIndexRecommendation(slowQuery),
            expectedImprovement: this.estimatePerformanceImprovement(slowQuery),
            implementationComplexity: 'medium',
            estimatedImpact: slowQuery.performanceImpact
          });
        }
      }

      // Analyze unused indexes
      for (const unusedIndex of indexUsage.unusedIndexes) {
        if (unusedIndex.indexName !== '_id_') { // Never recommend dropping _id index
          recommendations.push({
            type: 'index_cleanup',
            priority: 'medium',
            collection: unusedIndex.collection,
            issue: `Unused index consuming storage space: ${unusedIndex.indexName}`,
            recommendation: `Consider dropping unused index '${unusedIndex.indexName}' to save ${Math.round((unusedIndex.indexSize || 0) / 1024 / 1024)} MB storage`,
            expectedImprovement: {
              storageReduction: unusedIndex.indexSize || 0,
              maintenanceOverheadReduction: 'low'
            },
            implementationComplexity: 'low',
            estimatedImpact: unusedIndex.indexSize || 0
          });
        }
      }

      // Analyze compound index opportunities
      if (this.config.enableCompoundIndexOptimization) {
        const compoundIndexOpportunities = await this.analyzeCompoundIndexOpportunities(queryPatterns);
        recommendations.push(...compoundIndexOpportunities);
      }

      // Sort recommendations by priority and estimated impact
      recommendations.sort((a, b) => {
        const priorityOrder = { high: 3, medium: 2, low: 1 };
        const priorityDiff = priorityOrder[b.priority] - priorityOrder[a.priority];

        if (priorityDiff !== 0) return priorityDiff;
        return (b.estimatedImpact || 0) - (a.estimatedImpact || 0);
      });

      return recommendations.slice(0, 20); // Return top 20 recommendations

    } catch (error) {
      console.error('Error generating optimization recommendations:', error);
      return [];
    }
  }

  async generateIndexRecommendation(slowQuery) {
    try {
      // Analyze the query shape to determine optimal index structure
      const queryShape = JSON.parse(slowQuery._id.queryShape);
      const filterFields = Object.keys(queryShape.filter || {});
      const sortFields = queryShape.sort || [];

      let recommendedIndex = {};

      // Build compound index recommendation based on query patterns
      // Rule 1: Equality filters first
      filterFields.forEach(field => {
        if (queryShape.filter[field] === 'string' || queryShape.filter[field] === 'number') {
          recommendedIndex[field] = 1;
        }
      });

      // Rule 2: Range filters after equality filters
      filterFields.forEach(field => {
        if (queryShape.filter[field] === 'object') { // Likely range query
          if (!recommendedIndex[field]) {
            recommendedIndex[field] = 1;
          }
        }
      });

      // Rule 3: Sort fields last
      sortFields.forEach(field => {
        if (!recommendedIndex[field]) {
          recommendedIndex[field] = 1;
        }
      });

      return {
        suggestedIndex: recommendedIndex,
        indexCommand: `db.${slowQuery._id.collection}.createIndex(${JSON.stringify(recommendedIndex)})`,
        reasoning: `Compound index optimized for query pattern with ${filterFields.length} filter fields and ${sortFields.length} sort fields`,
        estimatedSize: this.estimateIndexSize(recommendedIndex, slowQuery._id.collection)
      };

    } catch (error) {
      console.error('Error generating index recommendation:', error);
      return {
        suggestedIndex: {},
        indexCommand: 'Manual analysis required',
        reasoning: 'Unable to analyze query pattern automatically',
        estimatedSize: 0
      };
    }
  }

  async explainQuery(collection, query, options = {}) {
    console.log(`Explaining query execution plan for collection: ${collection}`);

    try {
      const targetCollection = this.db.collection(collection);

      // Execute explain with detailed execution stats
      const explainResult = await targetCollection
        .find(query.filter || {}, options)
        .sort(query.sort || {})
        .limit(query.limit || 0)
        .explain('executionStats');

      // Analyze execution plan
      const executionAnalysis = this.analyzeExecutionPlan(explainResult);

      // Generate optimization insights
      const optimizationInsights = await this.generateQueryOptimizationInsights(
        collection, 
        query, 
        explainResult, 
        executionAnalysis
      );

      return {
        success: true,
        query: query,
        executionPlan: explainResult,
        executionAnalysis: executionAnalysis,
        optimizationInsights: optimizationInsights,
        explainTimestamp: new Date()
      };

    } catch (error) {
      console.error(`Error explaining query for collection ${collection}:`, error);
      return {
        success: false,
        collection: collection,
        query: query,
        error: error.message
      };
    }
  }

  analyzeExecutionPlan(explainResult) {
    try {
      const executionStats = explainResult.executionStats;
      const winningPlan = explainResult.queryPlanner?.winningPlan;

      const analysis = {
        // Basic execution metrics
        executionTime: executionStats.executionTimeMillis,
        documentsExamined: executionStats.totalDocsExamined,
        documentsReturned: executionStats.totalDocsReturned,
        keysExamined: executionStats.totalKeysExamined,

        // Efficiency calculations
        selectivityRatio: executionStats.totalDocsExamined > 0 
          ? executionStats.totalDocsReturned / executionStats.totalDocsExamined 
          : 0,

        indexEfficiency: executionStats.totalKeysExamined > 0 
          ? executionStats.totalDocsReturned / executionStats.totalKeysExamined 
          : 0,

        // Plan analysis
        planType: this.identifyPlanType(winningPlan),
        indexesUsed: this.extractIndexesUsed(winningPlan),
        hasSort: this.hasSortStage(winningPlan),
        hasBlockingSort: this.hasBlockingSortStage(winningPlan),

        // Performance assessment
        performanceRating: this.assessQueryPerformance(executionStats, winningPlan),

        // Resource usage
        workingSetSize: executionStats.workingSetSize || 0,

        // Optimization opportunities
        needsOptimization: this.needsOptimization(executionStats, winningPlan)
      };

      return analysis;

    } catch (error) {
      console.error('Error analyzing execution plan:', error);
      return {
        error: 'Failed to analyze execution plan',
        executionTime: 0,
        documentsExamined: 0,
        documentsReturned: 0,
        needsOptimization: true
      };
    }
  }

  async generateQueryOptimizationInsights(collection, query, explainResult, executionAnalysis) {
    try {
      const insights = [];

      // Check for full collection scans
      if (executionAnalysis.planType === 'COLLSCAN') {
        insights.push({
          type: 'full_scan_detected',
          severity: 'high',
          message: 'Query is performing a full collection scan',
          recommendation: 'Add an appropriate index to avoid collection scanning',
          suggestedIndex: await this.suggestIndexForQuery(query)
        });
      }

      // Check for low selectivity
      if (executionAnalysis.selectivityRatio < 0.1) {
        insights.push({
          type: 'low_selectivity',
          severity: 'medium',
          message: `Query selectivity is low (${(executionAnalysis.selectivityRatio * 100).toFixed(2)}%)`,
          recommendation: 'Consider more selective query conditions or compound indexes',
          currentSelectivity: executionAnalysis.selectivityRatio
        });
      }

      // Check for blocking sorts
      if (executionAnalysis.hasBlockingSort) {
        insights.push({
          type: 'blocking_sort',
          severity: 'high',
          message: 'Query requires in-memory sorting which can be expensive',
          recommendation: 'Create an index that supports the sort order',
          suggestedIndex: this.suggestSortIndex(query.sort)
        });
      }

      // Check for excessive key examination
      if (executionAnalysis.keysExamined > executionAnalysis.documentsReturned * 10) {
        insights.push({
          type: 'excessive_key_examination',
          severity: 'medium',
          message: 'Query is examining significantly more keys than documents returned',
          recommendation: 'Consider compound indexes to improve key examination efficiency',
          keysExamined: executionAnalysis.keysExamined,
          documentsReturned: executionAnalysis.documentsReturned
        });
      }

      // Check execution time
      if (executionAnalysis.executionTime > this.config.slowQueryThresholdMs) {
        insights.push({
          type: 'slow_execution',
          severity: executionAnalysis.executionTime > this.config.slowQueryThresholdMs * 5 ? 'high' : 'medium',
          message: `Query execution time (${executionAnalysis.executionTime}ms) exceeds threshold`,
          recommendation: 'Consider query optimization or index improvements',
          executionTime: executionAnalysis.executionTime,
          threshold: this.config.slowQueryThresholdMs
        });
      }

      return insights;

    } catch (error) {
      console.error('Error generating query optimization insights:', error);
      return [];
    }
  }

  async getPerformanceMetrics(timeRangeHours = 24) {
    console.log(`Retrieving performance metrics for the last ${timeRangeHours} hours...`);

    try {
      const startTime = new Date(Date.now() - (timeRangeHours * 60 * 60 * 1000));

      // Get comprehensive performance metrics
      const metrics = await this.collections.performanceMetrics
        .find({
          timestamp: { $gte: startTime }
        })
        .sort({ timestamp: -1 })
        .toArray();

      // Calculate summary statistics
      const performanceSummary = this.calculatePerformanceSummary(metrics);

      // Get current optimization recommendations
      const currentRecommendations = await this.collections.optimizationRecommendations
        .find({
          createdAt: { $gte: startTime },
          status: { $ne: 'implemented' }
        })
        .sort({ priority: -1, estimatedImpact: -1 })
        .limit(10)
        .toArray();

      return {
        success: true,
        timeRangeHours: timeRangeHours,
        metricsCollected: metrics.length,
        performanceSummary: performanceSummary,
        currentRecommendations: currentRecommendations,
        lastUpdated: new Date()
      };

    } catch (error) {
      console.error('Error retrieving performance metrics:', error);
      return {
        success: false,
        error: error.message,
        timeRangeHours: timeRangeHours
      };
    }
  }

  calculatePerformanceSummary(metrics) {
    if (metrics.length === 0) {
      return {
        totalQueries: 0,
        averageExecutionTime: 0,
        slowQueries: 0,
        indexEffectiveness: 'unknown'
      };
    }

    // Extract metrics from analysis results
    const analysisResults = metrics
      .filter(metric => metric.metricType === 'comprehensive_analysis')
      .map(metric => metric.analysisResults);

    if (analysisResults.length === 0) {
      return {
        totalQueries: 0,
        averageExecutionTime: 0,
        slowQueries: 0,
        indexEffectiveness: 'no_data'
      };
    }

    const latestAnalysis = analysisResults[0];

    return {
      totalQueries: latestAnalysis.queryPerformanceSummary?.totalQueries || 0,
      averageExecutionTime: latestAnalysis.queryPerformanceSummary?.averageExecutionTime || 0,
      p95ExecutionTime: latestAnalysis.queryPerformanceSummary?.p95ExecutionTime || 0,
      slowQueries: latestAnalysis.queryPerformanceSummary?.slowQueries || 0,

      // Index effectiveness
      indexEffectiveness: {
        totalIndexes: latestAnalysis.indexEffectiveness?.totalIndexes || 0,
        activelyUsedIndexes: latestAnalysis.indexEffectiveness?.activelyUsedIndexes || 0,
        unusedIndexes: latestAnalysis.indexEffectiveness?.unusedIndexes?.length || 0,
        averageSelectivity: latestAnalysis.indexEffectiveness?.averageIndexSelectivity || 0
      },

      // Performance trends
      performanceBottlenecks: latestAnalysis.performanceBottlenecks || [],
      optimizationOpportunities: latestAnalysis.optimizationOpportunities?.length || 0
    };
  }

  // Utility methods for performance analysis

  determineIndexType(index) {
    if (index.name === '_id_') return 'primary';
    if (index.unique) return 'unique';
    if (index.sparse) return 'sparse';
    if (index.partialFilterExpression) return 'partial';
    if (Object.values(index.key).includes('text')) return 'text';
    if (Object.values(index.key).includes('2dsphere')) return 'geospatial';
    if (Object.keys(index.key).length > 1) return 'compound';
    return 'single';
  }

  calculateIndexEffectiveness(indexStat, index) {
    const usageCount = indexStat.accesses?.ops || 0;
    const indexSize = index.size || 0;

    // Calculate effectiveness based on usage frequency and size efficiency
    if (usageCount === 0) return 0;
    if (indexSize === 0) return 1;

    // Simple effectiveness metric: usage per MB of index size
    const sizeInMB = indexSize / (1024 * 1024);
    return Math.min(usageCount / Math.max(sizeInMB, 1), 100);
  }

  assessIndexHealth(indexStat, index) {
    const usageCount = indexStat.accesses?.ops || 0;
    const effectiveness = this.calculateIndexEffectiveness(indexStat, index);

    if (usageCount === 0) return 'unused';
    if (effectiveness < 0.1) return 'inefficient';
    if (effectiveness > 10) return 'highly_effective';
    return 'moderate';
  }

  identifyPlanType(winningPlan) {
    if (!winningPlan) return 'unknown';
    if (winningPlan.stage === 'COLLSCAN') return 'COLLSCAN';
    if (winningPlan.stage === 'IXSCAN') return 'IXSCAN';
    if (winningPlan.inputStage?.stage === 'IXSCAN') return 'IXSCAN';
    return winningPlan.stage || 'unknown';
  }

  extractIndexesUsed(winningPlan) {
    const indexes = [];

    function extractFromStage(stage) {
      if (stage.indexName) {
        indexes.push(stage.indexName);
      }
      if (stage.inputStage) {
        extractFromStage(stage.inputStage);
      }
      if (stage.inputStages) {
        stage.inputStages.forEach(extractFromStage);
      }
    }

    if (winningPlan) {
      extractFromStage(winningPlan);
    }

    return [...new Set(indexes)]; // Remove duplicates
  }

  hasSortStage(winningPlan) {
    if (!winningPlan) return false;

    function checkForSort(stage) {
      if (stage.stage === 'SORT') return true;
      if (stage.inputStage) return checkForSort(stage.inputStage);
      if (stage.inputStages) return stage.inputStages.some(checkForSort);
      return false;
    }

    return checkForSort(winningPlan);
  }

  hasBlockingSortStage(winningPlan) {
    if (!winningPlan) return false;

    function checkForBlockingSort(stage) {
      // A sort is blocking if it's not supported by an index
      if (stage.stage === 'SORT' && !stage.inputStage?.stage?.includes('IXSCAN')) {
        return true;
      }
      if (stage.inputStage) return checkForBlockingSort(stage.inputStage);
      if (stage.inputStages) return stage.inputStages.some(checkForBlockingSort);
      return false;
    }

    return checkForBlockingSort(winningPlan);
  }

  assessQueryPerformance(executionStats, winningPlan) {
    const executionTime = executionStats.executionTimeMillis || 0;
    const selectivityRatio = executionStats.totalDocsExamined > 0 
      ? executionStats.totalDocsReturned / executionStats.totalDocsExamined 
      : 0;

    // Performance rating based on multiple factors
    let score = 100;

    // Penalize slow execution
    if (executionTime > 1000) score -= 40;
    else if (executionTime > 500) score -= 20;
    else if (executionTime > 100) score -= 10;

    // Penalize low selectivity
    if (selectivityRatio < 0.01) score -= 30;
    else if (selectivityRatio < 0.1) score -= 15;

    // Penalize full collection scans
    if (winningPlan?.stage === 'COLLSCAN') score -= 25;

    // Penalize blocking sorts
    if (this.hasBlockingSortStage(winningPlan)) score -= 15;

    if (score >= 80) return 'excellent';
    if (score >= 60) return 'good';
    if (score >= 40) return 'fair';
    return 'poor';
  }

  needsOptimization(executionStats, winningPlan) {
    const executionTime = executionStats.executionTimeMillis || 0;
    const selectivityRatio = executionStats.totalDocsExamined > 0 
      ? executionStats.totalDocsReturned / executionStats.totalDocsExamined 
      : 0;

    return executionTime > this.config.slowQueryThresholdMs ||
           selectivityRatio < 0.1 ||
           winningPlan?.stage === 'COLLSCAN' ||
           this.hasBlockingSortStage(winningPlan);
  }

  estimatePerformanceImprovement(slowQuery) {
    return {
      executionTimeReduction: '60-80%',
      documentExaminationReduction: '90-95%',
      resourceUsageReduction: '70-85%',
      confidenceLevel: 'high'
    };
  }

  estimateIndexSize(indexKeys, collection) {
    // Simplified index size estimation
    const keyCount = Object.keys(indexKeys).length;
    const estimatedDocumentSize = 100; // Average document size estimate
    const estimatedCollectionSize = 100000; // Estimate

    return keyCount * estimatedDocumentSize * estimatedCollectionSize * 0.1;
  }

  async shutdown() {
    console.log('Shutting down performance optimizer...');

    try {
      // Disable profiling
      if (this.config.enableQueryProfiling) {
        await this.db.admin().command({ profile: 0 });
      }

      // Close MongoDB connection
      if (this.client) {
        await this.client.close();
      }

      console.log('Performance optimizer shutdown complete');

    } catch (error) {
      console.error('Error during shutdown:', error);
    }
  }

  // Additional methods would include implementations for:
  // - startRealTimeMonitoring()
  // - initializeIndexAnalysis()
  // - identifyPerformanceBottlenecks()
  // - analyzeIndexUsagePatterns()
  // - calculateAverageIndexSelectivity()
  // - analyzeCompoundIndexOpportunities()
  // - identifyIndexOptimizations()
  // - suggestIndexForQuery()
  // - suggestSortIndex()
}

// Benefits of MongoDB Advanced Performance Optimization:
// - Comprehensive query performance analysis and monitoring
// - Intelligent index optimization recommendations
// - Real-time performance bottleneck identification
// - Advanced execution plan analysis and insights
// - Automated slow query detection and optimization
// - Index usage effectiveness assessment
// - Compound index optimization strategies
// - SQL-compatible performance operations through QueryLeaf integration
// - Production-ready monitoring and alerting capabilities
// - Enterprise-grade performance tuning automation

module.exports = {
  AdvancedPerformanceOptimizer
};

Understanding MongoDB Performance Architecture

Advanced Query Optimization and Index Management Patterns

Implement sophisticated performance optimization workflows for enterprise MongoDB deployments:

// Enterprise-grade performance optimization with advanced analytics capabilities
class EnterprisePerformanceManager extends AdvancedPerformanceOptimizer {
  constructor(mongoUri, enterpriseConfig) {
    super(mongoUri, enterpriseConfig);

    this.enterpriseConfig = {
      ...enterpriseConfig,
      enablePredictiveOptimization: true,
      enableCapacityPlanning: true,
      enableAutomatedTuning: true,
      enablePerformanceForecasting: true,
      enableComplianceReporting: true
    };

    this.setupEnterpriseCapabilities();
    this.initializePredictiveAnalytics();
    this.setupAutomatedOptimization();
  }

  async implementAdvancedOptimizationStrategy() {
    console.log('Implementing enterprise optimization strategy...');

    const optimizationStrategy = {
      // Multi-tier optimization approach
      optimizationTiers: {
        realTimeOptimization: {
          enabled: true,
          responseTimeThreshold: 100,
          automaticIndexCreation: true,
          queryRewriting: true
        },
        batchOptimization: {
          enabled: true,
          analysisInterval: '1h',
          comprehensiveIndexAnalysis: true,
          workloadPatternAnalysis: true
        },
        predictiveOptimization: {
          enabled: true,
          forecastingHorizon: '7d',
          capacityPlanning: true,
          performanceTrendAnalysis: true
        }
      },

      // Advanced analytics
      performanceAnalytics: {
        enableMachineLearning: true,
        anomalyDetection: true,
        performanceForecasting: true,
        workloadCharacterization: true
      }
    };

    return await this.deployOptimizationStrategy(optimizationStrategy);
  }
}

SQL-Style Performance Optimization with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB performance analysis and optimization:

-- QueryLeaf advanced performance optimization with SQL-familiar syntax for MongoDB

-- Comprehensive query performance analysis
WITH query_performance_analysis AS (
    SELECT 
        collection_name,
        query_shape_hash,
        query_pattern_type,

        -- Execution statistics
        COUNT(*) as execution_count,
        AVG(execution_time_ms) as avg_execution_time_ms,
        PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY execution_time_ms) as p95_execution_time_ms,
        MAX(execution_time_ms) as max_execution_time_ms,

        -- Document examination analysis
        AVG(documents_examined) as avg_docs_examined,
        AVG(documents_returned) as avg_docs_returned,
        CASE 
            WHEN AVG(documents_examined) > 0 THEN
                AVG(documents_returned) / AVG(documents_examined)
            ELSE 0
        END as avg_selectivity_ratio,

        -- Index usage analysis
        STRING_AGG(DISTINCT index_name, ', ') as indexes_used,
        AVG(keys_examined) as avg_keys_examined,

        -- Resource utilization
        SUM(execution_time_ms) as total_execution_time_ms,
        AVG(working_set_size_kb) as avg_working_set_kb,

        -- Performance categorization
        CASE 
            WHEN AVG(execution_time_ms) < 50 THEN 'fast'
            WHEN AVG(execution_time_ms) < 200 THEN 'moderate' 
            WHEN AVG(execution_time_ms) < 1000 THEN 'slow'
            ELSE 'very_slow'
        END as performance_category,

        -- Optimization need assessment
        CASE 
            WHEN AVG(execution_time_ms) > 500 OR 
                 (AVG(documents_examined) > AVG(documents_returned) * 100) OR
                 COUNT(*) > 1000 THEN true
            ELSE false
        END as needs_optimization

    FROM QUERY_PERFORMANCE_LOG
    WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
    GROUP BY collection_name, query_shape_hash, query_pattern_type
),

index_effectiveness_analysis AS (
    SELECT 
        collection_name,
        index_name,
        index_type,
        COALESCE(JSON_EXTRACT(index_definition, '$'), '{}') as index_keys,

        -- Usage statistics
        COALESCE(usage_count, 0) as usage_count,
        COALESCE(last_used_timestamp, '1970-01-01'::timestamp) as last_used,

        -- Size and storage analysis
        index_size_bytes,
        ROUND(index_size_bytes / 1024.0 / 1024.0, 2) as index_size_mb,

        -- Effectiveness calculations
        CASE 
            WHEN usage_count = 0 THEN 0
            WHEN index_size_bytes > 0 THEN 
                usage_count / GREATEST((index_size_bytes / 1024.0 / 1024.0), 1)
            ELSE usage_count
        END as effectiveness_score,

        -- Usage categorization
        CASE 
            WHEN usage_count = 0 THEN 'unused'
            WHEN usage_count < 100 THEN 'rarely_used'
            WHEN usage_count < 1000 THEN 'moderately_used'
            ELSE 'frequently_used'
        END as usage_category,

        -- Health assessment
        CASE 
            WHEN usage_count = 0 AND index_name != '_id_' THEN 'candidate_for_removal'
            WHEN usage_count > 0 AND index_size_bytes > 100*1024*1024 AND usage_count < 100 THEN 'review_necessity'
            WHEN usage_count > 1000 THEN 'valuable'
            ELSE 'monitor'
        END as health_status,

        -- Age analysis
        EXTRACT(DAYS FROM (CURRENT_TIMESTAMP - COALESCE(last_used_timestamp, created_timestamp))) as days_since_last_use

    FROM INDEX_USAGE_STATS
    WHERE analysis_timestamp >= CURRENT_TIMESTAMP - INTERVAL '7 days'
),

optimization_opportunities AS (
    SELECT 
        qpa.collection_name,
        qpa.query_pattern_type,
        qpa.execution_count,
        qpa.avg_execution_time_ms,
        qpa.avg_selectivity_ratio,
        qpa.performance_category,
        qpa.needs_optimization,

        -- Performance impact calculation
        qpa.total_execution_time_ms as performance_impact_ms,
        ROUND(qpa.total_execution_time_ms / 1000.0, 2) as performance_impact_seconds,

        -- Index analysis correlation
        COUNT(iea.index_name) as available_indexes,
        STRING_AGG(iea.index_name, ', ') as collection_indexes,
        AVG(iea.effectiveness_score) as avg_index_effectiveness,

        -- Optimization recommendations
        CASE 
            WHEN qpa.avg_selectivity_ratio < 0.01 AND qpa.execution_count > 100 THEN 'create_selective_index'
            WHEN qpa.avg_execution_time_ms > 1000 AND qpa.indexes_used IS NULL THEN 'add_supporting_index'
            WHEN qpa.avg_execution_time_ms > 500 AND qpa.indexes_used LIKE '%COLLSCAN%' THEN 'replace_collection_scan'
            WHEN qpa.performance_category = 'very_slow' THEN 'comprehensive_optimization'
            WHEN qpa.execution_count > 10000 AND qpa.performance_category IN ('slow', 'moderate') THEN 'high_frequency_optimization'
            ELSE 'monitor_performance'
        END as optimization_recommendation,

        -- Priority assessment
        CASE 
            WHEN qpa.total_execution_time_ms > 60000 AND qpa.execution_count > 1000 THEN 'critical'
            WHEN qpa.total_execution_time_ms > 30000 OR qpa.avg_execution_time_ms > 2000 THEN 'high'
            WHEN qpa.total_execution_time_ms > 10000 OR qpa.execution_count > 5000 THEN 'medium'
            ELSE 'low'
        END as optimization_priority,

        -- Estimated improvement potential
        CASE 
            WHEN qpa.avg_selectivity_ratio < 0.01 THEN '80-90% improvement expected'
            WHEN qpa.performance_category = 'very_slow' THEN '60-80% improvement expected'
            WHEN qpa.performance_category = 'slow' THEN '40-60% improvement expected'
            ELSE '20-40% improvement expected'
        END as estimated_improvement

    FROM query_performance_analysis qpa
    LEFT JOIN index_effectiveness_analysis iea ON qpa.collection_name = iea.collection_name
    WHERE qpa.needs_optimization = true
    GROUP BY 
        qpa.collection_name, qpa.query_pattern_type, qpa.execution_count,
        qpa.avg_execution_time_ms, qpa.avg_selectivity_ratio, qpa.performance_category,
        qpa.needs_optimization, qpa.total_execution_time_ms, qpa.indexes_used
)

SELECT 
    oo.collection_name,
    oo.query_pattern_type,
    oo.optimization_priority,
    oo.optimization_recommendation,

    -- Performance metrics
    oo.execution_count,
    ROUND(oo.avg_execution_time_ms, 2) as avg_execution_time_ms,
    ROUND(oo.performance_impact_seconds, 2) as total_impact_seconds,
    ROUND(oo.avg_selectivity_ratio * 100, 2) as selectivity_percent,

    -- Current state analysis
    oo.performance_category,
    oo.available_indexes,
    COALESCE(oo.collection_indexes, 'No indexes found') as current_indexes,
    ROUND(COALESCE(oo.avg_index_effectiveness, 0), 2) as avg_index_effectiveness,

    -- Optimization guidance
    oo.estimated_improvement,

    -- Specific recommendations based on analysis
    CASE oo.optimization_recommendation
        WHEN 'create_selective_index' THEN 
            'Create compound index on high-selectivity filter fields for collection: ' || oo.collection_name
        WHEN 'add_supporting_index' THEN 
            'Add index to eliminate collection scans in collection: ' || oo.collection_name
        WHEN 'replace_collection_scan' THEN 
            'Critical: Replace collection scan with indexed access in collection: ' || oo.collection_name
        WHEN 'comprehensive_optimization' THEN 
            'Comprehensive query and index optimization needed for collection: ' || oo.collection_name
        WHEN 'high_frequency_optimization' THEN 
            'Optimize high-frequency queries in collection: ' || oo.collection_name
        ELSE 'Continue monitoring performance trends'
    END as detailed_recommendation,

    -- Implementation complexity assessment
    CASE 
        WHEN oo.available_indexes = 0 THEN 'high_complexity'
        WHEN oo.avg_index_effectiveness < 1 THEN 'medium_complexity'
        ELSE 'low_complexity'
    END as implementation_complexity,

    -- Business impact estimation
    CASE oo.optimization_priority
        WHEN 'critical' THEN 'High business impact - immediate attention required'
        WHEN 'high' THEN 'Moderate business impact - optimize within 1 week'
        WHEN 'medium' THEN 'Low business impact - optimize within 1 month'
        ELSE 'Minimal business impact - optimize when convenient'
    END as business_impact_assessment,

    -- Resource requirements
    CASE 
        WHEN oo.optimization_recommendation IN ('create_selective_index', 'add_supporting_index') THEN 'Index creation: 5-30 minutes'
        WHEN oo.optimization_recommendation = 'comprehensive_optimization' THEN 'Full analysis: 2-8 hours'
        ELSE 'Monitoring: ongoing'
    END as estimated_effort

FROM optimization_opportunities oo
ORDER BY 
    CASE oo.optimization_priority 
        WHEN 'critical' THEN 1 
        WHEN 'high' THEN 2 
        WHEN 'medium' THEN 3 
        ELSE 4 
    END,
    oo.performance_impact_seconds DESC,
    oo.execution_count DESC;

-- Index usage and effectiveness analysis
WITH index_usage_trends AS (
    SELECT 
        collection_name,
        index_name,

        -- Usage trend analysis over time windows
        SUM(CASE WHEN analysis_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour' THEN usage_count ELSE 0 END) as usage_last_hour,
        SUM(CASE WHEN analysis_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours' THEN usage_count ELSE 0 END) as usage_last_24h,
        SUM(CASE WHEN analysis_timestamp >= CURRENT_TIMESTAMP - INTERVAL '7 days' THEN usage_count ELSE 0 END) as usage_last_7d,

        -- Size and storage trends
        AVG(index_size_bytes) as avg_index_size_bytes,
        MAX(index_size_bytes) as max_index_size_bytes,

        -- Usage efficiency trends
        AVG(CASE WHEN index_size_bytes > 0 AND usage_count > 0 THEN 
                usage_count / (index_size_bytes / 1024.0 / 1024.0)
            ELSE 0 
        END) as avg_usage_efficiency,

        -- Consistency analysis
        COUNT(DISTINCT DATE_TRUNC('day', analysis_timestamp)) as analysis_days,
        STDDEV(usage_count) as usage_variability,

        -- Most recent statistics
        MAX(analysis_timestamp) as last_analysis,
        MAX(last_used_timestamp) as most_recent_use

    FROM index_usage_stats
    WHERE analysis_timestamp >= CURRENT_TIMESTAMP - INTERVAL '7 days'
    GROUP BY collection_name, index_name
),

index_recommendations AS (
    SELECT 
        iut.*,

        -- Usage trend classification
        CASE 
            WHEN iut.usage_last_hour = 0 AND iut.usage_last_24h = 0 AND iut.usage_last_7d = 0 THEN 'completely_unused'
            WHEN iut.usage_last_hour = 0 AND iut.usage_last_24h = 0 AND iut.usage_last_7d > 0 THEN 'infrequently_used'
            WHEN iut.usage_last_hour = 0 AND iut.usage_last_24h > 0 THEN 'daily_usage'
            WHEN iut.usage_last_hour > 0 THEN 'active_usage'
            ELSE 'unknown_usage'
        END as usage_trend,

        -- Storage efficiency assessment
        CASE 
            WHEN iut.avg_index_size_bytes > 1024*1024*1024 AND iut.usage_last_7d < 100 THEN 'storage_inefficient'
            WHEN iut.avg_index_size_bytes > 100*1024*1024 AND iut.usage_last_7d < 10 THEN 'questionable_storage_usage'
            WHEN iut.avg_usage_efficiency > 10 THEN 'storage_efficient'
            ELSE 'acceptable_storage_usage'
        END as storage_efficiency,

        -- Recommendation generation
        CASE 
            WHEN iut.usage_last_7d = 0 AND iut.index_name != '_id_' THEN 'consider_dropping'
            WHEN iut.avg_index_size_bytes > 500*1024*1024 AND iut.usage_last_7d < 50 THEN 'evaluate_necessity'
            WHEN iut.usage_variability > iut.usage_last_7d * 0.8 THEN 'inconsistent_usage_investigate'
            WHEN iut.avg_usage_efficiency > 20 THEN 'high_value_maintain'
            WHEN iut.usage_last_hour > 100 THEN 'critical_index_monitor'
            ELSE 'continue_monitoring'
        END as recommendation,

        -- Impact assessment for potential changes
        CASE 
            WHEN iut.usage_last_hour > 0 THEN 'high_impact_if_removed'
            WHEN iut.usage_last_24h > 0 THEN 'medium_impact_if_removed'
            WHEN iut.usage_last_7d > 0 THEN 'low_impact_if_removed'
            ELSE 'no_impact_if_removed'
        END as removal_impact,

        -- Storage savings potential
        CASE 
            WHEN iut.avg_index_size_bytes > 0 THEN 
                ROUND(iut.avg_index_size_bytes / 1024.0 / 1024.0, 2)
            ELSE 0
        END as storage_savings_mb

    FROM index_usage_trends iut
),

collection_performance_summary AS (
    SELECT 
        collection_name,
        COUNT(*) as total_indexes,

        -- Usage distribution
        COUNT(*) FILTER (WHERE usage_trend = 'active_usage') as active_indexes,
        COUNT(*) FILTER (WHERE usage_trend = 'daily_usage') as daily_indexes,
        COUNT(*) FILTER (WHERE usage_trend = 'infrequently_used') as infrequent_indexes,
        COUNT(*) FILTER (WHERE usage_trend = 'completely_unused') as unused_indexes,

        -- Storage analysis
        SUM(avg_index_size_bytes) as total_index_storage_bytes,
        AVG(avg_usage_efficiency) as collection_avg_efficiency,

        -- Optimization potential
        COUNT(*) FILTER (WHERE recommendation = 'consider_dropping') as indexes_to_drop,
        COUNT(*) FILTER (WHERE recommendation = 'evaluate_necessity') as indexes_to_evaluate,
        SUM(CASE WHEN recommendation IN ('consider_dropping', 'evaluate_necessity') 
                 THEN storage_savings_mb ELSE 0 END) as potential_storage_savings_mb,

        -- Collection health assessment
        CASE 
            WHEN COUNT(*) FILTER (WHERE usage_trend = 'active_usage') = 0 THEN 'no_active_indexes'
            WHEN COUNT(*) FILTER (WHERE usage_trend = 'completely_unused') > COUNT(*) * 0.5 THEN 'many_unused_indexes'
            WHEN AVG(avg_usage_efficiency) < 1 THEN 'poor_index_efficiency'
            ELSE 'healthy_index_usage'
        END as collection_health

    FROM index_recommendations
    GROUP BY collection_name
)

SELECT 
    cps.collection_name,
    cps.total_indexes,
    cps.collection_health,

    -- Index usage distribution
    cps.active_indexes,
    cps.daily_indexes,
    cps.infrequent_indexes,
    cps.unused_indexes,

    -- Storage utilization
    ROUND(cps.total_index_storage_bytes / 1024.0 / 1024.0, 2) as total_storage_mb,
    ROUND(cps.collection_avg_efficiency, 2) as avg_efficiency_score,

    -- Optimization opportunities
    cps.indexes_to_drop,
    cps.indexes_to_evaluate, 
    ROUND(cps.potential_storage_savings_mb, 2) as potential_savings_mb,

    -- Optimization priority
    CASE 
        WHEN cps.collection_health = 'no_active_indexes' THEN 'critical_review_needed'
        WHEN cps.unused_indexes > 5 OR cps.potential_storage_savings_mb > 1000 THEN 'high_cleanup_priority'
        WHEN cps.collection_avg_efficiency < 2 THEN 'medium_optimization_priority'
        ELSE 'low_maintenance_priority'
    END as optimization_priority,

    -- Recommendations summary
    CASE cps.collection_health
        WHEN 'no_active_indexes' THEN 'URGENT: Collection has no actively used indexes - investigate query patterns'
        WHEN 'many_unused_indexes' THEN 'Multiple unused indexes detected - perform index cleanup'
        WHEN 'poor_index_efficiency' THEN 'Index usage is inefficient - review index design'
        ELSE 'Index usage appears healthy - continue monitoring'
    END as primary_recommendation,

    -- Storage efficiency assessment
    CASE 
        WHEN cps.potential_storage_savings_mb > 1000 THEN 
            'High storage optimization potential: ' || ROUND(cps.potential_storage_savings_mb, 0) || 'MB recoverable'
        WHEN cps.potential_storage_savings_mb > 100 THEN 
            'Moderate storage optimization: ' || ROUND(cps.potential_storage_savings_mb, 0) || 'MB recoverable'
        WHEN cps.potential_storage_savings_mb > 10 THEN 
            'Minor storage optimization: ' || ROUND(cps.potential_storage_savings_mb, 0) || 'MB recoverable'
        ELSE 'Minimal storage optimization potential'
    END as storage_optimization_summary,

    -- Specific next actions
    ARRAY[
        CASE WHEN cps.indexes_to_drop > 0 THEN 
            'Review and drop ' || cps.indexes_to_drop || ' unused indexes' END,
        CASE WHEN cps.indexes_to_evaluate > 0 THEN 
            'Evaluate necessity of ' || cps.indexes_to_evaluate || ' underutilized indexes' END,
        CASE WHEN cps.collection_avg_efficiency < 1 THEN 
            'Redesign indexes for better efficiency' END,
        CASE WHEN cps.active_indexes = 0 THEN 
            'Investigate why no indexes are actively used' END
    ]::TEXT[] as action_items

FROM collection_performance_summary cps
ORDER BY 
    CASE cps.collection_health 
        WHEN 'no_active_indexes' THEN 1 
        WHEN 'many_unused_indexes' THEN 2 
        WHEN 'poor_index_efficiency' THEN 3 
        ELSE 4 
    END,
    cps.potential_storage_savings_mb DESC,
    cps.total_indexes DESC;

-- Real-time query performance monitoring and alerting
CREATE VIEW real_time_performance_dashboard AS
WITH current_performance AS (
    SELECT 
        collection_name,
        query_pattern_type,

        -- Recent performance metrics (last hour)
        COUNT(*) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour') as queries_last_hour,
        AVG(execution_time_ms) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour') as avg_time_last_hour,
        MAX(execution_time_ms) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour') as max_time_last_hour,

        -- Performance trend comparison (current hour vs previous hour)
        AVG(execution_time_ms) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '2 hours'
                                              AND execution_timestamp < CURRENT_TIMESTAMP - INTERVAL '1 hour') as avg_time_prev_hour,

        -- Critical performance indicators
        COUNT(*) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour'
                               AND execution_time_ms > 5000) as critical_slow_queries,
        COUNT(*) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour'
                               AND execution_time_ms > 1000) as slow_queries,

        -- Resource utilization trends
        AVG(documents_examined) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour') as avg_docs_examined,
        AVG(documents_returned) FILTER (WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '1 hour') as avg_docs_returned,

        -- Most recent query information
        MAX(execution_timestamp) as last_execution,
        MAX(execution_time_ms) as recent_max_time

    FROM query_performance_log
    WHERE execution_timestamp >= CURRENT_TIMESTAMP - INTERVAL '2 hours'
    GROUP BY collection_name, query_pattern_type
),

performance_alerts AS (
    SELECT 
        cp.*,

        -- Performance trend analysis
        CASE 
            WHEN cp.avg_time_last_hour > cp.avg_time_prev_hour * 2 THEN 'degradation_alert'
            WHEN cp.avg_time_last_hour > 2000 THEN 'slow_performance_alert'
            WHEN cp.critical_slow_queries > 0 THEN 'critical_performance_alert'
            WHEN cp.queries_last_hour > 1000 AND cp.avg_time_last_hour > 500 THEN 'high_volume_slow_alert'
            ELSE 'normal'
        END as alert_level,

        -- Selectivity analysis
        CASE 
            WHEN cp.avg_docs_examined > 0 THEN cp.avg_docs_returned / cp.avg_docs_examined
            ELSE 1
        END as current_selectivity,

        -- Performance change calculation
        CASE 
            WHEN cp.avg_time_prev_hour > 0 THEN 
                ROUND(((cp.avg_time_last_hour - cp.avg_time_prev_hour) / cp.avg_time_prev_hour) * 100, 1)
            ELSE 0
        END as performance_change_percent,

        -- Alert priority
        CASE 
            WHEN cp.critical_slow_queries > 0 THEN 'critical'
            WHEN cp.avg_time_last_hour > cp.avg_time_prev_hour * 2 THEN 'high'
            WHEN cp.slow_queries > 10 THEN 'medium'
            ELSE 'low'
        END as alert_priority

    FROM current_performance cp
    WHERE cp.queries_last_hour > 0
)

SELECT 
    pa.collection_name,
    pa.query_pattern_type,
    pa.alert_level,
    pa.alert_priority,

    -- Current performance metrics
    pa.queries_last_hour,
    ROUND(pa.avg_time_last_hour, 2) as current_avg_time_ms,
    pa.max_time_last_hour,
    pa.recent_max_time,

    -- Performance comparison
    ROUND(COALESCE(pa.avg_time_prev_hour, 0), 2) as previous_avg_time_ms,
    pa.performance_change_percent || '%' as performance_change,

    -- Problem severity indicators
    pa.critical_slow_queries,
    pa.slow_queries,
    ROUND(pa.current_selectivity * 100, 2) as selectivity_percent,

    -- Alert messages
    CASE pa.alert_level
        WHEN 'critical_performance_alert' THEN 
            'CRITICAL: ' || pa.critical_slow_queries || ' queries exceeded 5 second threshold'
        WHEN 'degradation_alert' THEN 
            'WARNING: Performance degraded by ' || pa.performance_change_percent || '% from previous hour'
        WHEN 'slow_performance_alert' THEN 
            'WARNING: Average query time (' || ROUND(pa.avg_time_last_hour, 0) || 'ms) exceeds acceptable threshold'
        WHEN 'high_volume_slow_alert' THEN 
            'WARNING: High query volume (' || pa.queries_last_hour || ') with slow performance'
        ELSE 'No performance alerts'
    END as alert_message,

    -- Recommended actions
    CASE pa.alert_level
        WHEN 'critical_performance_alert' THEN 'Immediate investigation required - check for index issues or resource constraints'
        WHEN 'degradation_alert' THEN 'Investigate performance regression - check recent changes or resource utilization'
        WHEN 'slow_performance_alert' THEN 'Review query optimization opportunities and index effectiveness'
        WHEN 'high_volume_slow_alert' THEN 'Consider query optimization and capacity scaling'
        ELSE 'Continue monitoring'
    END as recommended_action,

    -- Urgency indicator
    CASE pa.alert_priority
        WHEN 'critical' THEN 'Immediate attention required (< 15 minutes)'
        WHEN 'high' THEN 'Urgent attention needed (< 1 hour)'
        WHEN 'medium' THEN 'Should be addressed within 4 hours'
        ELSE 'Monitor and address during normal maintenance'
    END as response_urgency,

    -- Last occurrence
    pa.last_execution,
    EXTRACT(MINUTES FROM (CURRENT_TIMESTAMP - pa.last_execution)) as minutes_since_last_query

FROM performance_alerts pa
WHERE pa.alert_level != 'normal'
ORDER BY 
    CASE pa.alert_priority 
        WHEN 'critical' THEN 1 
        WHEN 'high' THEN 2 
        WHEN 'medium' THEN 3 
        ELSE 4 
    END,
    pa.performance_change_percent DESC,
    pa.avg_time_last_hour DESC;

-- QueryLeaf provides comprehensive MongoDB performance optimization capabilities:
-- 1. Advanced query performance analysis with SQL-familiar syntax
-- 2. Comprehensive index usage monitoring and effectiveness analysis
-- 3. Real-time performance alerting and automated optimization recommendations
-- 4. Detailed execution plan analysis and optimization insights
-- 5. Index optimization strategies including compound index recommendations
-- 6. Performance trend analysis and predictive optimization
-- 7. Resource utilization monitoring and capacity planning
-- 8. Automated slow query detection and optimization guidance
-- 9. Enterprise-grade performance management with minimal configuration
-- 10. Production-ready monitoring and optimization automation

Best Practices for Production Performance Optimization

Index Strategy Design Principles

Essential principles for effective MongoDB index optimization deployment:

  1. Compound Index Design: Create efficient compound indexes following ESR rule (Equality, Sort, Range) for optimal query performance
  2. Index Usage Monitoring: Continuously monitor index usage patterns and effectiveness to identify optimization opportunities
  3. Query Pattern Analysis: Analyze query execution patterns to understand workload characteristics and optimization requirements
  4. Performance Testing: Implement comprehensive performance testing procedures for index changes and query optimizations
  5. Capacity Planning: Monitor query performance trends and resource utilization for proactive capacity management
  6. Automated Optimization: Establish automated performance monitoring and optimization recommendation systems

Enterprise Performance Management

Design performance optimization systems for enterprise-scale requirements:

  1. Real-Time Monitoring: Implement comprehensive real-time performance monitoring with intelligent alerting and automated responses
  2. Predictive Analytics: Use performance trend analysis and predictive modeling for proactive optimization and capacity planning
  3. Performance Governance: Establish performance standards, monitoring procedures, and optimization workflows
  4. Resource Optimization: Balance query performance with storage efficiency and maintenance overhead
  5. Compliance Integration: Ensure performance optimization procedures meet operational and compliance requirements
  6. Knowledge Management: Document optimization procedures, performance patterns, and best practices for operational excellence

Conclusion

MongoDB index optimization and query performance analysis provides comprehensive database tuning capabilities that enable applications to achieve optimal performance through intelligent indexing strategies, sophisticated query analysis, and automated optimization recommendations. The native performance analysis tools and integrated optimization guidance ensure that database operations maintain peak efficiency with minimal operational overhead.

Key MongoDB Performance Optimization benefits include:

  • Intelligent Analysis: Advanced query performance analysis with automated bottleneck identification and optimization recommendations
  • Index Optimization: Comprehensive index usage analysis with effectiveness assessment and automated cleanup suggestions
  • Real-Time Monitoring: Continuous performance monitoring with intelligent alerting and proactive optimization capabilities
  • Execution Plan Analysis: Detailed query execution plan analysis with optimization insights and improvement recommendations
  • Automated Recommendations: AI-powered optimization recommendations based on workload patterns and performance characteristics
  • SQL Accessibility: Familiar SQL-style performance operations through QueryLeaf for accessible database optimization

Whether you're optimizing high-traffic applications, managing large-scale data workloads, implementing performance monitoring systems, or maintaining enterprise database performance, MongoDB performance optimization with QueryLeaf's familiar SQL interface provides the foundation for sophisticated, scalable database tuning operations.

QueryLeaf Integration: QueryLeaf automatically translates SQL-style performance analysis operations into MongoDB's native profiling and indexing capabilities, making advanced performance optimization accessible to SQL-oriented database administrators. Complex index analysis, query optimization recommendations, and performance monitoring are seamlessly handled through familiar SQL constructs, enabling sophisticated database tuning without requiring deep MongoDB performance expertise.

The combination of MongoDB's robust performance analysis capabilities with SQL-style optimization operations makes it an ideal platform for applications requiring both sophisticated database performance management and familiar database administration patterns, ensuring your database operations can maintain optimal performance while scaling efficiently as workload complexity and data volume grow.

MongoDB Atlas Deployment Automation and Cloud Infrastructure: Advanced DevOps Integration and Infrastructure-as-Code for Scalable Database Operations

Modern cloud-native applications require sophisticated database infrastructure that can automatically scale, self-heal, and integrate seamlessly with DevOps workflows and CI/CD pipelines. Traditional database deployment approaches require manual configuration, complex scaling procedures, and extensive operational overhead to maintain production-ready database infrastructure. Effective cloud database management demands automated provisioning, intelligent resource optimization, and integrated monitoring capabilities.

MongoDB Atlas provides comprehensive cloud database automation through infrastructure-as-code integration, automated scaling policies, and advanced DevOps toolchain compatibility that enables sophisticated database operations with minimal manual intervention. Unlike traditional database hosting that requires complex server management and manual optimization, Atlas integrates database infrastructure directly into modern DevOps workflows with automated provisioning, intelligent scaling, and built-in operational excellence.

The Traditional Cloud Database Deployment Challenge

Conventional approaches to cloud database infrastructure management face significant operational complexity:

-- Traditional cloud database management - manual setup with extensive operational overhead

-- Basic database server provisioning tracking (manual process)
CREATE TABLE database_servers (
    server_id SERIAL PRIMARY KEY,
    server_name VARCHAR(255) NOT NULL,
    cloud_provider VARCHAR(100) NOT NULL,
    instance_type VARCHAR(100) NOT NULL,
    region VARCHAR(100) NOT NULL,

    -- Manual resource configuration
    cpu_cores INTEGER,
    memory_gb INTEGER,
    storage_gb INTEGER,
    iops INTEGER,

    -- Network configuration (manual setup)
    vpc_id VARCHAR(100),
    subnet_id VARCHAR(100),
    security_group_ids TEXT[],
    public_ip INET,
    private_ip INET,

    -- Database configuration
    database_engine VARCHAR(50) DEFAULT 'postgresql',
    engine_version VARCHAR(20),
    port INTEGER DEFAULT 5432,

    -- Status tracking
    server_status VARCHAR(50) DEFAULT 'creating',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    provisioned_by VARCHAR(100),

    -- Cost tracking (manual)
    estimated_monthly_cost DECIMAL(10,2),
    actual_monthly_cost DECIMAL(10,2)
);

-- Database deployment tracking (complex manual process)
CREATE TABLE database_deployments (
    deployment_id SERIAL PRIMARY KEY,
    deployment_name VARCHAR(255) NOT NULL,
    server_id INTEGER REFERENCES database_servers(server_id),
    environment VARCHAR(100) NOT NULL,

    -- Deployment configuration (manual setup)
    database_name VARCHAR(100) NOT NULL,
    schema_version VARCHAR(50),
    application_version VARCHAR(50),

    -- Manual backup configuration
    backup_enabled BOOLEAN DEFAULT true,
    backup_schedule VARCHAR(100), -- Cron format
    backup_retention_days INTEGER DEFAULT 30,
    backup_storage_location VARCHAR(200),

    -- Scaling configuration (manual)
    enable_auto_scaling BOOLEAN DEFAULT false,
    min_capacity INTEGER,
    max_capacity INTEGER,
    target_cpu_utilization DECIMAL(5,2) DEFAULT 70.0,
    target_memory_utilization DECIMAL(5,2) DEFAULT 80.0,

    -- Monitoring setup (manual integration)
    monitoring_enabled BOOLEAN DEFAULT false,
    monitoring_tools TEXT[],
    alert_endpoints TEXT[],

    -- Deployment metadata
    deployment_status VARCHAR(50) DEFAULT 'pending',
    deployed_at TIMESTAMP,
    deployed_by VARCHAR(100),
    deployment_duration_seconds INTEGER,

    -- Configuration validation
    config_validation_status VARCHAR(50),
    validation_errors TEXT[]
);

-- Manual scaling operation tracking
CREATE TABLE scaling_operations (
    scaling_id SERIAL PRIMARY KEY,
    server_id INTEGER REFERENCES database_servers(server_id),
    scaling_trigger VARCHAR(100),

    -- Resource changes (manual calculation)
    previous_cpu_cores INTEGER,
    new_cpu_cores INTEGER,
    previous_memory_gb INTEGER,
    new_memory_gb INTEGER,
    previous_storage_gb INTEGER,
    new_storage_gb INTEGER,

    -- Scaling metrics
    trigger_metric VARCHAR(100),
    trigger_threshold DECIMAL(10,2),
    current_utilization DECIMAL(10,2),

    -- Scaling execution
    scaling_status VARCHAR(50) DEFAULT 'pending',
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    downtime_seconds INTEGER,

    -- Cost impact
    previous_hourly_cost DECIMAL(10,4),
    new_hourly_cost DECIMAL(10,4),
    cost_impact_monthly DECIMAL(10,2)
);

-- Basic monitoring and alerting (very limited automation)
CREATE OR REPLACE FUNCTION check_database_health()
RETURNS TABLE (
    server_id INTEGER,
    health_status VARCHAR(50),
    cpu_utilization DECIMAL(5,2),
    memory_utilization DECIMAL(5,2),
    disk_utilization DECIMAL(5,2),
    connection_count INTEGER,
    active_queries INTEGER,
    replication_lag_seconds INTEGER,
    backup_status VARCHAR(50),
    alert_level VARCHAR(20),
    recommendations TEXT[]
) AS $$
BEGIN
    -- This would be a very simplified health check
    -- Real implementation would require complex monitoring integration

    RETURN QUERY
    SELECT 
        ds.server_id,

        -- Basic status assessment (very limited)
        CASE 
            WHEN ds.server_status != 'running' THEN 'unhealthy'
            ELSE 'healthy'
        END as health_status,

        -- Simulated metrics (would need real monitoring integration)
        (random() * 100)::DECIMAL(5,2) as cpu_utilization,
        (random() * 100)::DECIMAL(5,2) as memory_utilization,
        (random() * 100)::DECIMAL(5,2) as disk_utilization,
        (random() * 100)::INTEGER as connection_count,
        (random() * 20)::INTEGER as active_queries,
        (random() * 10)::INTEGER as replication_lag_seconds,

        -- Backup status (manual tracking)
        'unknown' as backup_status,

        -- Alert level determination
        CASE 
            WHEN ds.server_status != 'running' THEN 'critical'
            WHEN random() > 0.9 THEN 'warning'
            ELSE 'info'
        END as alert_level,

        -- Basic recommendations (very limited)
        ARRAY[
            CASE WHEN random() > 0.8 THEN 'Consider scaling up CPU resources' END,
            CASE WHEN random() > 0.7 THEN 'Review backup configuration' END,
            CASE WHEN random() > 0.6 THEN 'Monitor connection pool usage' END
        ]::TEXT[] as recommendations

    FROM database_servers ds
    WHERE ds.server_status = 'running';
END;
$$ LANGUAGE plpgsql;

-- Manual deployment automation attempt (very basic)
CREATE OR REPLACE FUNCTION deploy_database_environment(
    deployment_name_param VARCHAR(255),
    environment_param VARCHAR(100),
    instance_type_param VARCHAR(100),
    database_name_param VARCHAR(100)
) RETURNS TABLE (
    deployment_success BOOLEAN,
    deployment_id INTEGER,
    server_id INTEGER,
    deployment_time_seconds INTEGER,
    error_message TEXT
) AS $$
DECLARE
    new_deployment_id INTEGER;
    new_server_id INTEGER;
    deployment_start TIMESTAMP;
    deployment_end TIMESTAMP;
    deployment_error TEXT := '';
    deployment_result BOOLEAN := true;
BEGIN
    deployment_start := clock_timestamp();

    BEGIN
        -- Step 1: Create database server record (manual provisioning simulation)
        INSERT INTO database_servers (
            server_name,
            cloud_provider,
            instance_type,
            region,
            cpu_cores,
            memory_gb,
            storage_gb,
            server_status,
            provisioned_by
        )
        VALUES (
            deployment_name_param || '_' || environment_param,
            'manual_cloud_provider',
            instance_type_param,
            'us-east-1',
            -- Static resource allocation (no optimization)
            CASE instance_type_param
                WHEN 't3.micro' THEN 1
                WHEN 't3.small' THEN 2
                WHEN 't3.medium' THEN 2
                ELSE 4
            END,
            CASE instance_type_param
                WHEN 't3.micro' THEN 1
                WHEN 't3.small' THEN 2
                WHEN 't3.medium' THEN 4
                ELSE 8
            END,
            100, -- Fixed storage
            'creating',
            current_user
        )
        RETURNING server_id INTO new_server_id;

        -- Simulate provisioning time
        PERFORM pg_sleep(2);

        -- Update server status
        UPDATE database_servers 
        SET server_status = 'running', last_updated = clock_timestamp()
        WHERE server_id = new_server_id;

        -- Step 2: Create deployment record
        INSERT INTO database_deployments (
            deployment_name,
            server_id,
            environment,
            database_name,
            deployment_status,
            deployed_by
        )
        VALUES (
            deployment_name_param,
            new_server_id,
            environment_param,
            database_name_param,
            'creating',
            current_user
        )
        RETURNING deployment_id INTO new_deployment_id;

        -- Simulate deployment process
        PERFORM pg_sleep(1);

        -- Update deployment status
        UPDATE database_deployments 
        SET deployment_status = 'completed',
            deployed_at = clock_timestamp()
        WHERE deployment_id = new_deployment_id;

    EXCEPTION WHEN OTHERS THEN
        deployment_result := false;
        deployment_error := SQLERRM;

        -- Cleanup on failure
        IF new_server_id IS NOT NULL THEN
            UPDATE database_servers 
            SET server_status = 'failed'
            WHERE server_id = new_server_id;
        END IF;

        IF new_deployment_id IS NOT NULL THEN
            UPDATE database_deployments 
            SET deployment_status = 'failed'
            WHERE deployment_id = new_deployment_id;
        END IF;
    END;

    deployment_end := clock_timestamp();

    RETURN QUERY SELECT 
        deployment_result,
        new_deployment_id,
        new_server_id,
        EXTRACT(SECONDS FROM deployment_end - deployment_start)::INTEGER,
        deployment_error;
END;
$$ LANGUAGE plpgsql;

-- Basic infrastructure monitoring query (very limited capabilities)
WITH server_utilization AS (
    SELECT 
        ds.server_id,
        ds.server_name,
        ds.instance_type,
        ds.cpu_cores,
        ds.memory_gb,
        ds.storage_gb,
        ds.server_status,
        ds.estimated_monthly_cost,

        -- Simulated current utilization (would need real monitoring)
        (random() * 100)::DECIMAL(5,2) as current_cpu_percent,
        (random() * 100)::DECIMAL(5,2) as current_memory_percent,
        (random() * 100)::DECIMAL(5,2) as current_storage_percent,

        -- Basic scaling recommendations (very limited logic)
        CASE 
            WHEN random() > 0.8 THEN 'scale_up'
            WHEN random() < 0.2 THEN 'scale_down'
            ELSE 'no_action'
        END as scaling_recommendation

    FROM database_servers ds
    WHERE ds.server_status = 'running'
),

cost_analysis AS (
    SELECT 
        su.*,
        dd.environment,

        -- Basic cost optimization suggestions (manual analysis)
        CASE 
            WHEN su.current_cpu_percent < 30 AND su.current_memory_percent < 30 THEN 'overprovisioned'
            WHEN su.current_cpu_percent > 80 OR su.current_memory_percent > 80 THEN 'underprovisioned'
            ELSE 'appropriately_sized'
        END as resource_sizing,

        -- Simple cost projection
        su.estimated_monthly_cost * 
        CASE su.scaling_recommendation
            WHEN 'scale_up' THEN 1.5
            WHEN 'scale_down' THEN 0.7
            ELSE 1.0
        END as projected_monthly_cost

    FROM server_utilization su
    JOIN database_deployments dd ON su.server_id = dd.server_id
)

SELECT 
    ca.server_name,
    ca.environment,
    ca.instance_type,
    ca.server_status,

    -- Resource utilization
    ca.current_cpu_percent,
    ca.current_memory_percent,
    ca.current_storage_percent,

    -- Scaling analysis
    ca.scaling_recommendation,
    ca.resource_sizing,

    -- Cost analysis
    ca.estimated_monthly_cost,
    ca.projected_monthly_cost,
    ROUND((ca.projected_monthly_cost - ca.estimated_monthly_cost), 2) as monthly_cost_impact,

    -- Basic recommendations
    CASE 
        WHEN ca.resource_sizing = 'overprovisioned' THEN 'Consider downsizing to reduce costs'
        WHEN ca.resource_sizing = 'underprovisioned' THEN 'Scale up to improve performance'
        WHEN ca.current_storage_percent > 85 THEN 'Increase storage capacity soon'
        ELSE 'Monitor current resource usage'
    END as operational_recommendation

FROM cost_analysis ca
ORDER BY ca.estimated_monthly_cost DESC;

-- Problems with traditional cloud database deployment:
-- 1. Manual provisioning with no infrastructure-as-code integration
-- 2. Limited auto-scaling capabilities requiring manual intervention
-- 3. Basic monitoring with no intelligent alerting or remediation
-- 4. Complex backup and disaster recovery configuration
-- 5. No built-in security best practices or compliance features
-- 6. Manual cost optimization requiring constant monitoring
-- 7. Limited integration with CI/CD pipelines and DevOps workflows
-- 8. No automatic patching or maintenance scheduling
-- 9. Complex networking and security group management
-- 10. Basic performance optimization requiring database expertise

MongoDB Atlas provides comprehensive cloud database automation with advanced DevOps integration:

// MongoDB Atlas Advanced Deployment Automation and Cloud Infrastructure Management
const { MongoClient } = require('mongodb');
const axios = require('axios');

// Comprehensive MongoDB Atlas Infrastructure Manager
class AdvancedAtlasInfrastructureManager {
  constructor(atlasConfig = {}) {
    // Atlas API configuration
    this.atlasConfig = {
      publicKey: atlasConfig.publicKey,
      privateKey: atlasConfig.privateKey,
      baseURL: atlasConfig.baseURL || 'https://cloud.mongodb.com/api/atlas/v1.0',

      // Organization and project configuration
      organizationId: atlasConfig.organizationId,
      projectId: atlasConfig.projectId,

      // Infrastructure automation settings
      enableAutomatedDeployment: atlasConfig.enableAutomatedDeployment !== false,
      enableInfrastructureAsCode: atlasConfig.enableInfrastructureAsCode || false,
      enableAutomatedScaling: atlasConfig.enableAutomatedScaling !== false,

      // DevOps integration
      cicdIntegration: atlasConfig.cicdIntegration || false,
      terraformIntegration: atlasConfig.terraformIntegration || false,
      kubernetesIntegration: atlasConfig.kubernetesIntegration || false,

      // Monitoring and alerting
      enableAdvancedMonitoring: atlasConfig.enableAdvancedMonitoring !== false,
      enableAutomatedAlerting: atlasConfig.enableAutomatedAlerting !== false,
      enablePerformanceAdvisor: atlasConfig.enablePerformanceAdvisor !== false,

      // Security and compliance
      enableAdvancedSecurity: atlasConfig.enableAdvancedSecurity !== false,
      enableEncryptionAtRest: atlasConfig.enableEncryptionAtRest !== false,
      enableNetworkSecurity: atlasConfig.enableNetworkSecurity !== false,

      // Backup and disaster recovery
      enableContinuousBackup: atlasConfig.enableContinuousBackup !== false,
      enableCrossRegionBackup: atlasConfig.enableCrossRegionBackup || false,
      backupRetentionDays: atlasConfig.backupRetentionDays || 30,

      // Cost optimization
      enableCostOptimization: atlasConfig.enableCostOptimization || false,
      enableAutoArchiving: atlasConfig.enableAutoArchiving || false,
      costBudgetAlerts: atlasConfig.costBudgetAlerts || []
    };

    // Infrastructure state management
    this.clusters = new Map();
    this.deployments = new Map();
    this.scalingOperations = new Map();
    this.monitoringAlerts = new Map();

    // DevOps integration state
    this.cicdPipelines = new Map();
    this.infrastructureTemplates = new Map();

    // Performance and cost tracking
    this.performanceMetrics = {
      totalClusters: 0,
      averageResponseTime: 0,
      totalMonthlySpend: 0,
      costPerOperation: 0
    };

    this.initializeAtlasInfrastructure();
  }

  async initializeAtlasInfrastructure() {
    console.log('Initializing MongoDB Atlas infrastructure management...');

    try {
      // Validate Atlas API credentials
      await this.validateAtlasCredentials();

      // Initialize infrastructure automation
      if (this.atlasConfig.enableAutomatedDeployment) {
        await this.setupAutomatedDeployment();
      }

      // Setup infrastructure-as-code integration
      if (this.atlasConfig.enableInfrastructureAsCode) {
        await this.setupInfrastructureAsCode();
      }

      // Initialize monitoring and alerting
      if (this.atlasConfig.enableAdvancedMonitoring) {
        await this.setupAdvancedMonitoring();
      }

      // Setup DevOps integrations
      if (this.atlasConfig.cicdIntegration) {
        await this.setupCICDIntegration();
      }

      console.log('Atlas infrastructure management initialized successfully');

    } catch (error) {
      console.error('Error initializing Atlas infrastructure:', error);
      throw error;
    }
  }

  async deployCluster(clusterConfig, deploymentOptions = {}) {
    console.log(`Deploying Atlas cluster: ${clusterConfig.name}`);

    try {
      const deployment = {
        deploymentId: this.generateDeploymentId(),
        clusterName: clusterConfig.name,

        // Cluster specification
        clusterSpec: {
          name: clusterConfig.name,
          clusterType: clusterConfig.clusterType || 'REPLICASET',
          mongoDBVersion: clusterConfig.mongoDBVersion || '7.0',

          // Provider configuration
          providerSettings: {
            providerName: clusterConfig.providerName || 'AWS',
            regionName: clusterConfig.regionName || 'US_EAST_1',
            instanceSizeName: clusterConfig.instanceSizeName || 'M30',

            // Advanced configuration
            diskIOPS: clusterConfig.diskIOPS,
            encryptEBSVolume: this.atlasConfig.enableEncryptionAtRest,
            volumeType: clusterConfig.volumeType || 'STANDARD'
          },

          // Replication configuration
          replicationSpecs: clusterConfig.replicationSpecs || [
            {
              numShards: 1,
              regionsConfig: {
                [clusterConfig.regionName || 'US_EAST_1']: {
                  electableNodes: 3,
                  priority: 7,
                  readOnlyNodes: 0
                }
              }
            }
          ],

          // Backup configuration
          backupEnabled: this.atlasConfig.enableContinuousBackup,
          providerBackupEnabled: this.atlasConfig.enableCrossRegionBackup,

          // Auto-scaling configuration
          autoScaling: {
            diskGBEnabled: this.atlasConfig.enableAutomatedScaling,
            compute: {
              enabled: this.atlasConfig.enableAutomatedScaling,
              scaleDownEnabled: true,
              minInstanceSize: clusterConfig.minInstanceSize || 'M10',
              maxInstanceSize: clusterConfig.maxInstanceSize || 'M80'
            }
          }
        },

        // Deployment configuration
        deploymentConfig: {
          environment: deploymentOptions.environment || 'production',
          deploymentType: deploymentOptions.deploymentType || 'standard',
          rolloutStrategy: deploymentOptions.rolloutStrategy || 'immediate',

          // Network security
          networkAccessList: clusterConfig.networkAccessList || [],

          // Database users
          databaseUsers: clusterConfig.databaseUsers || [],

          // Advanced security
          ldapConfiguration: clusterConfig.ldapConfiguration,
          encryptionAtRestProvider: clusterConfig.encryptionAtRestProvider
        },

        // Deployment metadata
        startTime: new Date(),
        status: 'creating',
        createdBy: deploymentOptions.createdBy || 'system'
      };

      // Store deployment state
      this.deployments.set(deployment.deploymentId, deployment);

      // Execute Atlas cluster creation
      const clusterResponse = await this.createAtlasCluster(deployment.clusterSpec);

      // Setup monitoring and alerting
      if (this.atlasConfig.enableAdvancedMonitoring) {
        await this.setupClusterMonitoring(clusterResponse.clusterId, deployment);
      }

      // Configure network security
      await this.configureNetworkSecurity(clusterResponse.clusterId, deployment.deploymentConfig);

      // Create database users
      await this.createDatabaseUsers(clusterResponse.clusterId, deployment.deploymentConfig.databaseUsers);

      // Wait for cluster to be ready
      const clusterStatus = await this.waitForClusterReady(clusterResponse.clusterId);

      // Update deployment status
      deployment.status = 'completed';
      deployment.endTime = new Date();
      deployment.clusterId = clusterResponse.clusterId;
      deployment.connectionString = clusterStatus.connectionString;

      // Store cluster information
      this.clusters.set(clusterResponse.clusterId, {
        clusterId: clusterResponse.clusterId,
        deployment: deployment,
        specification: deployment.clusterSpec,
        status: clusterStatus,
        createdAt: deployment.startTime
      });

      // Update performance metrics
      this.updateInfrastructureMetrics(deployment);

      console.log(`Cluster deployed successfully: ${clusterConfig.name} (${clusterResponse.clusterId})`);

      return {
        success: true,
        deploymentId: deployment.deploymentId,
        clusterId: clusterResponse.clusterId,
        clusterName: clusterConfig.name,
        connectionString: clusterStatus.connectionString,

        // Deployment details
        deploymentTime: deployment.endTime.getTime() - deployment.startTime.getTime(),
        environment: deployment.deploymentConfig.environment,
        configuration: deployment.clusterSpec,

        // Monitoring and security
        monitoringEnabled: this.atlasConfig.enableAdvancedMonitoring,
        securityEnabled: this.atlasConfig.enableAdvancedSecurity,
        backupEnabled: deployment.clusterSpec.backupEnabled
      };

    } catch (error) {
      console.error(`Error deploying cluster '${clusterConfig.name}':`, error);

      // Update deployment status
      const deployment = this.deployments.get(this.generateDeploymentId());
      if (deployment) {
        deployment.status = 'failed';
        deployment.error = error.message;
        deployment.endTime = new Date();
      }

      return {
        success: false,
        error: error.message,
        clusterName: clusterConfig.name
      };
    }
  }

  async createAtlasCluster(clusterSpec) {
    console.log(`Creating Atlas cluster via API: ${clusterSpec.name}`);

    try {
      const response = await this.atlasAPIRequest('POST', `/groups/${this.atlasConfig.projectId}/clusters`, clusterSpec);

      return {
        clusterId: response.id,
        name: response.name,
        stateName: response.stateName,
        createDate: response.createDate
      };

    } catch (error) {
      console.error('Error creating Atlas cluster:', error);
      throw error;
    }
  }

  async setupAutomatedScaling(clusterId, scalingConfig) {
    console.log(`Setting up automated scaling for cluster: ${clusterId}`);

    try {
      const scalingConfiguration = {
        clusterId: clusterId,

        // Compute scaling configuration
        computeScaling: {
          enabled: scalingConfig.computeScaling !== false,
          scaleDownEnabled: scalingConfig.scaleDownEnabled !== false,

          // Instance size limits
          minInstanceSize: scalingConfig.minInstanceSize || 'M10',
          maxInstanceSize: scalingConfig.maxInstanceSize || 'M80',

          // Scaling triggers
          targetCPUUtilization: scalingConfig.targetCPUUtilization || 75,
          targetMemoryUtilization: scalingConfig.targetMemoryUtilization || 80,

          // Scaling behavior
          scaleUpPolicy: {
            cooldownMinutes: scalingConfig.scaleUpCooldown || 15,
            incrementPercent: scalingConfig.scaleUpIncrement || 100,
            units: 'INSTANCE_SIZE'
          },
          scaleDownPolicy: {
            cooldownMinutes: scalingConfig.scaleDownCooldown || 30,
            decrementPercent: scalingConfig.scaleDownDecrement || 50,
            units: 'INSTANCE_SIZE'
          }
        },

        // Storage scaling configuration
        storageScaling: {
          enabled: scalingConfig.storageScaling !== false,

          // Storage scaling triggers
          targetStorageUtilization: scalingConfig.targetStorageUtilization || 85,
          incrementGigabytes: scalingConfig.storageIncrement || 10,
          maxStorageGigabytes: scalingConfig.maxStorage || 4096
        },

        // Advanced scaling features
        advancedScaling: {
          enablePredictiveScaling: scalingConfig.enablePredictiveScaling || false,
          enableScheduledScaling: scalingConfig.enableScheduledScaling || false,
          scheduledScalingEvents: scalingConfig.scheduledScalingEvents || []
        }
      };

      // Configure compute auto-scaling
      if (scalingConfiguration.computeScaling.enabled) {
        await this.configureComputeScaling(clusterId, scalingConfiguration.computeScaling);
      }

      // Configure storage auto-scaling
      if (scalingConfiguration.storageScaling.enabled) {
        await this.configureStorageScaling(clusterId, scalingConfiguration.storageScaling);
      }

      // Store scaling configuration
      this.scalingOperations.set(clusterId, scalingConfiguration);

      return {
        success: true,
        clusterId: clusterId,
        scalingConfiguration: scalingConfiguration
      };

    } catch (error) {
      console.error(`Error setting up automated scaling for cluster ${clusterId}:`, error);
      return {
        success: false,
        error: error.message,
        clusterId: clusterId
      };
    }
  }

  async setupAdvancedMonitoring(clusterId, monitoringConfig = {}) {
    console.log(`Setting up advanced monitoring for cluster: ${clusterId}`);

    try {
      const monitoringConfiguration = {
        clusterId: clusterId,

        // Performance monitoring
        performanceMonitoring: {
          enabled: monitoringConfig.performanceMonitoring !== false,

          // Metrics collection
          collectDetailedMetrics: true,
          metricsRetentionDays: monitoringConfig.metricsRetentionDays || 30,

          // Performance insights
          enableSlowQueryAnalysis: true,
          enableIndexSuggestions: true,
          enableQueryOptimization: true,

          // Real-time monitoring
          enableRealTimeAlerts: true,
          alertLatencyThresholds: {
            warning: monitoringConfig.warningLatency || 1000,
            critical: monitoringConfig.criticalLatency || 5000
          }
        },

        // Infrastructure monitoring
        infrastructureMonitoring: {
          enabled: monitoringConfig.infrastructureMonitoring !== false,

          // Resource monitoring
          monitorCPUUtilization: true,
          monitorMemoryUtilization: true,
          monitorStorageUtilization: true,
          monitorNetworkUtilization: true,

          // Capacity planning
          enableCapacityForecasting: true,
          forecastingHorizonDays: monitoringConfig.forecastingHorizon || 30,

          // Health checks
          enableHealthChecks: true,
          healthCheckIntervalMinutes: monitoringConfig.healthCheckInterval || 5
        },

        // Application monitoring
        applicationMonitoring: {
          enabled: monitoringConfig.applicationMonitoring !== false,

          // Connection monitoring
          monitorConnectionUsage: true,
          connectionPoolAnalysis: true,

          // Query monitoring
          slowQueryThresholdMs: monitoringConfig.slowQueryThreshold || 1000,
          enableQueryProfiling: true,
          profileSampleRate: monitoringConfig.profileSampleRate || 0.1,

          // Error monitoring
          enableErrorTracking: true,
          errorAlertThreshold: monitoringConfig.errorAlertThreshold || 10
        },

        // Security monitoring
        securityMonitoring: {
          enabled: this.atlasConfig.enableAdvancedSecurity,

          // Access monitoring
          monitorDatabaseAccess: true,
          unusualAccessAlerts: true,

          // Authentication monitoring
          authenticationFailureAlerts: true,
          multipleFailedAttemptsThreshold: 5,

          // Data access monitoring
          sensitiveDataAccessMonitoring: true,
          dataExportMonitoring: true
        }
      };

      // Setup performance monitoring
      if (monitoringConfiguration.performanceMonitoring.enabled) {
        await this.configurePerformanceMonitoring(clusterId, monitoringConfiguration.performanceMonitoring);
      }

      // Setup infrastructure monitoring
      if (monitoringConfiguration.infrastructureMonitoring.enabled) {
        await this.configureInfrastructureMonitoring(clusterId, monitoringConfiguration.infrastructureMonitoring);
      }

      // Setup application monitoring
      if (monitoringConfiguration.applicationMonitoring.enabled) {
        await this.configureApplicationMonitoring(clusterId, monitoringConfiguration.applicationMonitoring);
      }

      // Setup security monitoring
      if (monitoringConfiguration.securityMonitoring.enabled) {
        await this.configureSecurityMonitoring(clusterId, monitoringConfiguration.securityMonitoring);
      }

      // Store monitoring configuration
      this.monitoringAlerts.set(clusterId, monitoringConfiguration);

      return {
        success: true,
        clusterId: clusterId,
        monitoringConfiguration: monitoringConfiguration
      };

    } catch (error) {
      console.error(`Error setting up monitoring for cluster ${clusterId}:`, error);
      return {
        success: false,
        error: error.message,
        clusterId: clusterId
      };
    }
  }

  async setupInfrastructureAsCode(templateConfig = {}) {
    console.log('Setting up infrastructure-as-code integration...');

    try {
      const infrastructureTemplate = {
        templateId: this.generateTemplateId(),
        templateName: templateConfig.name || 'mongodb-atlas-infrastructure',
        templateType: templateConfig.type || 'terraform',

        // Template configuration
        templateConfiguration: {
          // Provider configuration
          provider: templateConfig.provider || 'terraform',
          version: templateConfig.version || '1.0',

          // Atlas provider settings
          atlasProvider: {
            publicKey: '${var.atlas_public_key}',
            privateKey: '${var.atlas_private_key}',
            baseURL: this.atlasConfig.baseURL
          },

          // Infrastructure resources
          resources: {
            // Project resource
            project: {
              name: '${var.project_name}',
              orgId: this.atlasConfig.organizationId,

              // Project configuration
              isCollectingBugs: false,
              isDataExplorerEnabled: true,
              isPerformanceAdvisorEnabled: true,
              isRealtimePerformancePanelEnabled: true,
              isSchemaAdvisorEnabled: true
            },

            // Cluster resources
            clusters: templateConfig.clusters || [],

            // Database user resources
            databaseUsers: templateConfig.databaseUsers || [],

            // Network access rules
            networkAccessList: templateConfig.networkAccessList || [],

            // Alert configurations
            alertConfigurations: templateConfig.alertConfigurations || []
          },

          // Variables
          variables: {
            atlas_public_key: {
              description: 'MongoDB Atlas API Public Key',
              type: 'string',
              sensitive: false
            },
            atlas_private_key: {
              description: 'MongoDB Atlas API Private Key',
              type: 'string',
              sensitive: true
            },
            project_name: {
              description: 'Atlas Project Name',
              type: 'string',
              default: 'default-project'
            },
            environment: {
              description: 'Deployment Environment',
              type: 'string',
              default: 'development'
            }
          },

          // Outputs
          outputs: {
            cluster_connection_strings: {
              description: 'Atlas Cluster Connection Strings',
              value: '${tomap({ for k, cluster in mongodbatlas_cluster.clusters : k => cluster.connection_strings[0].standard_srv })}'
            },
            cluster_ids: {
              description: 'Atlas Cluster IDs',
              value: '${tomap({ for k, cluster in mongodbatlas_cluster.clusters : k => cluster.cluster_id })}'
            },
            project_id: {
              description: 'Atlas Project ID',
              value: '${mongodbatlas_project.project.id}'
            }
          }
        },

        // CI/CD integration
        cicdIntegration: {
          enabled: templateConfig.cicdIntegration || false,

          // Pipeline configuration
          pipeline: {
            stages: ['validate', 'plan', 'apply'],
            approvalRequired: templateConfig.requireApproval !== false,

            // Environment promotion
            environments: ['development', 'staging', 'production'],
            promotionStrategy: templateConfig.promotionStrategy || 'manual'
          },

          // Integration settings
          integrations: {
            github: templateConfig.githubIntegration || false,
            jenkins: templateConfig.jenkinsIntegration || false,
            gitlab: templateConfig.gitlabIntegration || false,
            azureDevOps: templateConfig.azureDevOpsIntegration || false
          }
        }
      };

      // Generate Terraform configuration
      const terraformConfig = this.generateTerraformConfig(infrastructureTemplate);

      // Generate CI/CD pipeline configuration
      const pipelineConfig = this.generatePipelineConfig(infrastructureTemplate);

      // Store template configuration
      this.infrastructureTemplates.set(infrastructureTemplate.templateId, infrastructureTemplate);

      return {
        success: true,
        templateId: infrastructureTemplate.templateId,
        templateConfiguration: infrastructureTemplate,
        terraformConfig: terraformConfig,
        pipelineConfig: pipelineConfig
      };

    } catch (error) {
      console.error('Error setting up infrastructure-as-code:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async performCostOptimization(clusterId, optimizationOptions = {}) {
    console.log(`Performing cost optimization for cluster: ${clusterId}`);

    try {
      const cluster = this.clusters.get(clusterId);
      if (!cluster) {
        throw new Error(`Cluster not found: ${clusterId}`);
      }

      // Collect performance and utilization metrics
      const performanceMetrics = await this.collectPerformanceMetrics(clusterId);
      const utilizationMetrics = await this.collectUtilizationMetrics(clusterId);
      const costMetrics = await this.collectCostMetrics(clusterId);

      // Analyze optimization opportunities
      const optimizationAnalysis = {
        clusterId: clusterId,
        analysisTime: new Date(),

        // Performance analysis
        performanceAnalysis: {
          averageResponseTime: performanceMetrics.averageResponseTime,
          peakResponseTime: performanceMetrics.peakResponseTime,
          queryThroughput: performanceMetrics.queryThroughput,
          resourceBottlenecks: performanceMetrics.bottlenecks
        },

        // Utilization analysis
        utilizationAnalysis: {
          cpuUtilization: {
            average: utilizationMetrics.cpu.average,
            peak: utilizationMetrics.cpu.peak,
            recommendation: this.generateCPURecommendation(utilizationMetrics.cpu)
          },
          memoryUtilization: {
            average: utilizationMetrics.memory.average,
            peak: utilizationMetrics.memory.peak,
            recommendation: this.generateMemoryRecommendation(utilizationMetrics.memory)
          },
          storageUtilization: {
            used: utilizationMetrics.storage.used,
            available: utilizationMetrics.storage.available,
            growthRate: utilizationMetrics.storage.growthRate,
            recommendation: this.generateStorageRecommendation(utilizationMetrics.storage)
          }
        },

        // Cost analysis
        costAnalysis: {
          currentMonthlyCost: costMetrics.currentMonthlyCost,
          costTrends: costMetrics.trends,
          costBreakdown: costMetrics.breakdown,

          // Optimization opportunities
          optimizationOpportunities: []
        }
      };

      // Generate optimization recommendations
      const recommendations = this.generateOptimizationRecommendations(
        performanceMetrics,
        utilizationMetrics,
        costMetrics,
        optimizationOptions
      );

      optimizationAnalysis.recommendations = recommendations;

      // Calculate potential savings
      const savingsAnalysis = this.calculatePotentialSavings(recommendations, costMetrics);
      optimizationAnalysis.savingsAnalysis = savingsAnalysis;

      // Apply optimizations if auto-optimization is enabled
      if (optimizationOptions.autoOptimize) {
        const optimizationResults = await this.applyOptimizations(clusterId, recommendations);
        optimizationAnalysis.optimizationResults = optimizationResults;
      }

      return {
        success: true,
        clusterId: clusterId,
        optimizationAnalysis: optimizationAnalysis
      };

    } catch (error) {
      console.error(`Error performing cost optimization for cluster ${clusterId}:`, error);
      return {
        success: false,
        error: error.message,
        clusterId: clusterId
      };
    }
  }

  // Utility methods for Atlas operations

  async atlasAPIRequest(method, endpoint, data = null) {
    const url = `${this.atlasConfig.baseURL}${endpoint}`;
    const auth = Buffer.from(`${this.atlasConfig.publicKey}:${this.atlasConfig.privateKey}`).toString('base64');

    try {
      const config = {
        method: method,
        url: url,
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Basic ${auth}`
        }
      };

      if (data) {
        config.data = data;
      }

      const response = await axios(config);
      return response.data;

    } catch (error) {
      console.error(`Atlas API request failed: ${method} ${endpoint}`, error);
      throw error;
    }
  }

  async validateAtlasCredentials() {
    try {
      await this.atlasAPIRequest('GET', '/orgs');
      console.log('Atlas API credentials validated successfully');
    } catch (error) {
      throw new Error('Invalid Atlas API credentials');
    }
  }

  generateDeploymentId() {
    return `deployment_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  generateTemplateId() {
    return `template_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }

  async waitForClusterReady(clusterId, timeoutMinutes = 30) {
    const timeout = timeoutMinutes * 60 * 1000;
    const startTime = Date.now();

    while (Date.now() - startTime < timeout) {
      try {
        const clusterStatus = await this.atlasAPIRequest('GET', `/groups/${this.atlasConfig.projectId}/clusters/${clusterId}`);

        if (clusterStatus.stateName === 'IDLE') {
          return {
            clusterId: clusterId,
            state: clusterStatus.stateName,
            connectionString: clusterStatus.connectionStrings?.standardSrv,
            mongoDBVersion: clusterStatus.mongoDBVersion
          };
        }

        console.log(`Waiting for cluster ${clusterId} to be ready. Current state: ${clusterStatus.stateName}`);
        await this.sleep(30000); // Wait 30 seconds

      } catch (error) {
        console.error(`Error checking cluster status: ${clusterId}`, error);
        await this.sleep(30000);
      }
    }

    throw new Error(`Cluster ${clusterId} did not become ready within ${timeoutMinutes} minutes`);
  }

  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  updateInfrastructureMetrics(deployment) {
    this.performanceMetrics.totalClusters++;
    // Update other metrics based on deployment
  }

  generateTerraformConfig(infrastructureTemplate) {
    // Generate Terraform configuration files based on template
    return {
      mainTf: `# MongoDB Atlas Infrastructure Configuration
provider "mongodbatlas" {
  public_key  = var.atlas_public_key
  private_key = var.atlas_private_key
}

# Variables and resources would be generated here based on template
`,
      variablesTf: `# Infrastructure variables
variable "atlas_public_key" {
  description = "MongoDB Atlas API Public Key"
  type        = string
}

variable "atlas_private_key" {
  description = "MongoDB Atlas API Private Key"
  type        = string
  sensitive   = true
}
`,
      outputsTf: `# Infrastructure outputs
output "cluster_connection_strings" {
  description = "Atlas Cluster Connection Strings"
  value       = mongodbatlas_cluster.main.connection_strings
}
`
    };
  }

  generatePipelineConfig(infrastructureTemplate) {
    // Generate CI/CD pipeline configuration
    return {
      githubActions: `# GitHub Actions workflow for Atlas infrastructure
name: MongoDB Atlas Infrastructure Deployment

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  terraform:
    name: Terraform
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v1

    - name: Terraform Plan
      run: terraform plan

    - name: Terraform Apply
      if: github.ref == 'refs/heads/main'
      run: terraform apply -auto-approve
`,
      jenkins: `// Jenkins pipeline for Atlas infrastructure
pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Terraform Plan') {
            steps {
                sh 'terraform plan -out=tfplan'
            }
        }

        stage('Terraform Apply') {
            when {
                branch 'main'
            }
            steps {
                sh 'terraform apply tfplan'
            }
        }
    }
}
`
    };
  }

  // Additional methods would include implementations for:
  // - setupAutomatedDeployment()
  // - setupCICDIntegration()
  // - configureNetworkSecurity()
  // - createDatabaseUsers()
  // - configureComputeScaling()
  // - configureStorageScaling()
  // - configurePerformanceMonitoring()
  // - configureInfrastructureMonitoring()
  // - configureApplicationMonitoring()
  // - configureSecurityMonitoring()
  // - collectPerformanceMetrics()
  // - collectUtilizationMetrics()
  // - collectCostMetrics()
  // - generateOptimizationRecommendations()
  // - calculatePotentialSavings()
  // - applyOptimizations()
}

// Benefits of MongoDB Atlas Advanced Infrastructure Management:
// - Automated deployment with infrastructure-as-code integration
// - Intelligent auto-scaling based on real-time metrics and predictions
// - Comprehensive monitoring and alerting for proactive management
// - Advanced security and compliance features built-in
// - DevOps pipeline integration for continuous deployment
// - Cost optimization with automated resource right-sizing
// - Enterprise-grade backup and disaster recovery capabilities
// - Multi-cloud deployment and management capabilities
// - SQL-compatible operations through QueryLeaf integration
// - Production-ready infrastructure automation and orchestration

module.exports = {
  AdvancedAtlasInfrastructureManager
};

Understanding MongoDB Atlas Infrastructure Architecture

Advanced Cloud Database Operations and DevOps Integration Patterns

Implement sophisticated Atlas infrastructure patterns for enterprise deployments:

// Enterprise-grade Atlas infrastructure with advanced DevOps integration and multi-cloud capabilities
class EnterpriseAtlasOrchestrator extends AdvancedAtlasInfrastructureManager {
  constructor(atlasConfig, enterpriseConfig) {
    super(atlasConfig);

    this.enterpriseConfig = {
      ...enterpriseConfig,
      enableMultiCloudDeployment: true,
      enableDisasterRecoveryAutomation: true,
      enableComplianceAutomation: true,
      enableAdvancedSecurity: true,
      enableGlobalDistribution: true
    };

    this.setupEnterpriseCapabilities();
    this.initializeMultiCloudOrchestration();
    this.setupComplianceAutomation();
  }

  async implementMultiCloudStrategy(cloudConfiguration) {
    console.log('Implementing multi-cloud Atlas deployment strategy...');

    const multiCloudStrategy = {
      // Multi-cloud provider configuration
      cloudProviders: {
        aws: { regions: ['us-east-1', 'eu-west-1'], priority: 1 },
        gcp: { regions: ['us-central1', 'europe-west1'], priority: 2 },
        azure: { regions: ['eastus', 'westeurope'], priority: 3 }
      },

      // Global distribution strategy
      globalDistribution: {
        primaryRegion: 'us-east-1',
        secondaryRegions: ['eu-west-1', 'asia-southeast-1'],
        dataResidencyRules: true,
        latencyOptimization: true
      },

      // Disaster recovery automation
      disasterRecovery: {
        crossCloudBackup: true,
        automaticFailover: true,
        recoveryTimeObjective: '4h',
        recoveryPointObjective: '15min'
      }
    };

    return await this.deployMultiCloudInfrastructure(multiCloudStrategy);
  }

  async setupAdvancedComplianceAutomation() {
    console.log('Setting up enterprise compliance automation...');

    const complianceCapabilities = {
      // Regulatory compliance
      regulatoryFrameworks: {
        gdpr: { dataResidency: true, rightToErasure: true },
        hipaa: { encryption: true, auditLogging: true },
        sox: { changeTracking: true, accessControls: true },
        pci: { dataEncryption: true, networkSecurity: true }
      },

      // Automated compliance monitoring
      complianceMonitoring: {
        continuousAssessment: true,
        violationDetection: true,
        automaticRemediation: true,
        complianceReporting: true
      },

      // Enterprise security
      enterpriseSecurity: {
        zeroTrustNetworking: true,
        advancedThreatDetection: true,
        dataLossPreventionm: true,
        privilegedAccessManagement: true
      }
    };

    return await this.deployComplianceAutomation(complianceCapabilities);
  }
}

SQL-Style Atlas Operations with QueryLeaf

QueryLeaf provides familiar SQL syntax for MongoDB Atlas infrastructure operations:

-- QueryLeaf advanced Atlas infrastructure operations with SQL-familiar syntax for MongoDB

-- Atlas cluster deployment with comprehensive configuration
CREATE ATLAS_CLUSTER production_cluster (
  -- Cluster configuration
  cluster_type = 'REPLICASET',
  mongodb_version = '7.0',

  -- Provider and region configuration
  provider_name = 'AWS',
  region_name = 'US_EAST_1',
  instance_size = 'M30',

  -- Multi-region configuration
  replication_specs = JSON_OBJECT(
    'num_shards', 1,
    'regions_config', JSON_OBJECT(
      'US_EAST_1', JSON_OBJECT(
        'electable_nodes', 3,
        'priority', 7,
        'read_only_nodes', 0
      ),
      'EU_WEST_1', JSON_OBJECT(
        'electable_nodes', 2,
        'priority', 6,
        'read_only_nodes', 1
      )
    )
  ),

  -- Auto-scaling configuration
  auto_scaling = JSON_OBJECT(
    'disk_gb_enabled', true,
    'compute_enabled', true,
    'compute_scale_down_enabled', true,
    'compute_min_instance_size', 'M10',
    'compute_max_instance_size', 'M80'
  ),

  -- Backup configuration
  backup_enabled = true,
  provider_backup_enabled = true,

  -- Performance configuration
  disk_iops = 3000,
  volume_type = 'PROVISIONED',
  encrypt_ebs_volume = true,

  -- Advanced configuration
  bi_connector_enabled = false,
  pit_enabled = true,
  oplog_size_mb = 2048,

  -- Network security
  network_access_list = ARRAY[
    JSON_OBJECT('ip_address', '10.0.0.0/8', 'comment', 'Internal network'),
    JSON_OBJECT('cidr_block', '172.16.0.0/12', 'comment', 'VPC network')
  ],

  -- Monitoring and alerting
  monitoring = JSON_OBJECT(
    'enable_performance_advisor', true,
    'enable_realtime_performance_panel', true,
    'enable_schema_advisor', true,
    'data_explorer_enabled', true
  )
);

-- Advanced Atlas cluster monitoring and performance analysis
WITH cluster_performance AS (
  SELECT 
    cluster_name,
    cluster_id,
    DATE_TRUNC('hour', metric_timestamp) as time_bucket,

    -- Performance metrics aggregation
    AVG(connections_current) as avg_connections,
    MAX(connections_current) as peak_connections,
    AVG(opcounters_query) as avg_queries_per_second,
    AVG(opcounters_insert) as avg_inserts_per_second,
    AVG(opcounters_update) as avg_updates_per_second,
    AVG(opcounters_delete) as avg_deletes_per_second,

    -- Resource utilization
    AVG(system_cpu_user) as avg_cpu_user,
    AVG(system_cpu_kernel) as avg_cpu_kernel,
    AVG(system_memory_used_mb) / AVG(system_memory_available_mb) * 100 as avg_memory_utilization,
    AVG(system_network_in_bytes) as avg_network_in_bytes,
    AVG(system_network_out_bytes) as avg_network_out_bytes,

    -- Storage metrics
    AVG(system_disk_space_used_data_bytes) as avg_data_size_bytes,
    AVG(system_disk_space_used_index_bytes) as avg_index_size_bytes,
    AVG(system_disk_space_used_total_bytes) as avg_total_storage_bytes,

    -- Performance indicators
    AVG(global_lock_current_queue_readers) as avg_queue_readers,
    AVG(global_lock_current_queue_writers) as avg_queue_writers,
    AVG(wt_cache_pages_currently_held_in_cache) as avg_cache_pages,

    -- Replication metrics
    AVG(replset_oplog_head_timestamp) as oplog_head_timestamp,
    AVG(replset_oplog_tail_timestamp) as oplog_tail_timestamp,
    MAX(replset_member_lag_millis) as max_replication_lag

  FROM ATLAS_METRICS('production_cluster')
  WHERE metric_timestamp >= CURRENT_TIMESTAMP - INTERVAL '24 hours'
  GROUP BY cluster_name, cluster_id, DATE_TRUNC('hour', metric_timestamp)
),

performance_analysis AS (
  SELECT 
    cp.*,

    -- Calculate total CPU utilization
    (cp.avg_cpu_user + cp.avg_cpu_kernel) as total_cpu_utilization,

    -- Calculate storage utilization percentage
    CASE 
      WHEN cp.avg_total_storage_bytes > 0 THEN
        (cp.avg_data_size_bytes + cp.avg_index_size_bytes) / cp.avg_total_storage_bytes * 100
      ELSE 0
    END as storage_utilization_percent,

    -- Network utilization
    (cp.avg_network_in_bytes + cp.avg_network_out_bytes) / 1024 / 1024 as total_network_mb,

    -- Performance score calculation
    CASE 
      WHEN (cp.avg_cpu_user + cp.avg_cpu_kernel) > 80 THEN 'high_cpu_load'
      WHEN cp.avg_memory_utilization > 85 THEN 'high_memory_usage'
      WHEN cp.avg_queue_readers + cp.avg_queue_writers > 10 THEN 'high_queue_pressure'
      WHEN cp.max_replication_lag > 10000 THEN 'high_replication_lag'
      ELSE 'healthy'
    END as performance_status,

    -- Capacity planning indicators
    LAG(cp.avg_connections) OVER (ORDER BY cp.time_bucket) as prev_hour_connections,
    LAG(cp.avg_total_storage_bytes) OVER (ORDER BY cp.time_bucket) as prev_hour_storage,

    -- Query performance trends
    (cp.avg_queries_per_second + cp.avg_inserts_per_second + 
     cp.avg_updates_per_second + cp.avg_deletes_per_second) as total_operations_per_second

  FROM cluster_performance cp
),

scaling_recommendations AS (
  SELECT 
    pa.*,

    -- Connection scaling analysis
    CASE 
      WHEN pa.peak_connections > 500 AND pa.avg_connections / pa.peak_connections > 0.8 THEN 'scale_up_connections'
      WHEN pa.peak_connections < 100 AND pa.avg_connections < 50 THEN 'optimize_connection_pooling'
      ELSE 'connections_appropriate'
    END as connection_scaling_recommendation,

    -- Compute scaling analysis
    CASE 
      WHEN pa.total_cpu_utilization > 75 AND pa.avg_memory_utilization > 80 THEN 'scale_up_compute'
      WHEN pa.total_cpu_utilization < 30 AND pa.avg_memory_utilization < 50 THEN 'scale_down_compute'
      ELSE 'compute_appropriate'
    END as compute_scaling_recommendation,

    -- Storage scaling analysis
    CASE 
      WHEN pa.storage_utilization_percent > 85 THEN 'increase_storage_immediately'
      WHEN pa.storage_utilization_percent > 75 THEN 'monitor_storage_closely'
      WHEN (pa.avg_total_storage_bytes - pa.prev_hour_storage) > 1024*1024*1024 THEN 'high_storage_growth'
      ELSE 'storage_appropriate'
    END as storage_scaling_recommendation,

    -- Performance optimization recommendations
    ARRAY[
      CASE WHEN pa.avg_queue_readers > 5 THEN 'optimize_read_queries' END,
      CASE WHEN pa.avg_queue_writers > 5 THEN 'optimize_write_operations' END,
      CASE WHEN pa.max_replication_lag > 5000 THEN 'investigate_replication_lag' END,
      CASE WHEN pa.avg_cache_pages < 1000 THEN 'increase_cache_size' END
    ]::TEXT[] as performance_optimization_recommendations,

    -- Cost optimization opportunities
    CASE 
      WHEN pa.total_cpu_utilization < 25 AND pa.avg_memory_utilization < 40 THEN 'overprovisioned'
      WHEN pa.total_operations_per_second < 100 AND pa.avg_connections < 10 THEN 'underutilized'
      ELSE 'appropriately_sized'
    END as cost_optimization_status

  FROM performance_analysis pa
)

SELECT 
  sr.cluster_name,
  sr.time_bucket,

  -- Performance metrics
  ROUND(sr.total_cpu_utilization, 2) as cpu_utilization_percent,
  ROUND(sr.avg_memory_utilization, 2) as memory_utilization_percent,
  ROUND(sr.storage_utilization_percent, 2) as storage_utilization_percent,
  sr.avg_connections,
  sr.peak_connections,

  -- Operations throughput
  ROUND(sr.total_operations_per_second, 2) as operations_per_second,
  ROUND(sr.total_network_mb, 2) as network_throughput_mb,

  -- Performance assessment
  sr.performance_status,

  -- Scaling recommendations
  sr.connection_scaling_recommendation,
  sr.compute_scaling_recommendation,
  sr.storage_scaling_recommendation,

  -- Optimization recommendations
  ARRAY_REMOVE(sr.performance_optimization_recommendations, NULL) as optimization_recommendations,

  -- Cost optimization
  sr.cost_optimization_status,

  -- Growth trends
  CASE 
    WHEN sr.avg_connections > sr.prev_hour_connections * 1.1 THEN 'connection_growth'
    WHEN sr.avg_total_storage_bytes > sr.prev_hour_storage * 1.05 THEN 'storage_growth'
    ELSE 'stable'
  END as growth_trend,

  -- Alert conditions
  ARRAY[
    CASE WHEN sr.total_cpu_utilization > 90 THEN 'CRITICAL: CPU utilization very high' END,
    CASE WHEN sr.avg_memory_utilization > 95 THEN 'CRITICAL: Memory utilization critical' END,
    CASE WHEN sr.storage_utilization_percent > 90 THEN 'CRITICAL: Storage nearly full' END,
    CASE WHEN sr.max_replication_lag > 30000 THEN 'WARNING: High replication lag detected' END,
    CASE WHEN sr.avg_queue_readers + sr.avg_queue_writers > 20 THEN 'WARNING: High queue pressure' END
  ]::TEXT[] as active_alerts,

  -- Actionable insights
  CASE 
    WHEN sr.performance_status = 'high_cpu_load' THEN 'Scale up instance size or optimize queries'
    WHEN sr.performance_status = 'high_memory_usage' THEN 'Increase memory or optimize data structures'
    WHEN sr.performance_status = 'high_queue_pressure' THEN 'Optimize slow queries and add indexes'
    WHEN sr.performance_status = 'high_replication_lag' THEN 'Check network connectivity and oplog size'
    WHEN sr.cost_optimization_status = 'overprovisioned' THEN 'Consider scaling down to reduce costs'
    ELSE 'Continue monitoring current performance'
  END as recommended_action

FROM scaling_recommendations sr
WHERE sr.performance_status != 'healthy' 
   OR sr.cost_optimization_status IN ('overprovisioned', 'underutilized')
   OR sr.compute_scaling_recommendation != 'compute_appropriate'
ORDER BY 
  CASE sr.performance_status 
    WHEN 'high_cpu_load' THEN 1
    WHEN 'high_memory_usage' THEN 2
    WHEN 'high_queue_pressure' THEN 3
    WHEN 'high_replication_lag' THEN 4
    ELSE 5
  END,
  sr.time_bucket DESC;

-- Atlas infrastructure-as-code deployment and management
WITH deployment_templates AS (
  SELECT 
    template_name,
    template_version,
    environment,

    -- Infrastructure specification
    JSON_BUILD_OBJECT(
      'cluster_config', JSON_BUILD_OBJECT(
        'cluster_type', 'REPLICASET',
        'mongodb_version', '7.0',
        'provider_name', 'AWS',
        'instance_size', CASE environment
          WHEN 'production' THEN 'M30'
          WHEN 'staging' THEN 'M20'
          WHEN 'development' THEN 'M10'
        END,
        'replication_factor', CASE environment
          WHEN 'production' THEN 3
          WHEN 'staging' THEN 3
          WHEN 'development' THEN 1
        END
      ),
      'auto_scaling', JSON_BUILD_OBJECT(
        'compute_enabled', environment IN ('production', 'staging'),
        'storage_enabled', true,
        'min_instance_size', CASE environment
          WHEN 'production' THEN 'M30'
          WHEN 'staging' THEN 'M20'
          WHEN 'development' THEN 'M10'
        END,
        'max_instance_size', CASE environment
          WHEN 'production' THEN 'M80'
          WHEN 'staging' THEN 'M40'
          WHEN 'development' THEN 'M20'
        END
      ),
      'backup_config', JSON_BUILD_OBJECT(
        'continuous_backup_enabled', environment = 'production',
        'snapshot_backup_enabled', true,
        'backup_retention_days', CASE environment
          WHEN 'production' THEN 7
          WHEN 'staging' THEN 3
          WHEN 'development' THEN 1
        END
      ),
      'security_config', JSON_BUILD_OBJECT(
        'encryption_at_rest', environment IN ('production', 'staging'),
        'network_access_restricted', true,
        'database_auditing', environment = 'production',
        'ldap_authentication', environment = 'production'
      )
    ) as infrastructure_spec,

    -- Deployment configuration
    JSON_BUILD_OBJECT(
      'deployment_strategy', 'rolling',
      'approval_required', environment = 'production',
      'automated_testing', true,
      'rollback_on_failure', true,
      'notification_channels', ARRAY[
        'email:[email protected]',
        'slack:#database-ops'
      ]
    ) as deployment_config,

    -- Monitoring configuration
    JSON_BUILD_OBJECT(
      'performance_monitoring', true,
      'custom_alerts', ARRAY[
        JSON_BUILD_OBJECT(
          'metric', 'CONNECTIONS_PERCENT',
          'threshold', 80,
          'comparison', 'GREATER_THAN'
        ),
        JSON_BUILD_OBJECT(
          'metric', 'NORMALIZED_SYSTEM_CPU_USER',
          'threshold', 75,
          'comparison', 'GREATER_THAN'
        ),
        JSON_BUILD_OBJECT(
          'metric', 'DISK_PARTITION_SPACE_USED_DATA',
          'threshold', 85,
          'comparison', 'GREATER_THAN'
        )
      ],
      'notification_delay_minutes', 5,
      'auto_scaling_triggers', JSON_BUILD_OBJECT(
        'cpu_threshold_percent', 75,
        'memory_threshold_percent', 80,
        'connections_threshold_percent', 80
      )
    ) as monitoring_config

  FROM (
    VALUES 
      ('web-app-cluster', '1.0', 'production'),
      ('web-app-cluster', '1.0', 'staging'),
      ('web-app-cluster', '1.0', 'development'),
      ('analytics-cluster', '1.0', 'production'),
      ('reporting-cluster', '1.0', 'production')
  ) as templates(template_name, template_version, environment)
),

deployment_validation AS (
  SELECT 
    dt.*,

    -- Cost estimation
    CASE dt.environment
      WHEN 'production' THEN 
        CASE 
          WHEN dt.infrastructure_spec->>'cluster_config'->>'instance_size' = 'M30' THEN 590
          WHEN dt.infrastructure_spec->>'cluster_config'->>'instance_size' = 'M40' THEN 940
          WHEN dt.infrastructure_spec->>'cluster_config'->>'instance_size' = 'M80' THEN 2350
          ELSE 300
        END
      WHEN 'staging' THEN 
        CASE 
          WHEN dt.infrastructure_spec->>'cluster_config'->>'instance_size' = 'M20' THEN 350
          WHEN dt.infrastructure_spec->>'cluster_config'->>'instance_size' = 'M40' THEN 940
          ELSE 200
        END
      ELSE 57  -- Development M10
    END as estimated_monthly_cost_usd,

    -- Compliance validation
    CASE 
      WHEN dt.environment = 'production' AND 
           (dt.infrastructure_spec->'security_config'->>'encryption_at_rest')::BOOLEAN = false THEN 'encryption_required'
      WHEN dt.environment = 'production' AND 
           (dt.infrastructure_spec->'backup_config'->>'continuous_backup_enabled')::BOOLEAN = false THEN 'continuous_backup_required'
      WHEN (dt.infrastructure_spec->'security_config'->>'network_access_restricted')::BOOLEAN = false THEN 'network_security_required'
      ELSE 'compliant'
    END as compliance_status,

    -- Resource sizing validation
    CASE 
      WHEN dt.environment = 'production' AND 
           dt.infrastructure_spec->>'cluster_config'->>'instance_size' < 'M30' THEN 'undersized_for_production'
      WHEN dt.environment = 'development' AND 
           dt.infrastructure_spec->>'cluster_config'->>'instance_size' > 'M20' THEN 'oversized_for_development'
      ELSE 'appropriately_sized'
    END as sizing_validation,

    -- Deployment readiness
    CASE 
      WHEN dt.infrastructure_spec IS NULL THEN 'missing_infrastructure_spec'
      WHEN dt.deployment_config IS NULL THEN 'missing_deployment_config'
      WHEN dt.monitoring_config IS NULL THEN 'missing_monitoring_config'
      ELSE 'ready_for_deployment'
    END as deployment_readiness

  FROM deployment_templates dt
)

SELECT 
  dv.template_name,
  dv.environment,
  dv.template_version,

  -- Infrastructure summary
  dv.infrastructure_spec->'cluster_config'->>'instance_size' as instance_size,
  dv.infrastructure_spec->'cluster_config'->>'mongodb_version' as mongodb_version,
  (dv.infrastructure_spec->'cluster_config'->>'replication_factor')::INTEGER as replication_factor,

  -- Auto-scaling configuration
  (dv.infrastructure_spec->'auto_scaling'->>'compute_enabled')::BOOLEAN as auto_scaling_enabled,
  dv.infrastructure_spec->'auto_scaling'->>'min_instance_size' as min_instance_size,
  dv.infrastructure_spec->'auto_scaling'->>'max_instance_size' as max_instance_size,

  -- Security and backup
  (dv.infrastructure_spec->'security_config'->>'encryption_at_rest')::BOOLEAN as encryption_enabled,
  (dv.infrastructure_spec->'backup_config'->>'continuous_backup_enabled')::BOOLEAN as continuous_backup,
  (dv.infrastructure_spec->'backup_config'->>'backup_retention_days')::INTEGER as backup_retention_days,

  -- Cost and validation
  dv.estimated_monthly_cost_usd,
  dv.compliance_status,
  dv.sizing_validation,
  dv.deployment_readiness,

  -- Alert configuration count
  JSON_ARRAY_LENGTH(dv.monitoring_config->'custom_alerts') as custom_alert_count,

  -- Deployment recommendations
  ARRAY[
    CASE WHEN dv.compliance_status != 'compliant' THEN 'Fix compliance issues before deployment' END,
    CASE WHEN dv.sizing_validation LIKE '%undersized%' THEN 'Increase instance size for production workload' END,
    CASE WHEN dv.sizing_validation LIKE '%oversized%' THEN 'Consider smaller instance size to reduce costs' END,
    CASE WHEN dv.estimated_monthly_cost_usd > 1000 AND dv.environment != 'production' 
         THEN 'Review cost allocation for non-production environment' END,
    CASE WHEN JSON_ARRAY_LENGTH(dv.monitoring_config->'custom_alerts') < 3 
         THEN 'Add more comprehensive monitoring alerts' END
  ]::TEXT[] as deployment_recommendations,

  -- Deployment priority
  CASE 
    WHEN dv.deployment_readiness != 'ready_for_deployment' THEN 'blocked'
    WHEN dv.compliance_status != 'compliant' THEN 'compliance_review_required'
    WHEN dv.environment = 'production' THEN 'high_priority'
    WHEN dv.environment = 'staging' THEN 'medium_priority'
    ELSE 'low_priority'
  END as deployment_priority,

  -- Terraform generation command
  CASE 
    WHEN dv.deployment_readiness = 'ready_for_deployment' THEN
      FORMAT('terraform apply -var="environment=%s" -var="instance_size=%s" -target=mongodbatlas_cluster.%s_%s',
             dv.environment,
             dv.infrastructure_spec->'cluster_config'->>'instance_size',
             dv.template_name,
             dv.environment)
    ELSE 'Fix validation issues first'
  END as terraform_command

FROM deployment_validation dv
ORDER BY 
  CASE dv.deployment_priority
    WHEN 'blocked' THEN 1
    WHEN 'compliance_review_required' THEN 2
    WHEN 'high_priority' THEN 3
    WHEN 'medium_priority' THEN 4
    ELSE 5
  END,
  dv.template_name,
  dv.environment;

-- QueryLeaf provides comprehensive MongoDB Atlas infrastructure capabilities:
-- 1. Automated cluster deployment with infrastructure-as-code integration
-- 2. Advanced performance monitoring and intelligent auto-scaling
-- 3. Cost optimization and resource right-sizing recommendations
-- 4. Security and compliance automation with policy enforcement
-- 5. DevOps pipeline integration for continuous deployment
-- 6. Multi-cloud deployment and disaster recovery capabilities
-- 7. SQL-familiar syntax for complex Atlas infrastructure operations
-- 8. Enterprise-grade monitoring, alerting, and operational excellence
-- 9. Terraform and CI/CD integration for automated infrastructure management
-- 10. Production-ready Atlas operations with comprehensive automation

Best Practices for Production Atlas Deployments

Infrastructure Architecture and Automation Strategy

Essential principles for effective MongoDB Atlas production deployment:

  1. Infrastructure-as-Code: Implement comprehensive infrastructure-as-code with version control, testing, and automated deployment pipelines
  2. Auto-Scaling Configuration: Design intelligent auto-scaling policies based on application patterns and performance requirements
  3. Security Integration: Implement advanced security controls, network isolation, and encryption at rest and in transit
  4. Monitoring Strategy: Configure comprehensive monitoring, alerting, and performance optimization for proactive management
  5. Disaster Recovery: Design multi-region backup strategies and automated disaster recovery procedures
  6. Cost Optimization: Implement continuous cost monitoring and automated resource optimization based on utilization patterns

DevOps Integration and Production Operations

Optimize Atlas operations for enterprise-scale DevOps workflows:

  1. CI/CD Integration: Build comprehensive deployment pipelines with automated testing, approval workflows, and rollback capabilities
  2. Environment Management: Design consistent environment promotion strategies with appropriate resource sizing and security controls
  3. Performance Monitoring: Implement intelligent performance monitoring with predictive scaling and optimization recommendations
  4. Compliance Automation: Ensure automated compliance monitoring and policy enforcement for regulatory requirements
  5. Operational Excellence: Design automated operational procedures for maintenance, scaling, and incident response
  6. Cost Management: Monitor cloud spending patterns and implement automated cost optimization strategies

Conclusion

MongoDB Atlas provides comprehensive cloud database infrastructure automation that enables sophisticated DevOps integration, intelligent scaling, and enterprise-grade operational capabilities through infrastructure-as-code, automated monitoring, and advanced security features. The Atlas platform ensures that cloud database operations benefit from MongoDB's managed service expertise while providing the flexibility and control needed for production applications.

Key MongoDB Atlas benefits include:

  • Infrastructure Automation: Complete infrastructure-as-code integration with automated provisioning, scaling, and lifecycle management
  • Intelligent Operations: AI-powered performance optimization, predictive scaling, and automated operational recommendations
  • DevOps Integration: Seamless CI/CD pipeline integration with automated testing, deployment, and rollback capabilities
  • Enterprise Security: Advanced security controls, compliance automation, and built-in best practices for production environments
  • Cost Optimization: Intelligent resource management and automated cost optimization based on actual usage patterns
  • SQL Accessibility: Familiar SQL-style Atlas operations through QueryLeaf for accessible cloud database management

Whether you're building cloud-native applications, implementing DevOps automation, managing multi-environment deployments, or optimizing database operations at scale, MongoDB Atlas with QueryLeaf's familiar SQL interface provides the foundation for sophisticated, automated cloud database infrastructure.

QueryLeaf Integration: QueryLeaf automatically optimizes MongoDB Atlas operations while providing SQL-familiar syntax for infrastructure management, monitoring, and automation. Advanced Atlas features, DevOps integration, and operational automation are seamlessly handled through familiar SQL constructs, making sophisticated cloud database operations accessible to SQL-oriented infrastructure teams.

The combination of MongoDB Atlas's robust cloud capabilities with SQL-style infrastructure operations makes it an ideal platform for applications requiring both automated database operations and familiar infrastructure management patterns, ensuring your cloud database infrastructure can scale efficiently while maintaining operational excellence and cost optimization as application complexity and usage grow.