MongoDB –集計とグループの例
このチュートリアルでは、MongoDB集計関数を使用してドキュメント(データ)をグループ化する方法を示します。
1. テストデータ
JSON形式のデータは、Webサイトのホスティングプロバイダーを示します。
website.json
{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" } { "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com"} { "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" } { "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" } { "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" } { "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" } { "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" } { "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" } { "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" } { "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }
「ウェブサイト」コレクションにインポートします。
> mongoimport -d testdb -c website --file website.json
connected to: 127.0.0.1
Mon Jan 13 14:30:22.662 imported 10 objects
Note
コレクションが存在する場合は、--upsert
オプションを追加してデータを上書きします。
> mongoimport -d testdb -c website --file website.json --upsert
2. グループ化の例
db.collection.aggregate
および$group
を使用してデータのグループ化を実行します。
2.1次の例では、「hosting」フィールドでグループ化し、各ホスティングの合計を表示します。
> db.website.aggregate( { $group : {_id : "$hosting", total : { $sum : 1 }} } );
出力
{ "result" : [ { "_id" : "godaddy.com", "total" : 1 }, { "_id" : "cloud.google.com", "total" : 2 }, { "_id" : "aws.amazon.com", "total" : 4 }, { "_id" : "hostgator.com", "total" : 3 } ], "ok" : 1 }
同等のSQL。
SELECT hosting, SUM(hosting) AS total FROM website GROUP BY hosting
2.2$sort
でソートを追加します。
> db.website.aggregate( { $group : {_id : "$hosting", total : { $sum : 1 }} }, { $sort : {total : -1} } );
出力-「合計」を降順で表示します。 昇順の場合、$sort : {total : 1}
を使用します。
{ "result" : [ { "_id" : "aws.amazon.com", "total" : 4 }, { "_id" : "hostgator.com", "total" : 3 }, { "_id" : "cloud.google.com", "total" : 2 }, { "_id" : "godaddy.com", "total" : 1 } ], "ok" : 1 }
2.3$match
条件を追加し、「aws.amazon.com」の場合のみ「hosting」でグループ化します。
> db.website.aggregate( { $match : {hosting : "aws.amazon.com"} }, { $group : { _id : "$hosting", total : { $sum : 1 } } } );
出力
{ "result" : [ { "_id" : "aws.amazon.com", "total" : 4 } ], "ok" : 1 }
More Examples
より高度な集計とグループの例については、この公式のMongoDB Aggregation guideを参照してください。
3. グループ化結果をCSVまたはJSONにエクスポートします
多くの場合、グループ化の結果をcsvまたはJSON形式でエクスポートする必要があります。 これを解決するには、グループの結果を新しいコレクションに挿入し、mongoexport
を介して新しいコレクションをエクスポートします。
3.1グループの結果を変数に設定します。 この場合、変数名は「groupdata」です。
> var groupdata = db.website.aggregate( { $group : {_id : "$hosting", total : { $sum : 1 }} }, { $sort : {total : -1} } );
3.2groupdata.toArray()
を新しいコレクションに挿入します。
> db.websitegroup.insert(groupdata.toArray()); > db.websitegroup.find().pretty() { "_id" : "aws.amazon.com", "total" : 4 } { "_id" : "hostgator.com", "total" : 3 } { "_id" : "cloud.google.com", "total" : 2 } { "_id" : "godaddy.com", "total" : 1 } >
3.3コレクション「websitegroup」をcsvファイルにエクスポートします。
c:\> mongoexport -d testdb -c websitegroup -f _id,total -o group.csv --csv connected to: 127.0.0.1 exported 4 records
group.csv
_id,total "aws.amazon.com",4.0 "cloud.google.com",2.0 "godaddy.com",1.0 "hostgator.com",3.0
3.4コレクション「websitegroup」をJSONファイルにエクスポートします。
c:\> mongoexport -d testdb -c websitegroup -o group.json connected to: 127.0.0.1 exported 4 records
group.json
{ "_id" : "aws.amazon.com", "total" : 4 } { "_id" : "cloud.google.com", "total" : 2 } { "_id" : "godaddy.com", "total" : 1 } { "_id" : "hostgator.com", "total" : 3 }
4. 大規模なソート操作
Changed in version 2.6 –これを読むMemory Restrictions
MongoDBでは、メモリ内の並べ替えには1億の制限があり、大規模な並べ替えを実行するには、allowDiskUse
オプションを有効にしてデータを書き込む必要がありますソート用の一時ファイルに。
sort exceeded memory limitエラーを回避するには、allowDiskUse
オプションを有効にします。
db.website.aggregate( [ {$group : {_id : "$hosting", total : { $sum : 1 }}}, {$sort : {total : -1}} ], {allowDiskUse: true} );