如何在 CouchDB 中使用 SUM() 加入?

How to JOIN with SUM() in CouchDB?

我想使用 CouchDB 和链接文档制作以下样式 table 输出:

+----------------+------------+--------------------+
|     Title      |    Date    | Number of Comments |
+----------------+------------+--------------------+
| Hello World    | 2000-01-01 |                301 |
| Next Question? | 1999-03-04 |                 11 |
| Final Post     | 1992-04-01 |                 64 |
+----------------+------------+--------------------+

我有类似这样的帖子文档:

{ _id : 'hello-world', title : 'Hello World', date : '2000-01-01', type : 'post' }

以及评论:

{ _id : 'some-comment', title : 'Great post!', postid: 'hello-world', type : 'comment' }
{ _id : 'some-comment2', title : 'Poor quality', postid: 'final-post', type : 'comment' }

我怎样才能做到这一点?我更愿意使用单个 map/reduce.

我会亲自回答这个问题,以防其他人有这个问题。我想我的误解是因为我习惯了SQL.

基本上,我的选择是:

  1. 创建一个设计文档视图 (comments-by-post),它会给我 post._id 作为键和 post 的评论数。然后,我可以将其与通过 Mango 查询或我的编程语言中的其他视图获得的 posts 拼接在一起。
  2. 或者,并且可能更正确,我可以在 post 对象上创建一个名为 numberOfComments 的 属性 并每隔有人评论的时间。从 SQL 的角度来看,这感觉不对,但我相信文档数据库的灵活性有助于实现这一点。

我选择做#1 开始并尝试#2。