Mongodb-aggregation
提供:Dev Guides
MongoDB-集約
集計操作はデータレコードを処理し、計算結果を返します。 集約操作は、複数のドキュメントの値をグループ化し、グループ化されたデータに対してさまざまな操作を実行して、単一の結果を返すことができます。 SQLではcount(*)とgroup byはMongoDB集計と同等です。
aggregate()メソッド
MongoDBでの集計には、* aggregate()*メソッドを使用する必要があります。
構文
- aggregate()*メソッドの基本的な構文は次のとおりです-
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
例
コレクションには、次のデータがあります-
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http://www.finddevguides.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
_id: ObjectId(7df78ad8902d)
title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'tutorials point',
url: 'http://www.finddevguides.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 10
},
{
_id: ObjectId(7df78ad8902e)
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 750
},
今、上記のコレクションから、各ユーザーが作成したチュートリアルの数を示すリストを表示する場合は、次の* aggregate()*メソッドを使用します-
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{ "_id" : "tutorials point", "num_tutorial" : 2 }
{ "_id" : "Neo4j", "num_tutorial" : 1 }
>
上記のユースケースのSQL同等のクエリは、by_user のmycol groupから select by_user、count(*)になります。
上記の例では、 by_user フィールドごとにドキュメントをグループ化し、ユーザーごとに発生するたびに、以前の値sumがインクリメントされます。 以下は利用可能な集約式のリストです。
Expression | Description | Example |
---|---|---|
$sum | Sums up the defined value from all documents in the collection. | db.mycol.aggregate([\{$group : \{_id : "$by_user", num_tutorial : \{$sum : "$likes"}}}]) |
$avg | Calculates the average of all given values from all documents in the collection. | db.mycol.aggregate([\{$group : \{_id : "$by_user", num_tutorial : \{$avg : "$likes"}}}]) |
$min | Gets the minimum of the corresponding values from all documents in the collection. | db.mycol.aggregate([\{$group : \{_id : "$by_user", num_tutorial : \{$min : "$likes"}}}]) |
$max | Gets the maximum of the corresponding values from all documents in the collection. | db.mycol.aggregate([\{$group : \{_id : "$by_user", num_tutorial : \{$max : "$likes"}}}]) |
$push | Inserts the value to an array in the resulting document. | db.mycol.aggregate([\{$group : \{_id : "$by_user", url : \{$push: "$url"}}}]) |
$addToSet | Inserts the value to an array in the resulting document but does not create duplicates. | db.mycol.aggregate([\{$group : \{_id : "$by_user", url : \{$addToSet : "$url"}}}]) |
$first | Gets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage. | db.mycol.aggregate([\{$group : \{_id : "$by_user", first_url : \{$first : "$url"}}}]) |
$last | Gets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage. | db.mycol.aggregate([\{$group : \{_id : "$by_user", last_url : \{$last : "$url"}}}]) |
パイプラインのコンセプト
UNIXコマンドでは、シェルパイプラインとは、ある入力で操作を実行し、その出力を次のコマンドの入力などとして使用する可能性を意味します。 MongoDBは、集計フレームワークでも同じ概念をサポートしています。 可能なステージのセットがあり、それらの各ステージは入力としてドキュメントのセットとして取得され、ドキュメントの結果セット(またはパイプラインの最後に最終的な結果のJSONドキュメント)を生成します。 これは、次のステージなどに順番に使用できます。
集約フレームワークで可能な段階は次のとおりです-
- $ project -コレクションから特定のフィールドを選択するために使用されます。
- $ match -これはフィルタリング操作であるため、次の段階への入力として与えられるドキュメントの量を減らすことができます。
- $ group -これは上記のように実際の集約を行います。
- $ sort -ドキュメントをソートします。
- $ skip -これにより、一定量のドキュメントのドキュメントのリストを前方にスキップすることができます。
- $ limit -これは、現在の位置から始まる指定された数で、見るドキュメントの量を制限します。
- $ unwind -これは配列を使用しているドキュメントを巻き戻すために使用されます。 配列を使用する場合、データは一種の事前結合されており、この操作は元に戻され、個々のドキュメントが再び取得されます。 したがって、この段階では、次の段階のドキュメントの量を増やします。