![](/img/trans.png)
[英]Count tags of a specific type in an html document and display it as a number using javascript
[英]count number of tags in a couchdb document
我有一個簡單的文檔設計,就像這樣......
{
_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"
但那只是給了我......
{“rows”:[{“key”:null,“value”:20}]}
有誰知道如何做到這一點? 我很困惑。 謝謝。
您的視圖中包含無效的javascript。
var i = 0; l doc.tags
l應該分配給doc.tags.length
查看固定版本:
{
"_id": "_design/tagcount",
"_rev": "1-d14c9d06e652ad23df1044ffbca9d85f",
"views": {
"count": {
"reduce": "_count",
"map": "function (doc) {\n if(doc.type== 'post') { \n for(var i = 0, l= doc.tags.length; i < l; i++) { emit(doc.tags[i], 1); } } \n}"
}
},
"language": "javascript"
}
此外,您需要指定reduce:
http://localhost:5984/<db>/_design/<design>/_view/<viem>?limit=20&reduce=true&group=true
注意:將<value>替換為實際值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.