Skip to content

Instantly share code, notes, and snippets.

@madpink
Last active August 30, 2017 14:55
Show Gist options
  • Save madpink/66c5bc8703eb8cdd0c6a478df330bc19 to your computer and use it in GitHub Desktop.
Save madpink/66c5bc8703eb8cdd0c6a478df330bc19 to your computer and use it in GitHub Desktop.
CouchDB - datascan example ddoc
{
"_id": "_design/datascan",
"views": {
"dead": {
"map": "function (doc) {\n if (doc.year_death != null)\n {emit(doc.year_death, doc.stagename);}\n}"
},
"nationality": {
"reduce": "_count",
"map": "function (doc) {\n emit(doc.nationality, 1);\n}"
},
"brits": {
"map": "function (doc) {\n if (doc.nationality == \"British\")\n emit(doc._id, {StageName: doc.stagename, Name: doc.name});\n}"
},
"deadoralive": {
"map": "function(doc) {\n if (doc.year_death) emit(\"Dead\", 1);\n else emit(\"Alive\", 1);\n}",
"reduce": "_count"
},
"instrumentsplayed": {
"map": "function (doc) { if(doc.stagename) emit(doc.stagename, doc.instruments); else emit(doc.name, doc.instruments);}"
},
"complex": {
"map": "function (doc) {emit([doc.nationality, doc.name], doc.bands);}"
},
"age of death": {
"map": "function (doc) {\n var age_at_death = doc.year_death - doc.year_birth;\n var name_to_use;\n if (doc.stagename != null)\n name_to_use = doc.stagename\n else\n name_to_use = doc.name;\n if (age_at_death > 0)\n emit(name_to_use, age_at_death);\n}"
},
"ageofdeath": {
"map": "function (doc) {\n var age_at_death = doc.year_death - doc.year_birth;\n var name_to_use;\n if (doc.stagename != null)\n name_to_use = doc.stagename\n else\n name_to_use = doc.name;\n if (age_at_death > 0)\n emit(name_to_use, age_at_death);\n}"
},
"northamerican": {
"map": "function (doc) {\n if(doc.nationality == \"American\" || doc.nationality == \"Cuban\" || doc.nationality == \"Canadian\")\n emit(doc.stagename, doc.name);\n}"
},
"born60s": {
"map": "function (doc) {\n if (doc.year_birth >= 1960 && doc.year_birth < 1970)\n emit([doc.stagename,doc.name], doc.year_birth);\n}"
},
"approxage": {
"map": "function(doc) {\n if (!doc.year_death) \n var age = 2017 - doc.year_birth;\n if (age) \n emit(age, [doc.stagename,doc.name]);\n}"
},
"averageage": {
"reduce": "function (keys, values, rereduce) {\n var allages = sum(values);\n var pplcount = values.length;\n var average = allages/pplcount;\n average = Math.round(average);\n return average;\n}",
"map": "function(doc) {\n if (!doc.year_death) \n var age = 2017 - doc.year_birth;\n if (age) \n emit(null, age);\n}"
},
"instruments": {
"map": "function(doc) {\n if(doc.instruments) {\n doc.instruments.forEach(function(inst) {\n emit(inst, 1);\n });\n }\n}",
"reduce": "_count"
}
},
"language": "javascript"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment