【发布时间】:2017-06-12 17:50:13
【问题描述】:
我有一个类似这样的简单文档设计...
{
_id: "something",
type: "post",
title: "A Title",
content: "A Blog Content",
tags: ["newtag", "oldtag", "bluetag", "youtag"]
}
所以每个文档都会有一个“标签”数组。我想做的是创建一个视图来计算标签的使用次数。所以它会搜索所有文档,发出标签,然后按键添加它们。示例...
Doc1
{
_id: "something",
type: "post",
title: "A title",
content: "Blog Content",
tags: ["oldtag", "newtag", "bluetag", "youtag"]
}
Doc2
{
_id: "domethin",
type: "post",
title: "another title",
content: "another post",
tags: ["oldtag", "notag", "whytag"]
}
鉴于我需要具有 "oldtag" : 2, "notag": 1, "whytag": 1, ...
但我似乎无法弄清楚。我尝试使用....
"map": "function(doc) {if(doc.type == 'post') { for(var i = 0, l doc.tags.length; i < l; i++) { emit(doc.tags[i], 1); } } }",
"reduce": "_count"
但这只是给了我......
{“行”:[ {“键”:空,“值”:20} ]}
有人知道怎么做吗?我很困惑。谢谢。
【问题讨论】:
标签: javascript mapreduce couchdb