connvoiのブログ

前だけ向いていけばいい。

mac

トマコロがまだ発売していた頃のはなし。

t = db.foo;
t.drop();

t.save({"user" : "synboo", "bigmac" : 1, "tomacoro" : 1, "potato" : 0, "coke" : 0 , "date": 20121206});
t.save({"user" : "synboo", "bigmac" : 1, "tomacoro" : 1, "potato" : 0, "coke" : 0 , "date": 20121207});
t.save({"user" : "synboo", "bigmac" : 1, "tomacoro" : 1, "potato" : 0, "coke" : 0 , "date": 20121208});
t.save({"user" : "synboo", "bigmac" : 1, "tomacoro" : 1, "potato" : 0, "coke" : 0 , "date": 20121209});
t.save({"user" : "synboo", "bigmac" : 1, "tomacoro" : 1, "potato" : 0, "coke" : 0 , "date": 20121211});
t.save({"user" : "connvoi_tyou", "bigmac" : 0, "tomacoro" : 1, "potato" : 1, "coke" : 1 , "date": 20121206});
t.save({"user" : "connvoi_tyou", "bigmac" : 0, "tomacoro" : 1, "potato" : 1, "coke" : 1 , "date": 20121207});
t.save({"user" : "connvoi_tyou", "bigmac" : 1, "tomacoro" : 0, "potato" : 1, "coke" : 0 , "date": 20121208});
t.save({"user" : "connvoi_tyou", "bigmac" : 1, "tomacoro" : 0, "potato" : 1, "coke" : 0 , "date": 20121209});
t.save({"user" : "connvoi_tyou", "bigmac" : 1, "tomacoro" : 0, "potato" : 1, "coke" : 1 , "date": 20121210});
t.save({"user" : "connvoi_tyou", "bigmac" : 1, "tomacoro" : 0, "potato" : 1, "coke" : 1, "date": 20121211});
t.save({"user" : "summerwind", "bigmac" : 0, "tomacoro" : 1, "potato" : 1, "coke" : 1 , "date": 20121206});
t.save({"user" : "summerwind", "bigmac" : 1, "tomacoro" : 0, "potato" : 1, "coke" : 1 , "date": 20121208});
t.save({"user" : "summerwind", "bigmac" : 0, "tomacoro" : 1, "potato" : 1, "coke" : 1 , "date": 20121209});
t.save({"user" : "summerwind", "bigmac" : 1, "tomacoro" : 0, "potato" : 1, "coke" : 1, "date": 20121211});
t.save({"user" : "takewick", "bigmac" : 0, "tomacoro" : 0, "potato" : 1, "coke" : 1 , "date": 20121206});
t.save({"user" : "takewick", "bigmac" : 0, "tomacoro" : 0, "potato" : 1, "coke" : 1 , "date": 20121207});
t.save({"user" : "takewick", "bigmac" : 0, "tomacoro" : 0, "potato" : 1, "coke" : 1 , "date": 20121209});
t.save({"user" : "takewick", "bigmac" : 0, "tomacoro" : 0, "potato" : 1, "coke" : 1 , "date": 20121210});
t.save({"user" : "takewick", "bigmac" : 0, "tomacoro" : 0, "potato" : 1, "coke" : 1, "date": 20121211});

m=function(){
  emit(this.user, {count: 1, bigmac :this.bigmac, tomacoro :this.tomacoro , potato :this.potato , coke: this.coke});
};

r=function(key,values){
  var result={count: 0, bigmac: 0,tomacoro: 0, potato: 0, coke: 0}; 
  values.forEach(function (value){
    result.count += value.count; 
    result.bigmac += value.bigmac; 
    result.tomacoro += value.tomacoro; 
    result.potato += value.potato; 
    result.coke += value.coke; 
  }); 
  return result;
};

f=function(key,value){
  //big:556 tomacoro:446 potato:454 coke:140 
  value.cal   = value.bigmac*556 + value.tomacoro*446 + value.potato*454 + value.coke*140;
  value.rate  =  value.cal/value.count;
  return value;
};

t.mapReduce(m,r,{finalize:f, out:"mr_out"});

> db.mr_out.find();
{ "_id" : "connvoi_tyou", "value" : { "count" : 6, "bigmac" : 4, "tomacoro" : 2, "potato" : 6, "coke" : 4, "cal" : 6400, "rate" : 1066.6666666666667 } }
{ "_id" : "summerwind", "value" : { "count" : 4, "bigmac" : 2, "tomacoro" : 2, "potato" : 4, "coke" : 4, "cal" : 4380, "rate" : 1095 } }
{ "_id" : "synboo", "value" : { "count" : 5, "bigmac" : 5, "tomacoro" : 5, "potato" : 0, "coke" : 0, "cal" : 5010, "rate" : 1002 } }
{ "_id" : "takewick", "value" : { "count" : 5, "bigmac" : 0, "tomacoro" : 0, "potato" : 5, "coke" : 5, "cal" : 2970, "rate" : 594 } }

ビックマックとポテトM食べる変わりに、ビックマックとトマコロ食べるのはダイエットにはならない。