且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CouchDB中编写SELECT COUNT(DISTINCT字段)查询?

更新时间:2023-02-04 16:56:09

此答案由Zachary Zolton在沙发上的邮件列表中提供:

This answer was provided by Zachary Zolton on the couchdb mailing list:

http:// mail-archives。 apache.org/mod_mbox/couchdb-user/201111.mbox/%3CCAGnHtbJ-1-YeLWMLivKzWub98HZY7%2BesnPOHU4pEYgWAsxaszA%40mail.gmail.com%3E

开始您已经有了一个视图,可以为您提供鲍里斯(Boris)的5万首独特的
歌曲,您可以使用_list函数返回行数。

Since you've already got a view that'll give you Boris's 50k unique songs, you could use a _list function to return the number of rows.

像这样应该可以解决问题:

Something like this should do the trick:

function() {
 var count = 0;
 while(getRow()) count++;
 return JSON.stringify({count: count});
}

如果使用相同的视图,键范围和$查询此列表函数b $ b组级别,它将仅使用一些JSON进行响应,例如: { count: 50612}

If you query this list function, with the same view, key range and group level, it'll just respond with a bit of JSON, such as: {"count":"50612"}

您可以在此处阅读更多内容:

You can read up more here:

  • http://guide.couchdb.org/draft/transforming.html
  • http://wiki.apache.org/couchdb/Formatting_with_Show_and_List