Select distinct count cloudant/couchdb

前端 未结 3 1544
盖世英雄少女心
盖世英雄少女心 2020-12-28 23:51

I am starting a project using Cloudant. It\'s a simple system for logging, so I can track the usage of my apps.

My documents looks like this:

{
app:\'na

3条回答
  •  暖寄归人
    2020-12-29 00:47

    We were able to do this in our project using the Cloudant Java API...

    https://github.com/cloudant/java-cloudant

    You should be able to get this sort of result by creating a view that has a map function like this...

    function(doc) {
        emit([doc.app, doc.date, doc.owner], 1);
    }
    

    The reduce function should look like this:

    function(keys, values, rereduce){
        if (rereduce){
            return sum(values);
        } else {
            return sum(values);
        }
    }
    

    Then we used the following query to get the data we wanted.

    Database db = ....
    db.view(viewName).startKey(startKeys).endKey(endKeys)
                .group(true).includeDocs(false).query(castClass)
    

    We supplied the view name and some start and end keys (since we emitted a compound key and we needed to supply a filter) and then used the group method to get the data back as you need it.

    Revised..

    With this new emit key in the map function you should get results like this:

    {[
    {[app1, 2015,06,28, john@somewhere.net], 12}, <- john visited 12 times     on that day...
    {[app1, 2015,06,29, john@somewhere.net], 10},
    {[app1, 2015,06,28, ann@somewhere.net], 1}
    ]}
    

    If you use good start and end keys, the amount of records you're querying will stay small and the number of records you get back is the unique visitors you are seeking. Note that in this scenario you are getting back a bit more than you want, but it does work.

提交回复
热议问题