本文介绍了选择不同的计数 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:'应用名称',
类型:'页面浏览量 |登录 |等等..',
所有者:'email_of_the_user',
设备:'iphone |安卓 |等等..',
日期:'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

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);
}

然后,您选择所需的 reduce 函数(例如 _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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:43