CouchDB View equivalent of SUM & GROUP BY

前端 未结 1 415
后悔当初
后悔当初 2020-12-16 23:30

I have multiple CouchDB documents representing a timestamp with a property userId and tag (a user can have n timestamps and assign each one a tag). I want to query CouchDB b

相关标签:
1条回答
  • 2020-12-17 00:13

    Answering my own question.

    Seems like CouchDB supports arrays as keys. With that in mind it's pretty simple:

    Map:

    function(doc) {
      if(doc.tag) emit([doc.userId, doc.tag], 1); 
    }
    

    Reduce:

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

    The result is then sorted by userId & tag.

    0 讨论(0)
提交回复
热议问题