開発者ドキュメント

MongoDB – 集計とグループの例


mongodb-group-example、width = 640、height = 214

このチュートリアルでは、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
> 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
}
  • その他の例** 詳細な集約とグループの例については、この公式のhttp://docs.mongodb.org/manual/aggregation/[MongoDB集計ガイドを参照してください。

3.グループ化結果をCSVまたはJSONにエクスポートする

多くの場合、グループ化結果をCSV形式またはJSON形式でエクスポートする必要があります。これを解決するために、グループ結果を新しいコレクションに挿入し、 `mongoexport`を介して新しいコレクションをエクスポートします。

  • 3.1 ** グループ結果を変数に設定します。この場合、変数名は “groupdata”です。

> var groupdata = db.website.aggregate(
    {
    $group : {__id : "$hosting", total : { $sum : 1 }}
    },
    {
    $sort : {total : -1}
    }
  );

  • 3.2

    ** Inserts

    groupdata.toArray()

    into a new collection.

> db.websitegroup.insert(groupdata.toArray());

> db.websitegroup.find()。pretty()
{"__id": "aws.amazon.com"、 "合計":4}
{"__id": "hostgator.com"、 "total":3}
{"__id": "cloud.google.com"、 "合計":2}
{"__id": "godaddy.com"、 "合計":1}
>
  • 3.3 ** コレクション “websitegroup”をcsvファイルにエクスポートします。

c:\> mongoexport -d testdb -cウェブサイトグループ-f __id、合計-o group.csv --csv
接続先:127.0.0.1
4レコードをエクスポート

group.csv

__id、合計
"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
接続先:127.0.0.1
4レコードをエクスポート

group.json

{"__id": "aws.amazon.com"、 "合計":4}
{"__id": "cloud.google.com"、 "合計":2}
{"__id": "godaddy.com"、 "合計":1}
{"__id": "hostgator.com"、 "total":3}

4.大きなソート演算

  • バージョン2.6で変更


    – これを読んでください

    Memory
    制限事項

    MongoDBでは、メモリ内ソートには10​​0Mの制限があり、
    大別すると、 `allowDiskUse`オプションを有効にする必要があります。
    ソート用の一時ファイル。

その
リンク://mongodb/mongodb-sort-exceeded-memory-limit-of-104857600-bytes/[sort
メモリ制限を超過しました]エラーの場合は、 `allowDiskUse`オプションを有効にしてください。

db.website.aggregate([

{$ group:{__id: "$ hosting"、合計:{$ sum:1}}}、
    {$ sort:{合計:-1}}]、
    {allowDiskUse:true}
);

===参考文献



MongoDB集約



MongoDB
db.collection.aggregate()



Aggregation
パイプラインの制限

。リンク://mongodb/mongodb-hello-world-example/[MongoDB Hello World
例]
リンク://タグ/グループ/[グループ]リンク://タグ/mongodb/[mongodb]

並べ替え

モバイルバージョンを終了