文档库:版本字段与渐进迁移
文档库:版本字段与渐进迁移
// 文档带 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 重写历史分区;列类型修改要核对版本支持的转换范围。