The data in couch has a record for each contact type as follows.
[{
"_id":"1"
"contact_type":"AA"
"contact_email" : "userA@domain.com"
},{
"_id":"2"
"contact_type":"BB"
"contact_email" : "userA@domain.com"
},{
"_id":"3"
"contact_type":"CC"
"contact_email" : "userA@domain.com"
},{
"_id":"4"
"contact_type":"AA"
"contact_email" : "userB@domain.com"
},
{
"_id":"5"
"contact_type":"BB"
"contact_email" : "userB@domain.com"
}]
Records 1,2,3 represent contact with the email userA@domain.com while records 4 and 5 represent contact with an email userB@domain.com.
My task is to build a query to get the total amount of unique contacts (2 in this case) but I am not sure how this can be achieved.
Currently, I have a map function with _count reduce
function(doc){
emit(doc.emailAddress, 1);
}
which returns
{
"rows":
{
"key" : "userA@domain.com",
"value" : 3
},
{
"key" : "userA@domain.com",
"value" : 2
}
}
However, what I need is the total amount of those records above, i.e. something like
{
"total" : 2
}
(Note: the database is large so looping through the mapped/reduced view to count the records is not an option)
Yo can use the approach described in this post
1.- Define a view as you described in your question (i.e. named "emails")
2.- Define a list function (in my example named "count")
function(head,req){
var count = 0;
while(getRow()) count++;
return JSON.stringify({total: count});
}
Assuming your design doc is named "users", you can transform the view result using the list function with this URL.
http://.../db/_design/users/_list/count/emails?group=true
You should get the distinct value count as the result.