Aller au contenu

MangoDB Boussole feuille de chaleur

MongoDB Boussole - L'interface graphique pour MongoDB

MangoDB Compass est l'interface utilisateur graphique officielle de MongoDB. Il offre un moyen intuitif d'explorer et de manipuler vos données MongoDB avec la construction de requêtes visuelles, des mesures de performance en temps réel et une analyse de schéma complète.

Copier toutes les commandes Générer PDF

Sommaire

  • [Installation] (LINK_0)
  • [Gestion de la connexion] (LINK_0)
  • [Opérations de base de données] (LINK_0)
  • [Gestion des collections] (LINK_0)
  • [Exploitation des documents] (LINK_0)
  • [Constructeur de requêtes] (LINK_0)
  • [Compagnie d'agrégation] (LINK_0)
  • [Analyse du schéma] (LINK_0)
  • [Gestion des indices] (LINK_0)
  • [Surveillance du rendement] (LINK_0)
  • [Import/Export] (LINK_0)
  • [Meilleures pratiques] (LINK_0)

Installation

Installation de Windows

# Download from MongoDB website
# https://www.mongodb.com/try/download/compass

# Install via Chocolatey
choco install mongodb-compass

# Install via winget
winget install MongoDB.Compass

# Silent installation
msiexec /i mongodb-compass-1.40.4-win32-x64.msi /quiet

# Install specific version
choco install mongodb-compass --version 1.40.4

installation macOS

# Download from MongoDB website
# https://www.mongodb.com/try/download/compass

# Install via Homebrew
brew install --cask mongodb-compass

# Install specific version
brew install --cask mongodb-compass@1.40.4

# Manual installation
curl -O https://downloads.mongodb.com/compass/mongodb-compass-1.40.4-darwin-x64.dmg
open mongodb-compass-1.40.4-darwin-x64.dmg
```_

### Installation Linux
```bash
# Ubuntu/Debian
wget https://downloads.mongodb.com/compass/mongodb-compass_1.40.4_amd64.deb
sudo dpkg -i mongodb-compass_1.40.4_amd64.deb

# Fix dependencies if needed
sudo apt-get install -f

# CentOS/RHEL/Fedora
wget https://downloads.mongodb.com/compass/mongodb-compass-1.40.4.x86_64.rpm
sudo rpm -i mongodb-compass-1.40.4.x86_64.rpm

# Or using yum/dnf
sudo yum install mongodb-compass-1.40.4.x86_64.rpm
sudo dnf install mongodb-compass-1.40.4.x86_64.rpm

# AppImage (Universal Linux)
wget https://downloads.mongodb.com/compass/mongodb-compass-1.40.4.AppImage
chmod +x mongodb-compass-1.40.4.AppImage
./mongodb-compass-1.40.4.AppImage

# Snap package
sudo snap install mongodb-compass
```_

### Installation Docker
```bash
# Run MongoDB Compass in Docker (with X11 forwarding)
docker run -it --rm \
  -e DISPLAY=$DISPLAY \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  -v $HOME/.Xauthority:/root/.Xauthority \
  --net=host \
  mongodb/compass

# For macOS with XQuartz
xhost +localhost
docker run -it --rm \
  -e DISPLAY=host.docker.internal:0 \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  mongodb/compass

Compass Editions

MongoDB Compass Editions:

1. Compass (Full Edition):
    - Complete feature set
    - Schema analysis
    - Query performance insights
    - Real-time server stats
    - Document validation
    - Aggregation pipeline builder

2. Compass Community:
    - Core features
    - Basic querying and editing
    - Index management
    - Import/export functionality

3. Compass Readonly:
    - View-only access
    - Query execution (read-only)
    - Schema analysis
    - No data modification capabilities

4. Compass Isolated:
    - No network access
    - No automatic updates
    - No usage statistics collection
    - Enhanced security for isolated environments

Gestion des connexions

Connexion de base

// Connection string format
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

// Local MongoDB instance
mongodb://localhost:27017

// MongoDB with authentication
mongodb://username:password@localhost:27017/database

// MongoDB Atlas connection
mongodb+srv://username:password@cluster.mongodb.net/database

// Replica set connection
mongodb://host1:27017,host2:27017,host3:27017/database?replicaSet=myReplicaSet

// Connection with SSL
mongodb://username:password@host:27017/database?ssl=true

// Connection with specific options
mongodb://username:password@host:27017/database?authSource=admin&readPreference;=secondary

Configuration de connexion

{
  "connectionString": "mongodb://localhost:27017",
  "ssl": {
    "enabled": true,
    "certificateFile": "/path/to/client.pem",
    "certificateKeyFile": "/path/to/client-key.pem",
    "certificateKeyFilePassword": "password",
    "caFile": "/path/to/ca.pem",
    "allowInvalidCertificates": false,
    "allowInvalidHostnames": false
  },
  "ssh": {
    "enabled": true,
    "host": "ssh-server.example.com",
    "port": 22,
    "username": "ssh-user",
    "password": "ssh-password",
    "identityFile": "/path/to/private-key",
    "passphrase": "key-passphrase"
  },
  "authentication": {
    "method": "SCRAM-SHA-256",
    "username": "dbuser",
    "password": "dbpassword",
    "authSource": "admin",
    "mechanism": "SCRAM-SHA-256"
  },
  "readPreference": "primary",
  "replicaSet": "myReplicaSet",
  "connectTimeoutMS": 10000,
  "serverSelectionTimeoutMS": 5000,
  "socketTimeoutMS": 0,
  "maxPoolSize": 10
}

Dépannage de la connexion

# Test MongoDB connection from command line
mongo "mongodb://localhost:27017"

# Test with authentication
mongo "mongodb://username:password@localhost:27017/database"

# Test Atlas connection
mongo "mongodb+srv://username:password@cluster.mongodb.net/database"

# Check MongoDB service status
sudo systemctl status mongod

# Check MongoDB logs
sudo tail -f /var/log/mongodb/mongod.log

# Common connection issues:

# 1. MongoDB not running
sudo systemctl start mongod

# 2. Wrong port
netstat -tulpn | grep 27017

# 3. Firewall blocking connection
sudo ufw allow 27017

# 4. Authentication failure
# Check user exists and has proper permissions
use admin
db.getUsers()

# 5. SSL/TLS issues
# Verify certificate validity
openssl x509 -in certificate.pem -text -noout

# 6. Network connectivity
ping mongodb-server.example.com
telnet mongodb-server.example.com 27017

Connexions préférées

// Compass stores favorite connections
// Location varies by OS:
// Windows: %APPDATA%/MongoDB Compass/Connections
// macOS: ~/Library/Application Support/MongoDB Compass/Connections
// Linux: ~/.config/MongoDB Compass/Connections

{
  "id": "local-dev",
  "name": "Local Development",
  "connectionString": "mongodb://localhost:27017",
  "color": "#1DB954",
  "isFavorite": true,
  "lastUsed": "2023-12-01T10:30:00Z"
}

Opérations de bases de données

Création de bases de données

// Databases are created implicitly when you create a collection
// Via Compass: Click "Create Database" button

// Database naming rules:
// - Cannot be empty
// - Cannot contain spaces, certain punctuation
// - Case sensitive
// - Maximum 64 bytes
// - Cannot use reserved names (admin, local, config)

// Good database names:
// - myapp
// - user_data
// - analytics_2023
// - ecommerce

// Bad database names:
// - my app (space)
// - my-app! (special characters)
// - admin (reserved)

Informations sur la base de données

// View database statistics in Compass
// Click on database name to see:
// - Collections count
// - Total size
// - Storage size
// - Number of indexes
// - Average object size

// Equivalent MongoDB commands:
db.stats()

// Database storage stats
db.runCommand({dbStats: 1, scale: 1024*1024}) // MB

// List all databases
show dbs

// Switch to database
use myapp

// Drop database
db.dropDatabase()

Opérations de base de données via Compass

Database Operations in Compass:

Creating Database:
1. Click "+" next to "Databases" in left sidebar
2. Enter database name
3. Enter initial collection name
4. Click "Create Database"

Viewing Database Info:
1. Click on database name in sidebar
2. View overview tab showing:
    - Collections list
    - Database stats
    - Storage information
    - Index information

Database Actions:
1. Right-click database name
2. Available options:
    - Create Collection
    - Drop Database
    - Refresh
    - Open in New Tab

Database Stats Display:
- Collections: Number of collections
- Storage Size: Total storage used
- Data Size: Size of data (excluding indexes)
- Index Size: Size of all indexes
- Objects: Total number of documents

Gestion de la collecte

Création de collections

// Create collection via Compass
// 1. Select database
// 2. Click "Create Collection"
// 3. Configure options

// Collection options:
{
  "capped": false,           // Capped collection
  "size": 1000000,          // Max size in bytes (for capped)
  "max": 1000,              // Max documents (for capped)
  "validator": {            // Document validation
    "$jsonSchema": {
      "bsonType": "object",
      "required": ["name", "email"],
      "properties": {
        "name": {
          "bsonType": "string",
          "description": "must be a string and is required"
        },
        "email": {
          "bsonType": "string",
          "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
          "description": "must be a valid email address"
        },
        "age": {
          "bsonType": "int",
          "minimum": 0,
          "maximum": 150,
          "description": "must be an integer between 0 and 150"
        }
      }
    }
  },
  "validationLevel": "strict",    // strict, moderate
  "validationAction": "error",    // error, warn
  "collation": {
    "locale": "en",
    "strength": 2
  }
}

// Time series collection (MongoDB 5.0+)
{
  "timeseries": {
    "timeField": "timestamp",
    "metaField": "metadata",
    "granularity": "seconds"    // seconds, minutes, hours
  }
}

// Clustered collection (MongoDB 5.3+)
{
  "clusteredIndex": {
    "key": {"_id": 1},
    "unique": true
  }
}

Opérations de recouvrement

// View collection statistics
db.collection.stats()

// Collection storage stats
db.collection.storageSize()
db.collection.totalIndexSize()
db.collection.totalSize()

// Count documents
db.collection.countDocuments()
db.collection.estimatedDocumentCount()

// Rename collection
db.collection.renameCollection("newName")

// Drop collection
db.collection.drop()

// Create index
db.collection.createIndex({"field": 1})

// List indexes
db.collection.getIndexes()

// Compact collection (reclaim space)
db.runCommand({compact: "collection"})

// Validate collection
db.runCommand({validate: "collection"})

Analyse du schéma de collecte

// Compass automatically analyzes schema
// Shows for each field:
// - Field name and path
// - Data types present
// - Frequency of each type
// - Sample values
// - Min/max values for numbers
// - String length statistics

// Schema analysis covers:
{
  "field_name": {
    "types": [
      {
        "name": "String",
        "bsonType": "string",
        "probability": 0.85,
        "unique": 1234,
        "samples": ["example1", "example2"]
      },
      {
        "name": "Number",
        "bsonType": "number", 
        "probability": 0.15,
        "min": 1,
        "max": 1000,
        "average": 250.5
      }
    ],
    "total_count": 10000,
    "has_duplicates": true,
    "probability": 0.95  // Field presence probability
  }
}

// Nested document analysis
{
  "address": {
    "types": [{"name": "Document", "bsonType": "object"}],
    "fields": {
      "street": {"types": [{"name": "String", "bsonType": "string"}]},
      "city": {"types": [{"name": "String", "bsonType": "string"}]},
      "zipcode": {"types": [{"name": "String", "bsonType": "string"}]}
    }
  }
}

// Array analysis
{
  "tags": {
    "types": [{"name": "Array", "bsonType": "array"}],
    "lengths": [0, 1, 2, 3, 4, 5],
    "average_length": 2.3,
    "total_count": 5000,
    "types": [
      {"name": "String", "bsonType": "string", "probability": 1.0}
    ]
  }
}

Opérations des documents

Affichage des documents

// Compass document view modes:
// 1. List View - Tabular format
// 2. JSON View - Raw JSON format
// 3. Table View - Spreadsheet-like format

// Document navigation:
// - Pagination controls
// - Documents per page (20, 50, 100, 1000)
// - Jump to page
// - Total document count

// Document expansion:
// - Expand/collapse nested objects
// - Expand/collapse arrays
// - Copy field path
// - Copy field value

Création de documents

// Insert single document via Compass
// 1. Click "Insert Document" button
// 2. Choose input mode:
//    - JSON Mode: Raw JSON input
//    - Field-by-field Mode: Form-based input

// JSON Mode example:
{
  "name": "John Doe",
  "email": "john@example.com",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "zipcode": "10001"
  },
  "tags": ["developer", "mongodb", "javascript"],
  "created_at": {"$date": "2023-12-01T10:30:00Z"},
  "is_active": true
}

