melaka
melaka

Reputation: 719

couchdb - Getting the total of distinct values

I have the following data structure in my Couch database. I need to get the count of distinct sessions_id's per day. The records must also filter by the app_version as well.

My current MapReduce looks like this

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

But this gives me the output as following

["2014-08-20", 2, "kOnNIhCNQ31LlkpEPQ7XnN1D"] 1
["2014-08-20", 2, "TOnNIhCNQ31LlkpEPQ7XnN1D"] 2
["2014-08-21", 2, "TOnNIhCNQ31LlkpEPQ7XnN1D"] 2

I need this to be printed as - because that's the number of unique session_ids per day

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

Any Ideas on how to?

Any help would be much appreciated.

[
        {
            "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"
        }

        }
    ]

Upvotes: 1

Views: 106

Answers (1)

Matt Jennings
Matt Jennings

Reputation: 1148

Put app_version first in your view:

emit([doc.app_version, date, doc.session.session_id], 1);

Then query with group_level=2

Upvotes: 1

Related Questions