Dan Milon
Dan Milon

Reputation: 2529

couchdb reduce with complex key

Suppose we have a few documents in a couchdb, like this

{ page: 'home', country: 'EN', timestamp: 15448 }
{ page: 'search', country: 'FR', timestamp: 15448 }
{ page: 'search', country: 'EN', timestamp: 15448 }
{ page: 'home', country: 'DE', timestamp: 15457 }

Each document represents a pageview from a country with a given timestamp. What i would like to do is query these documents, for a given page, and a timestamp range, get the number of pageviews per country for that range.

What i came up doing is this map function

function (doc) {
  emit([doc.page, doc.timestamp, doc.country], 1)
}

and _sum as the reduce function.

I can query the view with eg. startkey=["home",15448]&endkey=["home", 15448, {}] but this returns the pageviews per country, per day, which is more information than i need. (just the pageviews per country for a given period).

Upvotes: 4

Views: 568

Answers (1)

ajreal
ajreal

Reputation: 47331

just dun emit the country

function (doc)
{
  emit([doc.page, doc.timestamp], 1)
}

reduce function

function (keys, value)
{
  return sum(value);
}

Upvotes: 2

Related Questions