B
B
bassstorm2013-11-15 15:24:35
CouchDB
bassstorm, 2013-11-15 15:24:35

CouchDB: map/reduce selection by time span and additional keys

The database contains the following documents:

{
    "user_id": 1,
    "event_id": 2,
    "event_dt": [
        2013, 11, 15, 10, 0, 0, 0
    ]
}

I would like to get an answer to the question using map / reduce: how many events of each type occurred for a specific user in a given period of time.
The only solution I see so far is to use two views and significant support from the application code.
First, we select all user events in a given period of time. In this case, of course, you will have to choose page by page (you need your own implementation using limit=XXX), because the time interval can be up to a year.
function map(doc) {
    emit([doc.user_id, doc.event_dt], doc.event_id);
}

Then for each custom event, we request the number of occurrences in the same given time interval ( group_level=2):
function map(doc) {
    emit([doc.user_id, doc.event_id, doc.event_dt], 1);
}

I would like to get away from the need to repeatedly call the database and reduce the whole thing to one request.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question