// Field-by-field mode:
// - Automatically detects data types
// - Provides type selection dropdown
// - Validates input format
// - Supports nested objects and arrays

// Supported BSON types in Compass:
// - String
// - Number (Int32, Int64, Double, Decimal128)
// - Boolean
// - Date
// - ObjectId
// - Array
// - Object
// - Binary
// - Null
// - Regex
// - JavaScript
// - Timestamp

Édition des documents

// Edit document in Compass:
// 1. Click pencil icon next to document
// 2. Modify fields in editor
// 3. Click "Update" to save changes

// Edit modes:
// - JSON Mode: Edit raw JSON
// - Field Mode: Edit individual fields

// Field editing features:
// - Add new fields
// - Delete existing fields
// - Change field types
// - Modify nested objects
// - Edit array elements
// - Reorder array elements

// Validation during editing:
// - JSON syntax validation
// - BSON type validation
// - Schema validation (if enabled)
// - Required field validation

// Bulk operations:
// - Select multiple documents
// - Delete selected documents
// - Export selected documents

Validation des documents

// Set up document validation in Compass
// 1. Go to Collection tab
// 2. Click "Validation" tab
// 3. Configure validation rules

// JSON Schema validation example:
{
  "$jsonSchema": {
    "bsonType": "object",
    "title": "User Object Validation",
    "required": ["name", "email", "age"],
    "properties": {
      "name": {
        "bsonType": "string",
        "description": "must be a string and is required"
      },
      "email": {
        "bsonType": "string",
        "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
        "description": "must be a valid email address and is required"
      },
      "age": {
        "bsonType": "int",
        "minimum": 18,
        "maximum": 100,
        "description": "must be an integer between 18 and 100 and is required"
      },
      "phone": {
        "bsonType": "string",
        "pattern": "^\\+?[1-9]\\d{1,14}$",
        "description": "must be a valid phone number"
      },
      "address": {
        "bsonType": "object",
        "required": ["street", "city"],
        "properties": {
          "street": {
            "bsonType": "string",
            "description": "must be a string and is required"
          },
          "city": {
            "bsonType": "string", 
            "description": "must be a string and is required"
          },
          "zipcode": {
            "bsonType": "string",
            "pattern": "^[0-9]{5}(-[0-9]{4})?$",
            "description": "must be a valid US zipcode"
          }
        }
      },
      "tags": {
        "bsonType": "array",
        "items": {
          "bsonType": "string"
        },
        "uniqueItems": true,
        "description": "must be an array of unique strings"
      }
    }
  }
}

