Overview
このガイドでは、 Rubyドライバーを使用して集計操作を実行する方法を学習できます。
集計操作により MongoDB コレクション内のデータが処理され、計算結果が返されます。 クエリ API の一部である MongoDB 集計フレームワークは、データ処理パイプラインの概念をモデル化したものです。 ドキュメントは 1 つ以上の ステージを含むパイプラインに投入され、そこで集計結果に変換されます。
Tip
完全な集計チュートリアル
You can find tutorials that provide detailed explanations of common aggregation tasks in the Complete Aggregation Pipeline Tutorials section of the Server manual. Select a tutorial, and then pick Ruby from the Select your language drop-down menu in the upper-right corner of the page.
アナロジー
集計操作は自動車工場に似ています。工場内の組立ラインには、ドリルや溶接機のような、特定の作業をするための専用工具を備えた組立ステーションがあります。未加工のパーツが工場に搬入され、組立ラインで完成品に加工、組み立てられます。
集計パイプラインは組み立てライン、集計ステージは組み立てステーション、演算子式は専用ツールです。
集計操作と検索操作の比較
次の表は、検索操作が実行できるさまざまなタスクを示し、それらを集計操作と比較しています。 集計フレームワークは、データを変換および操作するための拡張機能を提供します。
検索操作 | 集計操作 |
|---|---|
返すドキュメントを選択する | 返すドキュメントの選択 |
制限
集計操作を実行する際には、次の制限を考慮してください。
Returned documents cannot violate the BSON document size limit of 16 megabytes.
パイプライン ステージには、デフォルトで 100 メガバイトのメモリ制限があります。 この制限を超えるには、
trueの値をallow_disk_useメソッドに渡し、そのメソッドをaggregateにチェーンします。The $graphLookup operator has a strict memory limit of 100 megabytes and ignores the value passed to the
allow_disk_usemethod.
集計操作の実行
注意
サンプル データ
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.
集計 を実行するには、各パイプラインステージをRuby hash として定義し、操作のパイプラインをaggregate メソッドに渡します。
集計の例
次のコード例では、ニューヨークの各地区のケーキの数のカウントを生成します。 そのために、次のステージを持つ集計パイプラインを使用します。
A $match stage to filter for documents whose
cuisinefield contains the value"Bakery".A $group stage to group the matching documents by the
boroughfield, accumulating a count of documents for each distinct value.
database = client.use('sample_restaurants') restaurants_collection = database[:restaurants] pipeline = [ { '$match' => { 'cuisine' => 'Bakery' } }, { '$group' => { '_id' => '$borough', 'count' => { '$sum' => 1 } } } ] aggregation = restaurants_collection.aggregate(pipeline) aggregation.each do |doc| puts doc end
集計の説明
MongoDB が操作を実行する方法に関する情報を表示するには、 MongoDBクエリ プランナーにそれを説明するように指示できます。MongoDB が操作を説明すると、実行プランとパフォーマンス統計が返されます。実行プランは、 MongoDB が操作を完了できる潜在的な方法です。MongoDB に操作を説明するよう指示すると、 MongoDBが実行したプランと拒否された実行プランの両方がデフォルトで返されます。
集計操作を説明するには、explain メソッドを aggregate メソッドに連鎖させます。
The following example instructs MongoDB to explain the aggregation operation from the preceding Aggregation Example:
explanation = restaurants_collection.aggregate(pipeline).explain() puts explanation
全文検索を実行する
1つ以上のフィールドの全文検索を指定するには、MongoDB 検索するを使用して $searchパイプラインステージを作成します。
この例では、次のアクションを実行するためのパイプラインステージを作成しています。
nameフィールドで"Salt"というタームを検索一致するドキュメントの
_idとnameの値のみをプロジェクションする
重要
次の例を実行するには、nameフィールドをカバーするrestaurantsコレクションにMongoDB検索インデックスを作成する必要があります。次に、"<your_search_index_name>" プレースホルダーをインデックスの名前に置き換えます。MongoDB 検索インデックスの作成方法については、MongoDB Search と MongoDB ベクトル検索インデックスのガイドを参照してください。
search_pipeline = [ { '$search' => { 'index' => '<your_search_index_name>', 'text' => { 'query' => 'Salt', 'path' => 'name' }, } }, { '$project' => { '_id' => 1, 'name' => 1 } } ] results = collection.aggregate(search_pipeline) results.each do |document| puts document end
詳細情報
MongoDB Server マニュアル
このガイドで説明されているトピックについて詳しくは、 MongoDB Serverマニュアルの次のページ を参照してください。
For a full list of aggregation stages, see Aggregation Stages in the MongoDB Server manual.
To learn about assembling an aggregation pipeline and to view examples, see Aggregation Pipeline.
To learn more about creating pipeline stages, see Aggregation Stages.
MongoDB 操作の説明の詳細については、「出力とクエリ プランの説明 」を参照してください。
API ドキュメント
To learn more about the Ruby driver's aggregation methods, see the API documentation for Aggregation.