I have a requirement to get only distinct Employee Ids from Employee table. My emit is .. emit(doc.empId,1), will give me all employee ids ie duplicates one also. How i can achieve the same in cloudant. If i use the reduce on value will it return me all Distinct employee ids?
1 Answers
you can use the Reduce function to obtain the distinct employee Ids.
see duplicate question: How do I do the SQL equivalent of "DISTINCT" in CouchDB?
if you are not interested in the value it could be simply
Map function
function (doc) {
emit(doc.empId, null);
}
Reduce function
function (key, values, rereduce) {
return null;
}
however, if you need to return values you can try something like this
Map
function (doc) {
emit([doc.empId, doc.joiningDate], {"companyId": doc.companyId, "dept": doc.dept});
}
Reduce
function (keys, values, rereduce) {
return values[0];
}
however, you need to make sure the values
argument in the Reduce function is an array of all the objects emitted in the Map function. therefore, you will need to determine how best to
reduce to a single scalar value. That is, an integer; a string; or a small, fixed-size list or object that includes an aggregated value (or values) from the values argument.
from http://guide.couchdb.org/draft/cookbook.html
in my example, above i simply just took the first entry from values
(i am assuming all the emitting values are identical).

- 706
- 1
- 4
- 9
-
Hi Barbosa Thanks for the answer but i need values , actually i need to get full document as in values like emit([doc.empId,doc.joiningDate],doc), (need to pass joining date as filter )i fear if above solution works or i just need to simply say return doc in reduce?, please correct me if i am wrong...please bear with me i am new to cloudant – Lucifer007 May 29 '17 at 15:37
-
@MayankMike not a problem. i no expert just trying to be helpful. we are all new at one time. i updated the answer to show an example of emitting values as well. you just need to keep in mind that in the Reduce you will need to somehow consolidate/condense/aggrigate/reduce the values into a single scalar value. depending on what the values are, you can determine how best to reduce. – vabarbosa May 29 '17 at 17:35
-
Thanks Barbosa. Your solution works for me, but if the number of documents are less it works like creamy. but if i have more number of documents(say number of documents are:114655) i am getting error as {"error":"timeout","reason":"The request could not be processed in a reasonable amount of time."}. – Lucifer007 May 30 '17 at 05:00
-
The documents need to be indexed, which might take some time. I've seen this behavior if I tried to query a view right after it was created (and indexing is still in progress). – ptitzler May 31 '17 at 19:34