Is there some way to do the equivalent of a COUNT(DISTINCT keys) using map/reduce?
Basically a Couchbase version of this couchdb one:
Is there some way to do the equivalent of a COUNT(DISTINCT keys) using map/reduce?
Basically a Couchbase version of this couchdb one:
Hello,
You should be able to take the same approach that the one describes in Stackoverflow. Have you tested?
Tug
@tgrall
The first answer over there is incorrect and the second one uses CouchDB’s list functions, which I thought Couchbase didn’t have. Which approach were you referring to?
Hello,
So suppose you have the following docs:
{ "id" : "person:001", "name" : "John Doe", "gender" : "M" }{ “id” : “person:002”,
“name” : “Jane Doe”,
“gender” : “F”
}
{ “id” : “person:003”,
“name” : “David Simon”,
“gender” : “M”
}
So if you want to count the number of “male” or “female” you just need to:
function (doc, meta) {
emit(doc.gender);
}
and the map function _count()
So when you execute the query you just need to put the group level to 1 (here we only have one)
by_gender?group_level=1
You will have the number of document for each key.
Is this what you want?
Regards
Tug
@tgrall
Hey Tug,
Sorry for not providing an example earlier.
{
"id" : "person:001",
"name" : "John Doe",
"gender" : "M"
}
{
“id” : “person:002”,
“name” : “John Doe”,
“gender” : “M”
}
{
“id” : “person:003”,
“name” : “David Simon”,
“gender” : “M”
}
Notice that (unlike your example) the first two users have the same name in mine. I’m looking at getting the count of unique names, i.e., I’m expecting an answer of 2 (John Doe and David Simon).
Hmm… too much time far away from SQL
One approach could be to do the following:
emit(doc.name);
Then call the view and count the number of name (row[].size) this will be the number of distinct name no?
Tug