melaka
melaka

Reputation: 719

couchdb - querying views with start_key and end_key

I have a couchdb record structure which looks like this

[
    {
        "app_version": 2,
        "platform": "android",
        "session": {
            "timestamp": "2014-08-20T00:00:00.000Z",
            "session_id": "TOnNIhCNQ31LlkpEPQ7XnN1D",
            "ip": "202.150.213.66",
            "location": "1.30324,103.5498"
        }

    },
{
    "app_version": 2,
    "platform": "android",
    "session": {
        "timestamp": "2014-08-21T00:00:00.000Z",
        "session_id": "TOnNIhCNQ31LlkpEPQ7XnN1D",
        "ip": "202.150.213.66",
        "location": "1.30324,103.5498"
    }

}
{
    "app_version": 2,
    "platform": "ios",
    "session": {
        "timestamp": "2014-08-21T00:00:00.000Z",
        "session_id": "TOnNIhCNQ31LlkpEPQ7XnN1D",
        "ip": "202.150.213.66",
        "location": "1.30324,103.5498"
    }

},
{
    "app_version": 1,
    "platform": "ios",
    "session": {
        "timestamp": "2014-08-21T00:00:00.000Z",
        "session_id": "TOnNIhCNQ31LlkpEPQ7XnN1D",
        "ip": "202.150.213.66",
        "location": "1.30324,103.5498"
    }

    }
]

I need to query all the records which happened between a a given number of dates and a app_version number, and I want to get the total of each by the platform.

So I wrote a map-reduce function like this;

   "total": {
               "map": "function(doc) { 
                     date = doc.session.timestamp.split("T")[0];   
                     emit([date, doc.app_version,doc.platform], 1);
                     }",
               "reduce": "_count"
           }

This gives me the output properly by grouping the records into dates.

["2014-08-20", 2, "android"]    2
["2014-08-20", 2, "ios"]        1
["2014-08-21", 2, "android"]    1
["2014-08-21", 2, "ios"]        1

But the problem comes when I try to query them using the start_key and end_key (to query by the date range)

Im sending the GET request as follows;

http://localhost/dummy_db_new/_design/views/_view/total?
start_key=["2014-08-20",2,WHAT_TO_PUT_HERE]
&end_key=["2014-08-20",2,WHAT_TO_PUT_HERE]
&group=true

I need to know what to put at the above places for it to have any platform(a string).

Upvotes: 2

Views: 170

Answers (1)

melaka
melaka

Reputation: 719

Oh I was able to find an answer.

The Answer was to use a wildcard. So basically I sent the request with a wildcard which will accept any platform type

http://localhost/dummy_db_new/_design/views/_view/total?
start_key=["2014-08-20",2,0]
&end_key=["2014-08-20",2,{}]
&group=true

{} means javascript object, so it will accept any JS object.

Upvotes: 1

Related Questions