@Grab(group='com.gmongo', module='gmongo', version='0.8') import com.mongodb.* import com.gmongo.* mongo = new GMongo("localhost", 27017) db = mongo.getDB("local") db["stores"].drop() // コレクションにオブジェクトを挿入する col = db["stores"] col.insert([country:"Japan", region: "east", store:"store_A", sales:100]) col.insert([country:"Japan", region: "west", store:"store_B", sales:120]) col.insert([country:"Japan", region: "west", store:"store_C", sales:170]) col.insert([country:"United States", region:"north", store:"store_D", sales:80]) col.insert([country:"United States", region:"west", store:"store_E", sales:200]) col.insert([country:"United States", region:"east", store:"store_F", sales:190]) col.insert([country:"United States", region:"east", store:"store_G", sales:100]) // MapReduceで国・地域ごとの店舗数を取得 println col.mapReduce( /* map */ """function(){ emit({country:this.country, region:this.region}, {count:1}); }""", /* reduce */ """function(key, values){ var total = 0; for(var vi = 0;vi<values.length;vi++){ total += values[vi].count; } return {count: total}; }""", /* outputTarget */"mr_result", /* query */ new BasicDBObject() ) // 結果を表示 db.mr_result.find().each { println it }
動作環境
Groovy 1.8.0, JDK6 Update22, gmongo 0.8, MongoDB 2.0
関連情報
gmongoのウェブサイト
https://github.com/poiati/gmongo
groovyとMongoDBのまとめ
0 件のコメント:
コメントを投稿