Views and count distinct


I am searched very must but failed to have a solution , I have documents like




I want to have a view that show me how many DISTINCT user with cliant a exists


Please reply me …


Any idea ???


:sob: help please …


After 5 days!! UP …:disappointed_relieved:


@matthew.groves @brett19 @vsr1 @ingenthr I really need your helps


@socketman2016, Just got alerted on this one. Started looking at it. We shall respond soon.


Hi @socketman2016,

Does it need to be a map/reduce view or is N1QL on the table (I notice you tagged @vsr1).


In N1QL it’s too easy
I want to know how can I do it in a map reduce view , the reason is here Question : Cached count

I know one solution is Feature request : _approx_count_distinct


Hi @socketman2016,

You could use N1QL embedded in an event handler, and store the result in a bucket. You can then either refresh the aggregate periodically using either timers.

Please see:

Best Regards,


Hi @socketman2016,

You could create a map function which emits client and user and reduce function _count.
Then call the view with group_level=2 and start key as [client, null] and end key as [client, “\uffff”] and count the number of row returned. This will be the number of distinct user for that client for startkey[0].

function (doc, meta) {
emit([doc.client, doc.user]);

reduce: _count

Querying for distinct user for client “a”.

counting the number of rows gives the number of distinct user for client “a”.


@AnkitPrabhu your approach is not scaled up
What do you think about 100 millions distinct user per client??
The N1QL works better than your approach