// Validation levels:
// - strict: Apply validation rules to all inserts and updates
// - moderate: Apply validation rules to inserts and updates of valid documents

// Validation actions:
// - error: Reject documents that violate validation rules
// - warn: Log validation failures but allow the operation

Créateur de requêtes

Constructeur de requêtes visuelles

// Compass Query Builder provides GUI for:
// 1. Filter - Find documents matching criteria
// 2. Project - Select which fields to return
// 3. Sort - Order results
// 4. Skip - Skip number of documents
// 5. Limit - Limit number of results

// Filter examples:
// Simple equality
{"name": "John Doe"}

// Comparison operators
{"age": {"$gte": 18, "$lt": 65}}

// Text search
{"$text": {"$search": "mongodb database"}}

// Array operations
{"tags": {"$in": ["javascript", "nodejs"]}}

// Nested field queries
{"address.city": "New York"}

// Date range queries
{
  "created_at": {
    "$gte": {"$date": "2023-01-01T00:00:00Z"},
    "$lt": {"$date": "2024-01-01T00:00:00Z"}
  }
}

// Regular expressions
{"email": {"$regex": ".*@gmail\\.com$", "$options": "i"}}

// Logical operators
{
  "$and": [
    {"age": {"$gte": 18}},
    {"status": "active"}
  ]
}

{
  "$or": [
    {"category": "premium"},
    {"score": {"$gte": 90}}
  ]
}

Demandes de renseignements avancées

// Complex filter combinations
{
  "$and": [
    {
      "$or": [
        {"category": "electronics"},
        {"category": "computers"}
      ]
    },
    {"price": {"$gte": 100, "$lte": 1000}},
    {"in_stock": true},
    {
      "$or": [
        {"rating": {"$gte": 4.0}},
        {"reviews_count": {"$gte": 100}}
      ]
    }
  ]
}

// Array element matching
{
  "products": {
    "$elemMatch": {
      "name": "laptop",
      "price": {"$lt": 1000}
    }
  }
}

// Geospatial queries
{
  "location": {
    "$near": {
      "$geometry": {
        "type": "Point",
        "coordinates": [-73.9857, 40.7484]
      },
      "$maxDistance": 1000
    }
  }
}

// Projection examples:
// Include specific fields
{"name": 1, "email": 1, "age": 1}

// Exclude specific fields
{"password": 0, "internal_notes": 0}

// Array element projection
{"comments": {"$slice": 5}}  // First 5 elements
{"comments": {"$slice": -5}} // Last 5 elements
{"comments": {"$slice": [10, 5]}} // Skip 10, take 5

// Nested field projection
{"user.name": 1, "user.email": 1, "order.total": 1}

// Sort examples:
{"created_at": -1}  // Descending
{"name": 1, "age": -1}  // Name ascending, age descending
{"score": -1, "_id": 1}  // Score descending, _id ascending

Exécution des requêtes

// Query performance insights in Compass:
// 1. Execution time
// 2. Documents examined vs returned
// 3. Index usage
// 4. Query plan explanation

// Performance metrics displayed:
{
  "executionTimeMillis": 45,
  "totalDocsExamined": 1000,
  "totalDocsReturned": 25,
  "executionStages": {
    "stage": "IXSCAN",  // Index scan
    "indexName": "email_1",
    "keysExamined": 25,
    "docsExamined": 25
  }
}

// Query optimization tips:
// 1. Use indexes for filter fields
// 2. Limit result sets
// 3. Use projection to reduce data transfer
// 4. Avoid regex queries on large collections
// 5. Use compound indexes for multiple field queries

// Index recommendations:
// Compass suggests indexes based on query patterns
// Shows potential performance improvements
// Provides index creation commands

Historique des requêtes

// Compass maintains query history
// Access via History tab in query bar
// Features:
// - Recent queries list
// - Favorite queries
// - Query execution time
// - Results count
// - Export query history

// Query bookmarking:
// 1. Execute query
// 2. Click "Save" button
// 3. Provide query name
// 4. Access from "Saved Queries" dropdown

// Sharing queries:
// 1. Copy query JSON
// 2. Export as MongoDB shell command
// 3. Export as programming language code
// 4. Generate aggregation pipeline

Pipeline d'agrégation

Constructeur de pipelines

// Compass Aggregation Pipeline Builder provides:
// 1. Visual pipeline construction
// 2. Stage-by-stage result preview
// 3. Pipeline performance metrics
// 4. Export to various formats

// Common pipeline stages:

