也许你做错了......你正试图以非 CouchDB 的方式解决 CouchDB 中的问题。为什么要将所有 cmets 存储在一个文档中?如果您想通过帖子在一个请求中检索它们,那么您可以使用 map 从单独的文档中聚合帖子和 cmets,如下所示:
发布文件:{ _id: "post1", type: "post", ... }
评论文件:
{ type: "comment",
post: "post1", // id of the post being commenting
...
}
地图:
function (self) {
if (!self.type) return;
if (self.type == "post") {
emit([self._id, 0], self);
} else if (self.type == "comment" && self.post) {
emit([self.post, 1, self.time], self);
}
}
使用按日期排序的 cmets 检索帖子:
curl http://127.0.0.1:5984/yourdb/_design/yourapp/_view/yourview?startkey=[%22post1%22]&endkey=[%22post1%22,{}]
通过这种方式,您还可以只检索部分 cmets 并轻松实现分页。所有的 cmets 都应该存储在靠近 post 的 B-tree 中,所以它也应该是高效的。
同样的方法在here有详细描述。