如何使用 couchdb 对日期范围内的视图值求和?

发布于 2024-11-14 17:10:45 字数 515 浏览 5 评论 0原文

如果我有一个映射函数,发出时间戳作为键和数字作为文档,如何获取选择日期范围的值的总和?

编辑 文档示例是:

{
   "_id": "[2011, 6, 7, 10, 55]",
   "_rev": "1-f87d54608d36cd2e28add67e88e416a4",
   "volt": 107,
   "ampere": 23.5
}

视图是:

{
   "_id": "_design/power",
   "_rev": "1-7788287ab51c480c725498eba4f79ddb",
   "language": "javascript",
   "views": {
       "watt": {
           "map": "function(doc) {\n  emit(doc._id, doc.volt * doc.ampere);\n}"
       }
   }
}

我需要查询例如:三月份的平均每小时。

If I have a map function emitting a timestamp as key ad a number as document, how to get sum of values selecting a date range?

EDIT
a document example is:

{
   "_id": "[2011, 6, 7, 10, 55]",
   "_rev": "1-f87d54608d36cd2e28add67e88e416a4",
   "volt": 107,
   "ampere": 23.5
}

the view is:

{
   "_id": "_design/power",
   "_rev": "1-7788287ab51c480c725498eba4f79ddb",
   "language": "javascript",
   "views": {
       "watt": {
           "map": "function(doc) {\n  emit(doc._id, doc.volt * doc.ampere);\n}"
       }
   }
}

I need to query eg.: the average per hour in the month of March.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-11-21 17:10:45

当您想要对日期信息进行范围查询时,我发现您的地图函数将 _id 作为键发出有问题。您应该将日期信息保存在单独的字段中。 _id 必须是一个字符串,您将无法对其正确执行 startkey-endkey 查询。

比如:

{
   "_id": "997b9f758899f102ab58570686001bc2",
   "_rev": "1-f87d54608d36cd2e28add67e88e416a4",
   "date": [2011, 6, 7, 10, 55],
   "volt": 107,
   "ampere": 23.5
}

那么你的设计文档就会变成这样:

{
   "_id": "_design/power",
   "_rev": "1-7788287ab51c480c725498eba4f79ddb",
   "language": "javascript",
   "views": {
       "watt": {
           "map": "function(doc) {\n  emit(doc.date, doc.volt * doc.ampere);\n}",
           "reduce": "_sum"
       }
   }
}

你可以使用组级别来根据你的键[年、月、日、小时、分钟]来控制信息的返回方式。组级别 1 是一年、两个月等的总计。除此之外,您可以使用 startkey 和 endkey 对其进行过滤。

GET db/_design/power/_view/watt?group_level=2

应该给您返回类似以下内容:

{"rows":[
 {"key":[2011,4],"value":1988.5},
 {"key":[2011,5],"value":1988.5},
 {"key":[2011,6],"value":7778.0}
]}

使用键范围删除分组和过滤也可以获得您想要的信息,但它'看起来会有所不同。

GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]

{"rows":[
{"key":null,"value":3977.0}
]}

将它们组合起来以对值进行分组,然后减少并过滤掉您想要的月份不想要。

GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]&group_level=2

{"rows":[
{"key":[2011,4],"value":1988.5},
{"key":[2011,5],"value":1988.5}
]}

无论如何,只是想彻底...也帮助我记住这些东西。

CouchDB HTTP View API - 查询选项

编辑:

我注意到您提到想要分组按小时计算。您将继续分组级别直至密钥的小时部分。

GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]&group_level=4

{"rows":[
{"key":[2011,4,9,11],"value":1988.5},
{"key":[2011,5,9,11],"value":1988.5}
]}

恐怕我的测试数据库没有有非常有用的信息,但我希望我展示了如何使用 startkey/endkey 和 group_level 。

I see a problem with your map function emitting your _id as a key when you want to do a range query on date information. You should save the date information in a separate field. The _id has to be a string and you won't be able to correctly perform the startkey-endkey query on it.

Something like:

{
   "_id": "997b9f758899f102ab58570686001bc2",
   "_rev": "1-f87d54608d36cd2e28add67e88e416a4",
   "date": [2011, 6, 7, 10, 55],
   "volt": 107,
   "ampere": 23.5
}

Then your design document would turn into something like:

{
   "_id": "_design/power",
   "_rev": "1-7788287ab51c480c725498eba4f79ddb",
   "language": "javascript",
   "views": {
       "watt": {
           "map": "function(doc) {\n  emit(doc.date, doc.volt * doc.ampere);\n}",
           "reduce": "_sum"
       }
   }
}

You can use the group level to control how the information comes back based on your key of [year, month, day, hour, minute]. Group level 1 would be totals for a year, 2 month, etc. On top of that you can filter it with startkey and endkey.

GET db/_design/power/_view/watt?group_level=2

Should give you back something similar to:

{"rows":[
 {"key":[2011,4],"value":1988.5},
 {"key":[2011,5],"value":1988.5},
 {"key":[2011,6],"value":7778.0}
]}

Dropping the grouping and filtering with a key range can also get you the information you want, but it'll look different.

GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]

{"rows":[
{"key":null,"value":3977.0}
]}

Combining them to both to group the values before reducing and filter out the months you don't want.

GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]&group_level=2

{"rows":[
{"key":[2011,4],"value":1988.5},
{"key":[2011,5],"value":1988.5}
]}

Anyway, just trying to be thorough...also helps me remember this stuff.

CouchDB HTTP View API - Query Options

EDIT:

I noticed that you mentioned wanting to group it by hour. You would continue the grouping level down to the hour portion of your key.

GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]&group_level=4

{"rows":[
{"key":[2011,4,9,11],"value":1988.5},
{"key":[2011,5,9,11],"value":1988.5}
]}

I'm afraid that my test database didn't have very useful information, but I hope I displayed how startkey/endkey and group_level can be used.

没︽人懂的悲伤 2024-11-21 17:10:45

您可以使用 _sum 内置函数作为 reduce

"reduce":"_sum"

然后您需要使用正确的组级别以及 startkey查询视图>endkey

GET db/_design/power/_view/watt?group_level=2&startkey=[2011,3]&endky=[2011,4]&inclusive_end=false

这给出了三月份的总和。格式化

You can use the _sum built-in as reduce:

"reduce":"_sum"

Then you need to query your view with the proper group level and a startkey and endkey:

GET db/_design/power/_view/watt?group_level=2&startkey=[2011,3]&endky=[2011,4]&inclusive_end=false

That gives you the sum for the month of March. formating

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文