// $match - Filter documents
{
  "$match": {
    "status": "active",
    "created_at": {
      "$gte": {"$date": "2023-01-01T00:00:00Z"}
    }
  }
}

// $project - Select/transform fields
{
  "$project": {
    "name": 1,
    "email": 1,
    "age": 1,
    "full_name": {
      "$concat": ["$first_name", " ", "$last_name"]
    },
    "age_group": {
      "$switch": {
        "branches": [
          {"case": {"$lt": ["$age", 18]}, "then": "minor"},
          {"case": {"$lt": ["$age", 65]}, "then": "adult"},
          {"case": {"$gte": ["$age", 65]}, "then": "senior"}
        ],
        "default": "unknown"
      }
    }
  }
}

// $group - Group and aggregate
{
  "$group": {
    "_id": "$category",
    "total_sales": {"$sum": "$amount"},
    "avg_price": {"$avg": "$price"},
    "count": {"$sum": 1},
    "max_price": {"$max": "$price"},
    "min_price": {"$min": "$price"},
    "products": {"$push": "$name"}
  }
}

// $sort - Sort results
{
  "$sort": {
    "total_sales": -1,
    "category": 1
  }
}

// $limit - Limit results
{
  "$limit": 10
}

// $skip - Skip documents
{
  "$skip": 20
}

Opérations pipelinières avancées

// $lookup - Join collections
{
  "$lookup": {
    "from": "orders",
    "localField": "_id",
    "foreignField": "user_id",
    "as": "user_orders"
  }
}

// $unwind - Deconstruct arrays
{
  "$unwind": {
    "path": "$tags",
    "preserveNullAndEmptyArrays": true
  }
}

// $addFields - Add computed fields
{
  "$addFields": {
    "total_with_tax": {
      "$multiply": ["$total", 1.08]
    },
    "order_year": {
      "$year": "$order_date"
    },
    "is_premium": {
      "$gte": ["$total", 1000]
    }
  }
}

// $facet - Multiple pipelines
{
  "$facet": {
    "by_category": [
      {"$group": {"_id": "$category", "count": {"$sum": 1}}},
      {"$sort": {"count": -1}}
    ],
    "by_price_range": [
      {
        "$bucket": {
          "groupBy": "$price",
          "boundaries": [0, 100, 500, 1000, 5000],
          "default": "expensive",
          "output": {"count": {"$sum": 1}}
        }
      }
    ],
    "statistics": [
      {
        "$group": {
          "_id": null,
          "total_products": {"$sum": 1},
          "avg_price": {"$avg": "$price"},
          "total_value": {"$sum": "$price"}
        }
      }
    ]
  }
}

// $bucket - Group by ranges
{
  "$bucket": {
    "groupBy": "$age",
    "boundaries": [0, 18, 30, 50, 65, 100],
    "default": "other",
    "output": {
      "count": {"$sum": 1},
      "avg_income": {"$avg": "$income"},
      "users": {"$push": "$name"}
    }
  }
}

// $sample - Random sample
{
  "$sample": {
    "size": 100
  }
}

Agrégation des séries chronologiques

// Time-based grouping and analysis
{
  "$group": {
    "_id": {
      "year": {"$year": "$timestamp"},
      "month": {"$month": "$timestamp"},
      "day": {"$dayOfMonth": "$timestamp"}
    },
    "daily_sales": {"$sum": "$amount"},
    "transaction_count": {"$sum": 1},
    "avg_transaction": {"$avg": "$amount"}
  }
}

// Moving averages
{
  "$setWindowFields": {
    "sortBy": {"timestamp": 1},
    "output": {
      "moving_avg_7d": {
        "$avg": "$value",
        "window": {
          "range": [-6, 0],
          "unit": "day"
        }
      },
      "cumulative_sum": {
        "$sum": "$value",
        "window": {
          "documents": ["unbounded", "current"]
        }
      }
    }
  }
}

// Date truncation and grouping
{
  "$group": {
    "_id": {
      "$dateTrunc": {
        "date": "$timestamp",
        "unit": "hour"
      }
    },
    "hourly_count": {"$sum": 1},
    "hourly_avg": {"$avg": "$value"}
  }
}

Optimisation des pipelines

// Pipeline optimization tips:

// 1. Place $match early
[
  {"$match": {"status": "active"}},  // Filter first
  {"$lookup": {...}},                // Then join
  {"$group": {...}}                  // Then aggregate
]

// 2. Use $project to reduce data
[
  {"$match": {"category": "electronics"}},
  {"$project": {"name": 1, "price": 1, "rating": 1}},  // Only needed fields
  {"$group": {"_id": "$rating", "avg_price": {"$avg": "$price"}}}
]

// 3. Leverage indexes
[
  {"$match": {"created_at": {"$gte": new Date("2023-01-01")}}},  // Use date index
  {"$sort": {"created_at": -1}},                                 // Use same index
  {"$limit": 100}
]

// 4. Use $limit early when possible
[
  {"$match": {"featured": true}},
  {"$sort": {"priority": -1}},
  {"$limit": 10},                    // Limit early
  {"$lookup": {...}}                 // Expensive operations on fewer docs
]

// Pipeline performance metrics in Compass:
// - Execution time per stage
// - Documents processed per stage
// - Memory usage
// - Index usage
// - Optimization suggestions

Analyse du schéma

Schéma automatique Découverte

// Compass analyzes collection schema automatically
// Provides insights on:

// Field frequency and types
{
  "field_name": {
    "probability": 0.95,        // Field present in 95% of documents
    "types": [
      {
        "name": "String",
        "bsonType": "string",
        "probability": 0.8,      // 80% are strings
        "unique": 1234,          // 1234 unique values
        "samples": ["value1", "value2", "value3"]
      },
      {
        "name": "Number",
        "bsonType": "number",
        "probability": 0.2,      // 20% are numbers
        "min": 1,
        "max": 1000,
        "average": 250.5
      }
    ]
  }
}

// Nested document analysis
{
  "address": {
    "probability": 0.9,
    "types": [
      {
        "name": "Document",
        "bsonType": "object",
        "probability": 1.0
      }
    ],
    "fields": {
      "street": {
        "probability": 0.95,
        "types": [{"name": "String", "bsonType": "string"}]
      },
      "city": {
        "probability": 0.98,
        "types": [{"name": "String", "bsonType": "string"}]
      },
      "coordinates": {
        "probability": 0.3,
        "types": [{"name": "Array", "bsonType": "array"}]
      }
    }
  }
}

