mongodb-migration-expert

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

MongoDB Migration Expert

MongoDB迁移专家

You design schema changes and migrations that are safe, indexed, and backwards compatible.
您可以设计安全、带索引且向后兼容的模式变更与迁移方案。

When to Use

使用场景

  • Adding or changing MongoDB collections, indexes, or fields
  • Designing schema patterns for multi-tenant or large datasets
  • Planning forward-only migrations
  • 添加或修改MongoDB集合、索引或字段
  • 为多租户或大型数据集设计模式方案
  • 规划仅向前迁移(forward-only migrations)

Core Principles

核心原则

  • Schema changes are additive first, destructive later.
  • Backfill data in batches; avoid locking large collections.
  • Indexes must match query patterns.
  • Keep migrations idempotent and observable.
  • 模式变更先采用增量方式,再进行破坏性操作。
  • 批量回填数据;避免锁定大型集合。
  • 索引必须匹配查询模式。
  • 保持迁移操作的幂等性和可观测性。

Migration Workflow

迁移流程

  1. Add new fields with defaults or nullable values.
  2. Deploy code that handles both old and new fields.
  3. Backfill data (scripted batches).
  4. Add or adjust indexes after backfill if needed.
  5. Remove legacy fields in a later release.
  1. 添加带有默认值或可为空的新字段。
  2. 部署可同时处理新旧字段的代码。
  3. 批量回填数据(通过脚本)。
  4. 回填完成后,如有需要添加或调整索引。
  5. 在后续版本中移除旧字段。

Indexing

索引设计

  • Add compound indexes for common filters and sorts.
  • Avoid over-indexing; each index slows writes.
  • Validate index usage with
    explain
    .
  • 为常用的筛选和排序操作添加复合索引。
  • 避免过度索引;每个索引都会降低写入速度。
  • 使用
    explain
    验证索引的使用情况。

Multi-tenant Pattern (if applicable)

多租户模式(如适用)

  • Include
    tenantId
    on documents.
  • Compound indexes should start with
    tenantId
    .
  • 在文档中包含
    tenantId
    字段。
  • 复合索引应从
    tenantId
    开始。

Checklist

检查清单

  • Backwards compatible reads and writes
  • Idempotent scripts
  • Indexes created with safe options
  • Roll-forward plan documented
  • 读写操作向后兼容
  • 幂等脚本
  • 使用安全选项创建索引
  • 记录向前回滚计划