Built-in reduces
While you can define your own reduce functions, it's often the case that your reduce is going to be doing a simple count or sum operation. There are a handful of built in reduce functions;_sum
, _count
and _stats
. If you
can use these functions you should - they're faster than a javascript
reduce (since they avoid serialisation between erlang and javascript) and are
very well tested._sum
- Produces the sum of all values for a key, values must be numeric
_count
- Produces the row count for a given key, values can be any valid json
_stats
- Produces a json structure containing sum, count, min, max and sum squared, values must be numeric
{ "_id": "_design/views103", "_rev": "8-1798b34c9929eb36d3bc04f99d2b5445", "indexes": { "animals": { "index": "function(doc){\n index(\"default\", doc._id);\n\n if (doc['customerID']){\n index(\"customerID\", doc['customerID'], {\"store\": \"yes\"});\n }\n\n if (doc['trxnStatus']){\n index(\"trxnStatus\", doc['trxnStatus'], {\"store\": \"yes\"});\n }\n\n if (doc['createdStamp']){\n index(\"createdStamp\", doc['createdStamp'], {\"store\": \"yes\"});\n }\n\n if (doc['paymentType']){\n index(\"paymentType\", doc['paymentType'], {\"store\": \"yes\"});\n }\n\n if (doc['returnAmount']){\n index(\"returnAmount\", doc['returnAmount'], {\"store\": \"yes\"});\n }\n\n if (doc['trxnNumber']){\n index(\"trxnNumber\", doc['trxnNumber'], {\"store\": \"yes\"});\n }\n\n if (doc['trxnReference']){\n index(\"trxnReference\", doc['trxnReference'], {\"store\": \"yes\"});\n }\n\n if (doc['authCode']){\n index(\"authCode\", doc['authCode'], {\"store\": \"yes\"});\n }\n\n }" } }, "views": { "returnAmount_sum": { "map": "function(doc) {\n if(doc.returnAmount && doc.customerID){\n emit(doc.customerID, doc.returnAmount);\n }\n}", "reduce": "function (keys, values, rereduce) {\nreturn (values);\n}" } } }
https://cloudant.com/for-developers/views/#pageArea
{
"_id": "_design/name",
"views": {
"view1": {
"map":"function(doc){emit(doc.field, 1)}",
"reduce": "function(key, value, rereduce){return sum(values)}"
}
}
}
No comments:
Post a Comment