title: couchdb - filtering on complex key
body: <p>I have 30m records of the following schema in couchdb.</p>
  
  <pre><code>{ countryIso: 'TR'
  , region: '68'
  , city: 'Ankara'
  , lat: '39.927200317383'
  , lon: '32.864398956299'
  , action: 'install'
  , itime: 1288523291000  // That is unix timestamp
  , fileId: 33221
  , api_level: '3'
  , display_size: '320x480'
  , model: 'Galaxy'
  }
  </code></pre>
  
  <p>These records represent downloads of files in a service. What i would like to do is provide statistics for these events.</p>
  
  <p>Some example queries:</p>
  
  <ul>
  <li>Given fileId and date range, get number of downloads per day</li>
  <li>Given fileId and date range, get display_size's used per day</li>
  <li>Given fileId and date range, get api_level used per day</li>
  </ul>
  
  <p>Ideally (if possible) we should also answer more complex queries, like:</p>
  
  <ul>
  <li>Given fileId, date range and countryIso, show downloads per day</li>
  </ul>
  
  <p>I made the following map function</p>
  
  <pre><code>function (doc) {
    emit([doc.fileId,
          Math.floor(doc.itime/60/60/24),
          doc.countryIso, doc.display_size,
          doc.api_level], 1)
  }
  </code></pre>
  
  <p>and <code>_sum</code> as the reduce function.</p>
  
  <p>Now i can do this</p>
  
  <pre><code>?group_level=2&amp;startkey=[247]&amp;endkey=[247,{}]
  
  {"rows":[
  {"key":[247,14913505],"value":4},
  {"key":[247,14913528],"value":4},
  {"key":[247,14913563],"value":4}
  ]}
  </code></pre>
  
  <p>for example, to get the downloads of a specific fileId per day.</p>
  
  <p>or this</p>
  
  <pre><code>?group_level=3&amp;startkey=[247]&amp;endkey=[247,{}]
  
  {"rows":[
  {"key":[247,14913505,"GB"],"value":4},
  {"key":[247,14913528,"AE"],"value":4},
  {"key":[247,14913563,"TR"],"value":4}
  ]}
  </code></pre>
  
  <p>to get the downloads of a specific fileId per day per country.</p>
  
  <p>But i cant seem to query my view in a way that will return this</p>
  
  <pre><code>{"rows":[
  {"key":[247,14913505,"320x480"],"value":4},
  {"key":[247,14913528,"320x480"],"value":4},
  {"key":[247,14913563,"320x533"],"value":4}
  ]}
  </code></pre>
  
  <p>Do i have to create a separate view for each of the queries (graphs) i need, or you think it is possible to create a complex view that will answer all these queries?</p>