couchdb 映射减少和分组
我正在尝试获取某个对象(比如视频)的唯一事件计数:
这是我的文档:
{
"type":"View",
"video_id": "12300",
"user_id": 3
}
{
"type":"View",
"video_id": "12300",
"user_id": 1
}
{
"type":"View",
"video_id": "45600",
"user_id": 3
}
我正在尝试获取每个视频的唯一(按 user_id
)观看次数
我假设我想像这样映射我的数据:
function(doc) {
if (doc.type === 'View') {
emit([doc.video_id, doc.user_id], 1);
}
},
但我不明白如何将其减少到每个视频的唯一用户,或者我是否会做错。
I am attempting to get a count of unique events for an object (lets say a video):
Here are my documents:
{
"type":"View",
"video_id": "12300",
"user_id": 3
}
{
"type":"View",
"video_id": "12300",
"user_id": 1
}
{
"type":"View",
"video_id": "45600",
"user_id": 3
}
I'm trying to get a unique (by user_id
) count of views for each video
I assume I want to map my data like so:
function(doc) {
if (doc.type === 'View') {
emit([doc.video_id, doc.user_id], 1);
}
},
But I don't understand how to reduce it down to unique users per video, or am I going about this wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该查看
group_level
视图参数。它将允许您更改分组发生的字段。通过使用
group_level = 1
,在本例中它将按video_id
分组。使用group_level = 2
,它将根据video_id
和user_id
进行分组。You should look at the
group_level
view parameter. It will allow you to change what field(s) the grouping occurs on.By using
group_level = 1
, in this case it will group byvideo_id
. Usinggroup_level = 2
, it will group on bothvideo_id
anduser_id
.在请求 URL 后添加 ?group=true。将相同的键组合在一起作为归约函数的输入:
应该可以了。
请注意,键和值是键及其值的解压缩列表。通过分组,对于每个reduce调用,按键都是相同的。
Add ?group=true after the request URL. That groups identical keys together as input for the reduce function:
That should do it.
Note that keys and values are unzipped lists of keys and their values. With grouping on the keys are all identical for each call of the reduce.