// Array field analysis
{
  "tags": {
    "probability": 0.7,
    "types": [
      {
        "name": "Array",
        "bsonType": "array",
        "probability": 1.0,
        "lengths": [0, 1, 2, 3, 4, 5, 6],
        "average_length": 2.3,
        "types": [
          {
            "name": "String",
            "bsonType": "string",
            "probability": 1.0
          }
        ]
      }
    ]
  }
}

Règles de validation du schéma

// Generate validation rules from schema
// Compass can suggest JSON Schema based on analysis

{
  "$jsonSchema": {
    "bsonType": "object",
    "required": ["name", "email", "created_at"],
    "properties": {
      "name": {
        "bsonType": "string",
        "minLength": 1,
        "maxLength": 100,
        "description": "User's full name"
      },
      "email": {
        "bsonType": "string",
        "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
        "description": "Valid email address"
      },
      "age": {
        "bsonType": "int",
        "minimum": 0,
        "maximum": 150,
        "description": "User's age in years"
      },
      "address": {
        "bsonType": "object",
        "required": ["street", "city"],
        "properties": {
          "street": {"bsonType": "string"},
          "city": {"bsonType": "string"},
          "zipcode": {
            "bsonType": "string",
            "pattern": "^[0-9]{5}(-[0-9]{4})?$"
          },
          "coordinates": {
            "bsonType": "array",
            "items": {"bsonType": "double"},
            "minItems": 2,
            "maxItems": 2
          }
        }
      },
      "tags": {
        "bsonType": "array",
        "items": {"bsonType": "string"},
        "uniqueItems": true,
        "maxItems": 10
      },
      "created_at": {
        "bsonType": "date",
        "description": "Account creation timestamp"
      },
      "last_login": {
        "bsonType": ["date", "null"],
        "description": "Last login timestamp"
      }
    }
  }
}

Analyse de la qualité des données

// Compass identifies data quality issues:

// 1. Missing required fields
{
  "field": "email",
  "issue": "missing",
  "affected_documents": 150,
  "percentage": 1.5
}

// 2. Type inconsistencies
{
  "field": "age",
  "issue": "mixed_types",
  "types_found": ["string", "number"],
  "affected_documents": 45,
  "percentage": 0.45
}

// 3. Invalid formats
{
  "field": "email",
  "issue": "invalid_format",
  "pattern_expected": "email",
  "affected_documents": 23,
  "percentage": 0.23
}

// 4. Outliers
{
  "field": "price",
  "issue": "outliers",
  "outlier_threshold": 3,  // Standard deviations
  "outlier_values": [999999, -100],
  "affected_documents": 12
}

// 5. Duplicate values in unique fields
{
  "field": "username",
  "issue": "duplicates",
  "duplicate_count": 5,
  "affected_documents": 10
}

// Data profiling summary
{
  "total_documents": 10000,
  "total_fields": 25,
  "data_quality_score": 0.92,  // 92% quality score
  "issues": [
    {"type": "missing_values", "count": 150},
    {"type": "type_inconsistency", "count": 45},
    {"type": "format_violations", "count": 23},
    {"type": "outliers", "count": 12},
    {"type": "duplicates", "count": 5}
  ]
}

Suivi de l'évolution du schéma

// Track schema changes over time
// Compass can compare schemas between time periods

{
  "schema_comparison": {
    "baseline_date": "2023-01-01",
    "current_date": "2023-12-01",
    "changes": [
      {
        "field": "phone_number",
        "change_type": "added",
        "first_seen": "2023-03-15",
        "adoption_rate": 0.85
      },
      {
        "field": "legacy_id",
        "change_type": "deprecated",
        "last_seen": "2023-06-30",
        "remaining_documents": 150
      },
      {
        "field": "status",
        "change_type": "type_changed",
        "old_type": "string",
        "new_type": "number",
        "migration_progress": 0.75
      }
    ]
  }
}

// Schema versioning recommendations
{
  "recommendations": [
    {
      "type": "cleanup",
      "description": "Remove deprecated field 'legacy_id'",
      "affected_documents": 150,
      "priority": "low"
    },
    {
      "type": "migration",
      "description": "Complete type migration for 'status' field",
      "affected_documents": 2500,
      "priority": "medium"
    },
    {
      "type": "validation",
      "description": "Add validation for new 'phone_number' field",
      "priority": "high"
    }
  ]
}

Gestion des index

Aperçu général

// Compass Index tab shows:
// - Existing indexes
// - Index usage statistics
// - Index size and performance
// - Index creation/deletion tools

// Index information displayed:
{
  "name": "email_1",
  "keys": {"email": 1},
  "size": "2.5 MB",
  "usage": {
    "ops": 15420,
    "since": "2023-01-01T00:00:00Z"
  },
  "properties": ["unique", "sparse"],
  "background": true,
  "partialFilterExpression": {"email": {"$exists": true}}
}

Création d'index

// Single field index
db.collection.createIndex({"email": 1})

// Compound index
db.collection.createIndex({"category": 1, "price": -1})

// Text index
db.collection.createIndex({"title": "text", "description": "text"})

// Geospatial index
db.collection.createIndex({"location": "2dsphere"})

// Partial index
db.collection.createIndex(
  {"email": 1},
  {
    "partialFilterExpression": {
      "email": {"$exists": true, "$ne": null}
    }
  }
)

// Unique index
db.collection.createIndex(
  {"username": 1},
  {"unique": true}
)

// Sparse index
db.collection.createIndex(
  {"phone": 1},
  {"sparse": true}
)

// TTL index (Time To Live)
db.collection.createIndex(
  {"expireAt": 1},
  {"expireAfterSeconds": 3600}  // 1 hour
)

// Background index creation
db.collection.createIndex(
  {"large_field": 1},
  {"background": true}
)

// Index with collation
db.collection.createIndex(
  {"name": 1},
  {
    "collation": {
      "locale": "en",
      "strength": 2,
      "caseLevel": false
    }
  }
)

Analyse des performances de l'indice

// Index usage statistics
db.collection.aggregate([
  {"$indexStats": {}}
])

// Query performance with explain
db.collection.find({"email": "user@example.com"}).explain("executionStats")

// Index effectiveness metrics:
{
  "executionStats": {
    "totalDocsExamined": 1,      // Good: examined = returned
    "totalDocsReturned": 1,
    "executionTimeMillis": 2,    // Fast execution
    "indexesUsed": ["email_1"],  // Index was used
    "stage": "IXSCAN"           // Index scan (not COLLSCAN)
  }
}

// Poor index performance indicators:
{
  "executionStats": {
    "totalDocsExamined": 10000,  // Bad: examined >> returned
    "totalDocsReturned": 1,
    "executionTimeMillis": 150,  // Slow execution
    "stage": "COLLSCAN"         // Collection scan (no index)
  }
}

