Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sum values of a view in a date range using couchdb?

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.

like image 798
fdb Avatar asked Jun 09 '11 14:06

fdb


1 Answers

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.

like image 95
Sean Copenhaver Avatar answered Nov 15 '22 07:11

Sean Copenhaver