1


we have a nice little CouchDB running storing some user habit data for the people using our website.
A sample entry looks like this:

{
   "_id": "5a2711671b321c6295acfe04700e3d32",
   "_rev": "1-a74c2d88a8277d9ad7ceb74406c9bb9e",
   "search_term": "dream_theater",
   "timestamp": 1447255047,
   "session_id": "qjrsh7cq10oa9m1mebdikdcvg3",
   "ip_address": "THE IP",
   "browser": "Safari",
   "browser_version": "9.0.1",
   "platform": "Macintosh",
   "service": [
   ],
   "content_type": [
   ],
   "language": "en",
   "limit": 100
}

Right now I wrote several MapReduce functions to access this habit data and extract information out of it. An example looks like this:

"by_platform_browser": {
           "map": "function(doc) { if (doc.browser && doc.platform)  emit([doc.platform, doc.browser], 1) }",
           "reduce": "function(keys, values, rereduce) { return sum(values) }"
       }

This returns me some stuff like this (with group=true):

{"rows":[
{"key":["Linux","Chrome"],"value":198},
{"key":["Linux","Firefox"],"value":129},
{"key":["Macintosh","Chrome"],"value":36},
{"key":["Macintosh","Safari"],"value":135},
{"key":["Windows","Chrome"],"value":37}
]}

As you see there is a timestamp field in each habit data document.
Now to my question itself:
How can I just collect data between a given amount of time. Like starting at epoch 1447255000 and ending at 1447266000.
When I combine keys I cannot use the group.
Is there some other way to solve this?
Thank you so much in advance! //Jonas

TacoVox
  • 141
  • 10

1 Answers1

0

You should emit the timestamp in your view like this:

"by_timestamp": {
    "map": "function(doc) {\n  emit(doc.timestamp, doc._id);\n}"
}

And, to get data between a specific range of timestamps use startkey and endkey attributes in your query. From the browser you could do something like this and see if you are getting your desired result.

http://127.0.0.1:5984/<DATABASE_NAME>/_design/<DESIGN_DOC_NAME>/_view/by_timestamp?startkey=1447255000&endkey=1447266000

Let me know if this helped.

Suhas
  • 1,451
  • 14
  • 22