構文:
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
> db.demo.find()
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
>
> db.demo.aggregate([{$group:{_id:"$name",num_tutorial:{$sum:1}}}])
{ "_id" : "yourName", "num_tutorial" : 1 }
{ "_id" : "hisName", "num_tutorial" : 1 }
>
同様のSQLステートメント:
select name as _id, count(*) as num_tutorial from demo group by name
表情 | 説明文 | 例 |
---|---|---|
$ sum | 合計を計算します。 | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:… roup:{_id:" name"、num_tutorial:{ いいね」}}}]) |
$ avg | 平均を計算する | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:… roup:{_id:" name"、num_tutorial:{ いいね」}}}]) |
$分 | コレクション内のすべてのドキュメントに対応する最小値を取得します。 | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:… roup:{_id:" name"、num_tutorial:{ いいね」}}}]) |
$ max | コレクション内のすべてのドキュメントの対応する値の最大値を取得します。 | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:… roup:{_id:" name"、num_tutorial:{ いいね」}}}]) |
$ push | 結果のドキュメントの配列に値を挿入します。 | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:…roup:{_id: " name"、url:{ url"}}}]) |
$ addToSet | 結果のドキュメントの配列に値を挿入しますが、コピーは作成しません。 | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:…roup:{_id: " name"、url:{ url"}}}]) |
$ first | リソース文書の順序に従って最初の文書データを取得します。 | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:… roup:{_id:" name"、first_url:{ url"}}}]) |
$ last | リソースドキュメントの順序に従って最後のドキュメントデータを取得する | db.mycol.aggregate([{ KaTeX parse error:Expected '}'、got 'EOF' at end of input:… roup:{_id:" name"、last_url:{ url"}}}]) |
-
集約フレームワークで一般的に使用されるいくつかの操作:
- $プロジェクト:入力ドキュメントの構造を変更します。フィールドの名前変更、追加、削除、計算結果の作成、ネストされたドキュメントの作成に使用できます。
- ** matchは、MongoDBの標準クエリ操作を使用します。
- $ limit **:MongoDB集約パイプラインによって返されるドキュメントの数を制限するために使用されます。
- $ skip:集約パイプラインで指定された数のドキュメントをスキップし、残りのドキュメントを返します。
- $ unwind:ドキュメントの配列型フィールドを複数の項目に分割します。各項目には配列の値が含まれます。
- $ group:統計結果に使用できるコレクション内のドキュメントをグループ化します。
- $ sort:入力ドキュメントと出力をソートします。
- $ geoNear:特定の地理的位置に近い順序付けされたドキュメントを出力します。
-
時間キーワードは次のとおりです。
- $ dayOfYear:年の日を返します(年間を通じて366日)。
- $ dayOfMonth:月の日(1〜31)を返します。
- $ dayOfWeek:曜日(1:日曜日、7:土曜日)が返されます。
- $ year:日付の年の部分を返します。
- $ month:日付の月の部分(1から12)を返します。
- $週:年の週を返します(0〜53)。
- $ hour:日付の時間の部分を返します。
- $ minute:日付の分の部分を返します。
- $秒:日付の2番目の部分を返します(日付の2番目の部分を0〜59の数値として返しますが、うるう秒を計算するには60にすることもできます)
- $ millisecond:日付のミリ秒の部分を返します(0〜999)。
- $ dateToString:{$ dateToString:{形式:、日付:}。
パイプライン演算子の例
1. $プロジェクトインスタンス
db.article.aggregate(
{ $project : {
title : 1 ,
author : 1 ,
}}
);
この場合、結果には_id、tilt、authorの3つのフィールドしかありません。デフォルトでは、_idフィールドが含まれています。_idを除外する場合は、次のようにできます:
db.article.aggregate(
{ $project : {
_id : 0 ,
title : 1 ,
author : 1
}});
2. $一致インスタンス
db.articles.aggregate( [
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
処理のためのグループ・パイプライン演算子。
3. $の例をスキップ
db.article.aggregate(
{ $skip : 5 });
$ skipパイプライン演算子による処理後、最初の5つのドキュメントは「フィルターで除外」されます。