How to count uniques with view?

So say I have the following documents

{“acct”:“123456”,“name”:“John Smith”}
{“acct”:“123456”,“name”:“John Smith”}
{“acct”:“123457”,“name”:“Jane Doe”}

function(doc) {
if(doc.acct)
emit (doc.acct, 1)
}

_count

This view will return 3

I want to find how many unique acct I have.

The result should be 2

Nice To Meet You
My name is favour.
here is my privet e-mail '(favourmadi@hotmail.com)
I would like to know you the more, I can send you my picture and my detail’s, I’m waiting for your mail to my email address above. please do not chat with me here just send me email and I’ll get back to you with love.

The query result will be 3 since 3 keys were actually emitted, to actually get the unique you need to group the reduce results, I created a quick gist to illustrate, which I copied here

# Get a number of unique account numbers via a map / reduce view # # foobar bucket contains: # # A = {"acct":"123456","name":"John Smith"} # B = {"acct":"123456","name":"John Smith"} # C = {"acct":"123457","name":"Jane Doe"} # # map: # # function (doc, meta) { # if (doc.acct) { # emit(doc.acct); # } # } # # and reduce: # # _count #

require "couchbase"
cb = Couchbase.connect(“http://localhost:9000/pools/default/buckets/foobar”)

accts = cb.design_docs[“test”].test(group: true).map { |doc| doc.key }
p accts # => [“123456”, “123457”]