选择不同数量的cloudant/couchdb [英] Select distinct count cloudant/couchdb

查看:93
本文介绍了选择不同数量的cloudant/couchdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cloudant启动一个项目. 这是一个简单的日志记录系统,因此我可以跟踪应用程序的使用情况.

I am starting a project using Cloudant. It's a simple system for logging, so I can track the usage of my apps.

我的文档如下:

{
app:应用名称",
类型:页面视图|登录|等.',
所有者:'电子邮件用户名',
设备:"iphone | android |等.',
日期: 'yyyy-mm-dd'
}

{
app:'name of the app',
type:'page view | login | etc..',
owner:'email_of_the_user',
device: 'iphone | android | etc..',
date: 'yyyy-mm-dd'
}

我尝试进行一些地图缩小和多面搜索,但到目前为止找不到我想要的结果.

I've tried to do some map reducing and faceted searches, but couldn't find so far the result for what I want.

我想计算按同一所有者,日期(yyyy-mm-dd)和应用分组的不同文档的数量.

I want to count the number of distinct documents grouped by same owner, date (yyyy-mm-dd), and app.

[例如,如果同一个人在同一日期两次或两次登录应用程序,则该应用程序将仅计一次. 我想计算每天有多少单用户使用某个应用程序,而不管日志的类型是什么,或者他使用的设备是什么.]

[For example, if a the same guy logs in the app twice or 20 times in the same date, it will be counted only once. I want to count how many single users used an app each day, no matter what's the type of the log, or the device he used.]

如果是SQL,假设文档的每个键都是一列,我将查询如下内容:

If it was SQL, assuming that each key of the document is a column, I would query something like this:

从日期,所有者,应用按日志组中选择应用,日期,计数(*)

SELECT app, date, count(*) FROM LOGS group by date, owner, app

结果可能是这样的:

'App1','2015-06-01',200
'App1','2015-06-02',232
'App2','2015-06-01',142
'App2','2015-06-02',120

'App1', '2015-06-01', 200
'App1', '2015-06-02', 232
'App2', '2015-06-01', 142
'App2', '2015-06-02', 120

如何使用Cloudant/CouchDB获得相同的结果?

How can I get the same result using Cloudant/CouchDB?

推荐答案

您可以使用设计文档来完成此操作,如Cesar所述.一个具体的示例是创建一个视图,您的地图函数会在该视图上发出您要分组的位置的字段,例如:

You can do this using design documents, as Cesar mentioned. A concrete example would be to create a view where your map function emits the field on where you want to group on, such as:

function(doc) {
  emit(doc.email, 1);
}

然后,选择所需的归约函数(例如_count).在Cloudant仪表板上查看此内容时,请确保选择 Reduce (减少)作为查询选项的一部分.通过URL访问视图时,您需要传递适当的参数(reduce=true&group=true).

Then, you select your desired reduce function (such as _count). When viewing this on Cloudant dashboard, make sure you select Reduce as part of the query options. When accessing the view via URL you need to pass the appropriate parameters (reduce=true&group=true).

关于视图"的文档非常详尽: https://docs.cloudant.com/creating_views.html

The documentation on Views here is pretty thorough: https://docs.cloudant.com/creating_views.html

这篇关于选择不同数量的cloudant/couchdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