// Index hit ratio
{
  "index_stats": {
    "email_1": {
      "accesses": {
        "ops": 15420,
        "since": "2023-01-01T00:00:00Z"
      }
    },
    "unused_index": {
      "accesses": {
        "ops": 0,
        "since": "2023-01-01T00:00:00Z"
      }
    }
  }
}

Optimisation de l'indice

// Index recommendations from Compass:

// 1. Missing indexes for common queries
{
  "recommendation": "create_index",
  "query_pattern": {"status": 1, "created_at": -1},
  "suggested_index": {"status": 1, "created_at": -1},
  "estimated_improvement": "95% faster",
  "query_frequency": 1500  // queries per hour
}

// 2. Unused indexes
{
  "recommendation": "drop_index",
  "index_name": "old_compound_index",
  "last_used": "2023-06-15T10:30:00Z",
  "size": "15 MB",
  "maintenance_cost": "high"
}

// 3. Redundant indexes
{
  "recommendation": "consolidate_indexes",
  "redundant_indexes": [
    {"name": "name_1", "keys": {"name": 1}},
    {"name": "name_email_1", "keys": {"name": 1, "email": 1}}
  ],
  "suggested_action": "Drop name_1 (covered by name_email_1)"
}

// 4. Index order optimization
{
  "recommendation": "reorder_index",
  "current_index": {"category": 1, "price": 1},
  "suggested_index": {"price": 1, "category": 1},
  "reason": "Price has higher selectivity"
}

// Index maintenance commands:

// Rebuild index
db.collection.reIndex()

// Get index build progress
db.currentOp({"command.createIndexes": {"$exists": true}})

// Kill index build
db.killOp(operationId)

// Compact collection (rebuilds indexes)
db.runCommand({"compact": "collection"})

Surveillance de la performance

Mesure des performances en temps réel

// Compass Performance tab shows:
// 1. Operations per second
// 2. Query execution times
// 3. Index usage
// 4. Memory usage
// 5. Connection statistics
// 6. Slow operations

// Performance metrics displayed:
{
  "operations": {
    "insert": 150,     // ops/sec
    "query": 1200,
    "update": 80,
    "delete": 20,
    "getmore": 300,
    "command": 50
  },
  "timing": {
    "avg_query_time": 15,    // milliseconds
    "avg_insert_time": 5,
    "avg_update_time": 12,
    "slow_operations": 3     // operations > 100ms
  },
  "memory": {
    "resident": "2.1 GB",
    "virtual": "4.8 GB",
    "mapped": "1.9 GB"
  },
  "connections": {
    "current": 45,
    "available": 955,
    "total_created": 1250
  }
}

Surveillance lente des opérations

// Configure slow operation threshold
db.setProfilingLevel(1, {slowms: 100})  // Log operations > 100ms

// View slow operations
db.system.profile.find().sort({ts: -1}).limit(10)

// Slow operation analysis in Compass:
{
  "slow_operations": [
    {
      "timestamp": "2023-12-01T10:30:00Z",
      "duration_ms": 250,
      "operation": "find",
      "collection": "products",
      "query": {"category": "electronics", "price": {"$gte": 100}},
      "docs_examined": 15000,
      "docs_returned": 150,
      "index_used": false,
      "recommendation": "Create index on {category: 1, price: 1}"
    },
    {
      "timestamp": "2023-12-01T10:25:00Z", 
      "duration_ms": 180,
      "operation": "aggregate",
      "collection": "orders",
      "pipeline": [
        {"$match": {"status": "completed"}},
        {"$group": {"_id": "$customer_id", "total": {"$sum": "$amount"}}}
      ],
      "docs_examined": 50000,
      "docs_returned": 1200,
      "recommendation": "Add index on status field"
    }
  ]
}

// Performance profiling levels:
// 0: Off (default)
// 1: Log slow operations only
// 2: Log all operations

// Enable profiling for specific collection
db.setProfilingLevel(2, {
  slowms: 50,
  sampleRate: 0.1,  // Sample 10% of operations
  filter: {
    "command.find": {"$exists": true}  // Only find operations
  }
})

Utilisation des ressources

// Server status information
db.serverStatus()

// Key metrics to monitor:
{
  "opcounters": {
    "insert": 1000000,
    "query": 5000000,
    "update": 800000,
    "delete": 200000,
    "getmore": 1500000,
    "command": 3000000
  },
  "opcountersRepl": {
    "insert": 1000000,
    "query": 0,
    "update": 800000,
    "delete": 200000,
    "getmore": 0,
    "command": 500000
  },
  "mem": {
    "bits": 64,
    "resident": 2048,      // MB
    "virtual": 4096,
    "supported": true,
    "mapped": 1024,
    "mappedWithJournal": 2048
  },
  "connections": {
    "current": 45,
    "available": 955,
    "totalCreated": 1250
  },
  "network": {
    "bytesIn": 1073741824,     // 1GB
    "bytesOut": 2147483648,    // 2GB
    "numRequests": 1000000
  },
  "locks": {
    "Global": {
      "acquireCount": {
        "r": 5000000,
        "w": 1000000
      },
      "acquireWaitCount": {
        "r": 100,
        "w": 50
      }
    }
  }
}

// WiredTiger storage engine stats
{
  "wiredTiger": {
    "cache": {
      "maximum bytes configured": 1073741824,  // 1GB
      "bytes currently in the cache": 536870912,  // 512MB
      "pages evicted by application threads": 1000,
      "cache hit ratio": 0.95  // 95% hit rate
    },
    "block-manager": {
      "blocks read": 100000,
      "blocks written": 50000
    }
  }
}

Alertes de performance

// Configure performance alerts in Compass
// (Available in MongoDB Atlas integration)

{
  "alerts": [
    {
      "type": "slow_queries",
      "threshold": 1000,  // milliseconds
      "enabled": true,
      "notification": "email"
    },
    {
      "type": "high_cpu",
      "threshold": 80,    // percentage
      "enabled": true,
      "notification": "slack"
    },
    {
      "type": "memory_usage",
      "threshold": 90,    // percentage
      "enabled": true,
      "notification": "email"
    },
    {
      "type": "connection_limit",
      "threshold": 90,    // percentage of max
      "enabled": true,
      "notification": "pagerduty"
    },
    {
      "type": "replication_lag",
      "threshold": 10,    // seconds
      "enabled": true,
      "notification": "email"
    }
  ]
}

// Custom performance monitoring queries
// Monitor collection growth
db.stats()

// Monitor index usage
db.collection.aggregate([{"$indexStats": {}}])

