mongodb/mapreduceとAggregation Frameworkを試してみた

Aggregation FrameworkはSQLの集計関数などを簡単に扱うための仕組み

MapReduceはMapとReduceを自分で定義してMapReduceを実行する感じでさらに複雑な集計が行える

mapreduce

この通りに実行してゆるふわと理解

Aggregation Framework

データの投入

-> % mongo
MongoDB shell version: 2.4.4
connecting to: test
> for(i=1;i<=5;i++){db.abunaideka.save({"name":"taka", "iine":i})}
> for(i=1;i<=10;i++){db.abunaideka.save({"name":"yuji", "iine":i})}
> db.abunaideka.find()
{ "_id" : ObjectId("51d297e7fe59bfb6b81b57ec"), "name" : "taka", "iine" : 1 }
{ "_id" : ObjectId("51d297e7fe59bfb6b81b57ed"), "name" : "taka", "iine" : 2 }
{ "_id" : ObjectId("51d297e7fe59bfb6b81b57ee"), "name" : "taka", "iine" : 3 }
{ "_id" : ObjectId("51d297e7fe59bfb6b81b57ef"), "name" : "taka", "iine" : 4 }
{ "_id" : ObjectId("51d297fafe59bfb6b81b57f0"), "name" : "taka", "iine" : 1 }
{ "_id" : ObjectId("51d297fafe59bfb6b81b57f1"), "name" : "taka", "iine" : 2 }
{ "_id" : ObjectId("51d297fafe59bfb6b81b57f2"), "name" : "taka", "iine" : 3 }
{ "_id" : ObjectId("51d297fafe59bfb6b81b57f3"), "name" : "taka", "iine" : 4 }
{ "_id" : ObjectId("51d2980f6fe2d20cf9b318ae"), "name" : "taka", "iine" : 1 }
{ "_id" : ObjectId("51d2980f6fe2d20cf9b318af"), "name" : "taka", "iine" : 2 }
{ "_id" : ObjectId("51d2980f6fe2d20cf9b318b0"), "name" : "taka", "iine" : 3 }
{ "_id" : ObjectId("51d2980f6fe2d20cf9b318b1"), "name" : "taka", "iine" : 4 }
{ "_id" : ObjectId("51d2980f6fe2d20cf9b318b2"), "name" : "taka", "iine" : 5 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b3"), "name" : "yuji", "iine" : 1 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b4"), "name" : "yuji", "iine" : 2 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b5"), "name" : "yuji", "iine" : 3 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b6"), "name" : "yuji", "iine" : 4 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b7"), "name" : "yuji", "iine" : 5 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b8"), "name" : "yuji", "iine" : 6 }
{ "_id" : ObjectId("51d298226fe2d20cf9b318b9"), "name" : "yuji", "iine" : 7 }
Type "it" for more

aggregateの実行

> db.abunaideka.aggregate({$match:{"name":"taka"}},{$project:{"iine":1}},{$group:{"_id":"iine1","iine1_sum":{"$sum":"$iine"}}});
{ "result" : [ { "_id" : "iine1", "iine1_sum" : 50 } ], "ok" : 1 }
> db.abunaideka.aggregate({$match:{"name":"yuji"}},{$project:{"iine":1}},{$group:{"_id":"iine1","iine1_sum":{"$sum":"$iine"}}});
{
    "result" : [
        {
            "_id" : "iine1",
            "iine1_sum" : 110
        }
    ],
    "ok" : 1
}

$matchに検索条件、$projectに集計列、$groupに集計方法を指定する。

db.abunaideka.aggregate(
    {$match: {"name":"taka"}},
    {$project:{"iine":1}},
    {$group:{"_id":"iine1","iine1_sum":{"$sum":"$iine"}}}
);

他に$min, $max, $avgなどもある。