AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

集計によるデータの変換

このガイドでは、 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_use method.

注意

サンプル データ

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 cuisine field contains the value "Bakery".

  • A $group stage to group the matching documents by the borough field, 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

注意

MongoDB 検索インデックスを持つコレクションでのみ使用可能

この集計パイプライン演算子は、 MongoDB Searchインデックスを持つコレクションでのみ使用できます。

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マニュアルの次のページ を参照してください。

To learn more about the Ruby driver's aggregation methods, see the API documentation for Aggregation.