如何使用 couchdb 对日期范围内的视图值求和?
如果我有一个映射函数,发出时间戳作为键和数字作为文档,如何获取选择日期范围的值的总和?
编辑 文档示例是:
{
"_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您想要对日期信息进行范围查询时,我发现您的地图函数将 _id 作为键发出有问题。您应该将日期信息保存在单独的字段中。 _id 必须是一个字符串,您将无法对其正确执行 startkey-endkey 查询。
比如:
那么你的设计文档就会变成这样:
你可以使用组级别来根据你的键[年、月、日、小时、分钟]来控制信息的返回方式。组级别 1 是一年、两个月等的总计。除此之外,您可以使用 startkey 和 endkey 对其进行过滤。
GET db/_design/power/_view/watt?group_level=2
应该给您返回类似以下内容:
使用键范围删除分组和过滤也可以获得您想要的信息,但它'看起来会有所不同。
GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]
将它们组合起来以对值进行分组,然后减少并过滤掉您想要的月份不想要。
GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]&group_level=2
无论如何,只是想彻底...也帮助我记住这些东西。
CouchDB HTTP View API - 查询选项
编辑:
我注意到您提到想要分组按小时计算。您将继续分组级别直至密钥的小时部分。
GET db/_design/power/_view/watt?startkey=[2011,4]&endkey=[2011,6]&group_level=4
恐怕我的测试数据库没有有非常有用的信息,但我希望我展示了如何使用 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:
Then your design document would turn into something like:
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:
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]
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
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
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.
您可以使用
_sum
内置函数作为reduce
:然后您需要使用正确的组级别以及
startkey
和查询视图>endkey
:这给出了三月份的总和。格式化
You can use the
_sum
built-in asreduce
:Then you need to query your view with the proper group level and a
startkey
andendkey
:That gives you the sum for the month of March. formating