// Monitor operation latency
db.runCommand({serverStatus: 1}).opLatencies

// Monitor lock contention
db.serverStatus().locks

// Monitor replication lag (for replica sets)
rs.printSlaveReplicationInfo()

Importation/Exportation

Importation de données

// Import formats supported by Compass:
// 1. JSON (MongoDB Extended JSON)
// 2. CSV
// 3. TSV (Tab-separated values)

// JSON import example:
[
  {
    "_id": {"$oid": "507f1f77bcf86cd799439011"},
    "name": "John Doe",
    "email": "john@example.com",
    "age": 30,
    "created_at": {"$date": "2023-12-01T10:30:00Z"}
  },
  {
    "_id": {"$oid": "507f1f77bcf86cd799439012"},
    "name": "Jane Smith", 
    "email": "jane@example.com",
    "age": 25,
    "created_at": {"$date": "2023-12-01T11:00:00Z"}
  }
]

// CSV import configuration:
{
  "delimiter": ",",           // Field delimiter
  "newline": "\n",           // Line delimiter
  "quote": "\"",             // Quote character
  "escape": "\\",            // Escape character
  "header": true,            // First row contains headers
  "skipEmptyLines": true,    // Skip empty lines
  "encoding": "utf8",        // File encoding
  "typeInference": true,     // Auto-detect data types
  "fieldMapping": {          // Map CSV columns to MongoDB fields
    "user_name": "name",
    "user_email": "email",
    "user_age": "age"
  },
  "typeMapping": {           // Specify field types
    "age": "number",
    "created_at": "date",
    "is_active": "boolean"
  }
}

// Import options:
{
  "stopOnError": false,      // Continue on import errors
  "ordered": false,          // Unordered bulk insert (faster)
  "upsert": false,          // Update existing documents
  "upsertFields": ["email"], // Fields to match for upsert
  "batchSize": 1000,        // Documents per batch
  "numInsertionWorkers": 1,  // Parallel workers
  "writeConcern": {
    "w": 1,                 // Write concern
    "j": true               // Journal acknowledgment
  }
}

Exportation de données

// Export formats supported by Compass:
// 1. JSON (MongoDB Extended JSON)
// 2. CSV

// Export configuration:
{
  "format": "json",          // json or csv
  "query": {                 // Filter documents to export
    "status": "active",
    "created_at": {
      "$gte": {"$date": "2023-01-01T00:00:00Z"}
    }
  },
  "projection": {            // Select fields to export
    "name": 1,
    "email": 1,
    "created_at": 1,
    "_id": 0
  },
  "sort": {                  // Sort exported documents
    "created_at": -1
  },
  "limit": 10000,           // Limit number of documents
  "skip": 0,                // Skip documents
  "includeSystemFields": false,  // Include _id and other system fields
  "jsonFormat": "canonical"  // canonical, relaxed, or legacy
}

// CSV export options:
{
  "format": "csv",
  "delimiter": ",",
  "quote": "\"",
  "escape": "\\",
  "header": true,           // Include column headers
  "flattenArrays": true,    // Convert arrays to comma-separated strings
  "flattenObjects": true,   // Flatten nested objects
  "dateFormat": "iso",      // iso, epoch, or custom format
  "nullValue": "",          // How to represent null values
  "undefinedValue": "",     // How to represent undefined values
  "maxDepth": 3            // Maximum nesting depth for flattening
}

// Flattened object example:
// Original: {"user": {"name": "John", "address": {"city": "NYC"}}}
// Flattened: {"user.name": "John", "user.address.city": "NYC"}

// Array flattening example:
// Original: {"tags": ["mongodb", "database", "nosql"]}
// Flattened: {"tags": "mongodb,database,nosql"}

Opérations en vrac

// Bulk import strategies:

// 1. Large file import
{
  "strategy": "streaming",
  "batchSize": 1000,
  "parallelWorkers": 4,
  "memoryLimit": "512MB",
  "tempDirectory": "/tmp/compass-import"
}

// 2. Error handling
{
  "errorHandling": {
    "stopOnError": false,
    "logErrors": true,
    "errorLogFile": "/tmp/import-errors.log",
    "maxErrors": 100,
    "skipDuplicates": true
  }
}

// 3. Performance optimization
{
  "performance": {
    "ordered": false,        // Faster unordered inserts
    "bypassValidation": true, // Skip document validation
    "writeConcern": {
      "w": 1,               // Minimal write concern
      "j": false            // No journal sync
    }
  }
}

// 4. Data transformation during import
{
  "transformations": [
    {
      "field": "created_at",
      "type": "date",
      "format": "YYYY-MM-DD HH:mm:ss"
    },
    {
      "field": "price",
      "type": "number",
      "scale": 2
    },
    {
      "field": "tags",
      "type": "array",
      "delimiter": "|"
    }
  ]
}

// Progress monitoring during import/export:
{
  "progress": {
    "totalDocuments": 100000,
    "processedDocuments": 25000,
    "percentage": 25,
    "estimatedTimeRemaining": "00:03:45",
    "documentsPerSecond": 1000,
    "errorsEncountered": 5
  }
}

Outils de migration

// Database migration using Compass:

// 1. Export from source
{
  "source": {
    "connection": "mongodb://source-server:27017",
    "database": "source_db",
    "collections": ["users", "orders", "products"]
  },
  "export": {
    "format": "json",
    "includeIndexes": true,
    "includeCollectionOptions": true,
    "batchSize": 1000
  }
}

// 2. Transform data (if needed)
{
  "transformations": [
    {
      "collection": "users",
      "operations": [
        {"rename": {"old_field": "new_field"}},
        {"remove": ["deprecated_field"]},
        {"convert": {"date_string": {"to": "date", "format": "YYYY-MM-DD"}}}
      ]
    }
  ]
}

// 3. Import to destination
{
  "destination": {
    "connection": "mongodb://dest-server:27017",
    "database": "dest_db",
    "createCollections": true,
    "createIndexes": true
  },
  "import": {
    "mode": "insert",        // insert, upsert, replace
    "batchSize": 1000,
    "ordered": false
  }
}

// Migration validation:
{
  "validation": {
    "compareDocumentCounts": true,
    "compareSampleDocuments": 100,
    "validateIndexes": true,
    "validateCollectionOptions": true,
    "generateReport": true
  }
}

Meilleures pratiques

Meilleures pratiques en matière de rendement

// 1. Query optimization
// Use indexes for frequently queried fields
db.collection.createIndex({"status": 1, "created_at": -1})

// Use projection to limit returned fields
db.collection.find(
  {"status": "active"},
  {"name": 1, "email": 1, "_id": 0}
)

