首页/数据/NoSQL 速查/模式演进:无模式不等于免治理
本包目录(322 条)
MongoDB99 条Elasticsearch89 条ClickHouse56 条时序与列式28 条
选型与架构50 条

模式演进:无模式不等于免治理

文档库与搜索库放宽了建表约束,但数据形态依然随业务演进:新增字段、改类型、拆合字段都要有迁移计划。通行做法是版本字段加双写回填再切读,搜索库受映射限制还要走重建索引加别名切换。

NoSQL 速查数据更新于 2026-10-12

文档库:版本字段与渐进迁移

文档库:版本字段与渐进迁移
// 文档带 schemaVersion,读取端兼容新旧两种形态
{ _id: 1, schemaVersion: 2, name: "ann", profile: { city: "hz" } }

// 步骤:双写新字段 → 后台回填存量 → 切读新字段 → 稳定后清理旧字段
db.users.updateMany(
  { schemaVersion: 1 },
  { $set: { profile: { city: "$legacyCity" }, schemaVersion: 2 } }
)

搜索库:映射变更走重建

搜索库:映射变更走重建
# 新字段类型无法原地修改:建新索引 → reindex → 别名切换
curl -X PUT "localhost:9200/users_v2" -H 'Content-Type: application/json' \
  -d '{"mappings":{"properties":{"profile":{"properties":{"city":{"type":"keyword"}}}}}}'

curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' \
  -d '{"source":{"index":"users_v1"},"dest":{"index":"users_v2"}}'

curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' \
  -d '{"actions":[{"remove":{"index":"users_v1","alias":"users"}},{"add":{"index":"users_v2","alias":"users"}}]}'
ClickHouse 侧新增列可先带 DEFAULT 上线,再按需执行 MATERIALIZE COLUMN 重写历史分区;列类型修改要核对版本支持的转换范围。

易错点

  • 直接改搜索库映射字段类型会报错,缺少重建与别名流程就会卡在变更窗口
  • 一次性全量回填大集合不限速,回填流量会和线上写入抢占资源

数据来源与参考

  • MongoDB Manual: Schema versioning
  • Elasticsearch Guide: Mapping
  • ClickHouse Docs: ALTER TABLE
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