无 JOIN 的世界:博客数据建模规约
1 分钟2 条评论
Torchwood DocumentDB 没有外键,也没有跨集合 JOIN。本站的 categories 1:N posts、posts M:N tags 用两个原语表达:
1:N:引用属性 + key 索引#
posts 上放 category_id(string,required),建 by_category key 索引。"某分类下的文章"两段式:先按 slug 查分类拿 id,再 eq("category_id", id)。
M:N:数组属性(GIN 自动)#
posts 上放 tag_ids(string,array=true)。数组列的 GIN 索引由服务端自动创建——查询用 containsAny(交集非空)/containsAll(子集)。
contains 是 ILIKE 模糊匹配,不能用于数组成员判断。
删除协议#
服务端不会级联。删父文档前三步:
countDocuments检查引用数;- 迁移 / 级联 / 拒绝,处置子文档;
- 删除父文档(带 OCC 版本)。
本站删除分类时执行的就是这个协议。