// Use limit for large result sets
db.collection.find({"category": "electronics"}).limit(100)

// 2. Index strategy
// Create compound indexes for multi-field queries
db.collection.createIndex({"category": 1, "price": -1, "rating": -1})

// Use partial indexes for sparse data
db.collection.createIndex(
  {"premium_features": 1},
  {"partialFilterExpression": {"premium_features": {"$exists": true}}}
)

// 3. Aggregation optimization
// Place $match early in pipeline
[
  {"$match": {"status": "active"}},     // Filter first
  {"$lookup": {...}},                   // Then join
  {"$group": {...}}                     // Then aggregate
]

// Use $project to reduce data size
[
  {"$match": {"category": "electronics"}},
  {"$project": {"name": 1, "price": 1}},  // Only needed fields
  {"$group": {"_id": "$brand", "avg_price": {"$avg": "$price"}}}
]

// 4. Connection management
// Use connection pooling
{
  "maxPoolSize": 10,
  "minPoolSize": 2,
  "maxIdleTimeMS": 30000,
  "waitQueueTimeoutMS": 5000
}

// 5. Memory usage optimization
// Monitor working set size
// Keep frequently accessed data in memory
// Use appropriate data types (int32 vs int64)

Pratiques exemplaires en matière de sécurité

// 1. Authentication and authorization
// Use strong passwords
// Enable authentication
// Create application-specific users with minimal privileges

// 2. Network security
// Use SSL/TLS for connections
{
  "ssl": true,
  "sslValidate": true,
  "sslCA": "/path/to/ca.pem",
  "sslCert": "/path/to/client.pem"
}

// Restrict network access
// Use VPN or private networks
// Configure firewall rules

// 3. Data encryption
// Enable encryption at rest
// Use field-level encryption for sensitive data
// Encrypt backups

// 4. Audit logging
// Enable audit logging
{
  "auditLog": {
    "destination": "file",
    "format": "JSON",
    "path": "/var/log/mongodb/audit.log",
    "filter": {
      "atype": {"$in": ["authenticate", "authCheck", "createUser", "dropUser"]}
    }
  }
}

// 5. Regular security updates
// Keep MongoDB and Compass updated
// Monitor security advisories
// Implement security patches promptly

Modélisation des données Meilleures pratiques

// 1. Document structure design
// Embed related data that is accessed together
{
  "_id": ObjectId("..."),
  "user": {
    "name": "John Doe",
    "email": "john@example.com",
    "address": {
      "street": "123 Main St",
      "city": "New York",
      "zipcode": "10001"
    }
  },
  "orders": [
    {
      "order_id": "ORD-001",
      "date": ISODate("2023-12-01"),
      "total": 99.99
    }
  ]
}

// 2. Reference related data that changes frequently
{
  "_id": ObjectId("..."),
  "user_id": ObjectId("..."),  // Reference to users collection
  "product_id": ObjectId("..."), // Reference to products collection
  "quantity": 2,
  "price": 49.99,
  "order_date": ISODate("2023-12-01")
}

// 3. Use appropriate data types
{
  "price": NumberDecimal("99.99"),    // Use Decimal128 for currency
  "quantity": NumberInt(5),           // Use Int32 for small integers
  "timestamp": ISODate("2023-12-01"), // Use Date for timestamps
  "is_active": true,                  // Use Boolean for flags
  "tags": ["mongodb", "database"]     // Use Arrays for lists
}

// 4. Avoid deep nesting (limit to 3-4 levels)
// Good:
{
  "user": {
    "profile": {
      "preferences": {
        "theme": "dark"
      }
    }
  }
}

// Bad (too deep):
{
  "level1": {
    "level2": {
      "level3": {
        "level4": {
          "level5": "value"
        }
      }
    }
  }
}

// 5. Consider document size limits (16MB max)
// Use GridFS for large files
// Split large documents into smaller ones
// Use references for large arrays

Surveillance et entretien

// 1. Regular monitoring
// Monitor key metrics:
// - Query performance
// - Index usage
// - Memory usage
// - Disk space
// - Connection count

// 2. Index maintenance
// Review index usage regularly
db.collection.aggregate([{"$indexStats": {}}])

// Drop unused indexes
db.collection.dropIndex("unused_index")

// Rebuild indexes if needed
db.collection.reIndex()

// 3. Performance profiling
// Enable profiling for slow operations
db.setProfilingLevel(1, {slowms: 100})

// Analyze slow operations
db.system.profile.find().sort({ts: -1}).limit(10)

// 4. Backup and recovery
// Regular automated backups
// Test restore procedures
// Monitor backup completion
// Verify backup integrity

// 5. Capacity planning
// Monitor data growth trends
// Plan for storage expansion
// Monitor query patterns
// Scale resources proactively

// 6. Health checks
// Monitor replica set status
rs.status()

// Check database integrity
db.runCommand({validate: "collection"})

// Monitor server status
db.serverStatus()

// Check for errors in logs
// Review MongoDB logs regularly
// Set up log monitoring and alerting

Résumé

MangoDB Compass est une interface graphique puissante qui rend l'administration et le développement de la base de données MongoDB plus accessibles et plus efficaces. Cette feuille de triche offre une couverture complète des fonctionnalités Compass des opérations de base à l'administration avancée.

Principales forces: - Interface visuelle: GUI intuitif pour Mongo Opérations DB - Analyse du schéma: découverte et validation automatiques du schéma - Constructeur de requêtes: Construction de requêtes visuelles avec aperçus de performance - Agrégation Pipeline: Constructeur de pipeline visuel avec prévisualisation étape par étape - Surveillance du rendement: mesures en temps réel et analyse lente des opérations - Gestion des indices: Outils de création et d'optimisation d'index visuel

Cas de la meilleure utilisation: - Mongo Exploration et administration de la base de données DB - Développement et optimisation des requêtes - Analyse et validation du schéma - Surveillance des performances et dépannage - Opérations d'importation/exportation de données - Environnements de développement et d'essai

** Considérations importantes :** - La performance peut être affectée par de très grandes collections - Certaines fonctionnalités avancées nécessitent MongoDB Intégration du système Atlas - Des mises à jour régulières sont recommandées pour les dernières fonctionnalités - Une sécurité de connexion appropriée doit être configurée - Les recommandations de l'index devraient être validées avant la mise en œuvre

En suivant les pratiques et techniques décrites dans cette feuille de triche, vous pouvez utiliser efficacement MongoDB Compass pour gérer Mongo Les bases de données DB tout en maintenant la performance, la sécurité et l'intégrité des données dans vos opérations de base de données.