マングース:集計関数を実装するaggregate()メソッド



Mongoose Aggregate Method Implement Aggregate Functions



もともと含まれています https://szhshp.org/tech/2019/09/07/mongodbaggregatefunc.html 、 ご指定ください

要件の説明

最初に私はアイテムのコレクションを持っています:



type Item { _id: String itemname: String! itemid: String! itemtype: String! parent_id: String } /////////////////// /// Mainly the following parts type Query { getItemSummary: ItemSummaryResponse } type ItemSummaryResponse { data: [ItemSummary] success: Boolean } type ItemSummary { _id: String count: Int }

gql側で呼び出されるメソッド:

{ getItemSummary { data{ _id count } success } }

次に、カテゴリ(アイテムタイプ)に従って合計します。



したがって、実際には、これをバックグラウンドのマングースで記述する必要があります。

getItemSummary: root => Item.aggregate([{ $group: { _id: '$itemtype', count: { $sum: 1, }, }, }, ]).then((res) => { if (res === null) { return { success: 0, errors: ['sadfsdfsdf'] } } return ({ success: 1, errors: [], data: res, }) }),

集計関数aggregate()の使用

最初にどちらを指定しますfield集計

$group: { _id: '$itemtype', // This place is more important. First, the left side must be written as _id, and finally when the front end is fetched through gql, it is also written as _id // The other important thing is that the value of this key is $itemtype, indicating that grouping is based on itemtype, just write $ + fieldname directly count: { $sum: 1, }, },

集計する必要がある$ groupの列は、_idと記述する必要があるためです。 、それ以外の場合は表示されますThe field 'xxx' must be an accumulator objectエラーメッセージ



さらに、SQL集計関数はここで使用できます。

集約パイプライン

パイプは通常、UnixおよびLinuxで使用され、現在のコマンドの出力を次のコマンドのパラメーターとして受け取ります。

基本的に、集計メソッドのメソッドは1つずつ実行されます。

上記の集計関数は、1つのフィールドの集計のみを実行します。

Item.aggregate([{ $group: { _id: '$itemtype', count: { $sum: 1 } } }, ])

ただし、実際には、これらの操作の前後に操作を追加できます。

たとえば、グループ化する前に70ポイントから90ポイントの間のデータを除外する場合は、次のようにします。

db.articles.aggregate([{ $match: { score: { $gt: 70, $lte: 90 } } }, { $group: { _id: null, count: { $sum: 1 } } } ])

一般的に使用されるいくつかの操作

  • $ project:入力ドキュメントの構造を変更します。フィールドの名前の変更、追加、削除に使用できます。また、計算結果やネストされたドキュメントの作成にも使用できます。
  • $ match:データをフィルタリングし、条件を満たすドキュメントのみを出力するために使用されます。 MongoDBの標準クエリ操作を使用します。
  • $ limit:MongoDB集約パイプラインによって返されるドキュメントの数を制限するために使用されます。
  • $ skip:集計パイプラインで指定された数のドキュメントをスキップし、残りのドキュメントを返します。
  • $ unwind:ドキュメント内の配列型フィールドを複数の部分に分割します。各部分には配列内の1つの値が含まれます。
  • $ group:コレクション内のドキュメントをグループ化します。これは統計結果に使用できます。
  • $ sort:入力ドキュメントを並べ替えて出力します。
  • $ geoNear:特定の地理的位置に近い注文済みドキュメントを出力します。

インスタンス

$ projectインスタンス

0は表示されず、1が表示されます。デフォルトでは、_idフィールドは1です。

db.articles.aggregate({ $project: { _id: 0, title: 1, by_user: 1, } }) // return { 'title' : 'MongoDB Overview', 'by_user' : 'runoob.com' } { 'title' : 'NoSQL Overview', 'by_user' : 'runoob.com' } { 'title' : 'Neo4j Overview', 'by_user' : 'Neo4j' }

$ matchインスタンス

GetTakeでの一致の使用分数Bigin 70smallinまたはWaitin 90 Rememberレコード、もちろん、RearはCombine Article Pieces of Rememberレコードを1つの注文の下で配布するセグメントマッチは、スコアが70を超えるレコードを取得するために使用されます。 90以下の場合、条件を満たすレコードを次のステージに送信しますグループパイプラインオペレーターが処理を実行します。

db.articles.aggregate([{ $match: { score: { $gt: 70, $lte: 90 } } }, { $group: { _id: null, count: { $sum: 1 } } } ]) // return { '_id' : null, 'count' : 1 }

$ skipインスタンス

$ skipパイプライン演算子が処理された後、最初の2つのドキュメントは「フィルターで除外」されます。

db.col_1.aggregate({ $skip: 2 })

参照

https://www.jianshu.com/p/baea1bce6de3