Skip to content

Instantly share code, notes, and snippets.

@krawaller
Created October 21, 2010 10:05
Show Gist options
  • Save krawaller/638246 to your computer and use it in GitHub Desktop.
Save krawaller/638246 to your computer and use it in GitHub Desktop.
CouchOne Tristania example
function map(doc){
if(doc.username && doc.presentation){
emit(doc._id, doc);
}
}
{
_id: "0DB6DACF-A385-5284-BFD2-519F9AC802BB",
_rev: "31-62f831b083c0539e8276f667e357bef2",
coords: {
latitude: 57.863380432128906,
longitude: 11.911527633666992
},
presentation: "moo moo moo moo",
username: "David",
favalbum: "rubicon",
favtracks: {
"rubicon": "patriotgames"
}
}
{
rows:[
{
key:null,
value: {
favalbum: {
votes:2,
results: {
rubicon: {
votes: 2,
percentage:100
}
}
},
favtracks: {
rubicon: {
votes: 1,
results: {
patriotgames: {
votes:1,
percentage: 100
}
}
}
}
}
}
]
}
function map(doc){
if(doc.favalbum || doc.favtracks){
emit(doc._id, doc);
}
}
function reduce(keys,values){
var a,t,stats = { favalbum: {votes: 0,results:{}}, favtracks: {} };
values.forEach(function(u){
if (u.favalbum){
if (!stats.favalbum.results[u.favalbum]){
stats.favalbum.results[u.favalbum] = {votes:0};
}
stats.favalbum.results[u.favalbum].votes++;
stats.favalbum.votes++;
}
if (u.favtracks){
// omitted for brevity
}
}
return stats;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment