[英]How to get a "fieldcount" (like wordcount) on CouchDB/Cloudant?
Trying to get a count of fields, just like the classic word count example.尝试获取字段计数,就像经典的字数统计示例一样。 I thought this would be trivial...
我以为这将是微不足道的...
but I got this useless result...但我得到了这个无用的结果......
{"rows":[
{"key":null,"value":212785214}
]}
How can I get what I wanted... an inventory of all fields used in my documents, with a count of how many times they occur?我怎样才能得到我想要的……我的文档中使用的所有字段的清单,以及它们出现的次数?
To get a count of each key in your document, create a "map" function like this:要获取文档中每个键的计数,请创建一个“map”函数,如下所示:
function (doc) {
var keys = Object.keys(doc);
for(var i in keys) {
emit(keys[i], null);
}
}
and use the built in _count
reducer.并使用内置的
_count
减速器。
You can then retrieve the grouped answer by accessing the view like so:然后,您可以通过访问视图来检索分组的答案,如下所示:
/mydb/_design/<designdocname>/_view/<viewname>?group_level=1
which will produce grouped keys and counts like this:这将产生分组的键和计数,如下所示:
{"rows":[
{"key":"_id","value":4},
{"key":"_rev","value":4},
{"key":"a","value":3},
{"key":"c","value":1}
]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.