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

集計

このガイドでは、 Cドライバーを使用して集計操作 を実行する方法を学習できます。

集計操作を使用して MongoDB コレクション内のデータを処理し、計算された結果を返すことができます。 クエリ API の一部である MongoDB 集計フレームワークは、データ処理パイプラインの概念をモデル化したものです。 ドキュメントは 1 つ以上の ステージを含むパイプラインに投入され、各ステージではドキュメントを変換して最終集計結果を出力します。

集計自動車操作は工場に似たものとして考えることができます。 自動車工場の組立ラインには、ドリルや溶接機のような、特定の作業をするための専用工具を備えた組立ステーションがあります。 未加工のパーツが工場に搬入され、組立ラインで完成品に加工、組み立てられます。

集計パイプラインは組み立てライン、集計ステージは組み立てステーション、演算子式は専用ツールです。

検索操作を使用して、次のアクションを実行できます。

  • どのドキュメントを返すかを選ぶ

  • どのフィールドを返すかを選ぶ

  • 結果を並べ替える

集計操作を使用して、次のアクションを実行できます。

  • 検索操作の実行

  • フィールドの名前を変更する

  • フィールドを計算する

  • データを要約する

  • 値をグループ化する

集計操作を使用する場合、次の制限が適用されます。

  • Returned documents must not violate the BSON document size limit of 16 megabytes.

  • パイプライン ステージには、デフォルトで 100 メガバイトのメモリ制限があります。allowDiskUse オプションを true に設定することで、この制限を超えることができます。

重要

$graphLookup の例外

The $graphLookup stage has a strict memory limit of 100 megabytes and ignores the allowDiskUse option.

このガイドの例では、Atlas サンプルデータセットの sample_restaurants データベースの restaurants コレクションを使用します。MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、MongoDBを使い始めるガイドを参照してください。

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 C from the Select your language drop-down menu in the upper-right corner of the page.

コレクション内のドキュメントに対して集計を実行するには、パイプラインステージを表す bson_t 構造を mongoc_collection_aggregate() 関数に渡します。

この例では、ニューヨーク市の各地区のケーキの数を出力します。 次のコードでは、次のステージを含む集計パイプラインが作成されます。

  • A $match stage to filter for documents in which the value of the cuisine field is "Bakery".

  • A $group stage to group the matching documents by the borough field, producing a count of documents for each distinct value of that field.

const bson_t *doc;
bson_t *pipeline = BCON_NEW("pipeline",
"[",
"{", "$match", "{", "cuisine", BCON_UTF8("Bakery"), "}", "}",
"{", "$group", "{",
"_id", BCON_UTF8("$borough"), "count", "{", "$sum", BCON_INT32(1), "}", "}",
"}",
"]");
mongoc_cursor_t *results =
mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
bson_error_t error;
if (mongoc_cursor_error(results, &error))
{
fprintf(stderr, "Aggregate failed: %s\n", error.message);
} else {
while (mongoc_cursor_next(results, &doc)) {
char *str = bson_as_canonical_extended_json(doc, NULL);
printf("%s\n", str);
bson_free(str);
}
}
bson_destroy(pipeline);
mongoc_cursor_destroy(results);

MongoDB が操作を実行する方法に関する情報を表示するには、パイプラインで explain操作を実行します。 MongoDB が操作 を説明すると、実行プランとパフォーマンス統計が返されます。実行プランは、 MongoDB が操作を完了できる潜在的な方法です。 MongoDB に操作を説明する ように指示すると、その操作のためにMongoDB が選択したプランと拒否された実行プランの両方が返されます。

次のコード例では、前のセクションと同じ集計を実行しますが、操作の詳細を説明するために mongoc_client_command_simple() 関数を使用しています。

bson_t reply;
bson_error_t error;
bson_t *command = BCON_NEW(
"aggregate", BCON_UTF8("restaurants"),
"explain", BCON_BOOL(true),
"pipeline",
"[",
"{", "$match", "{", "cuisine", BCON_UTF8("Bakery"), "}", "}",
"{", "$group", "{",
"_id", BCON_UTF8("$borough"), "count", "{", "$sum", BCON_INT32(1), "}", "}",
"}",
"]");
if (mongoc_client_command_simple(client, "sample_restaurants", command, NULL, &reply, &error)) {
char *str = bson_as_canonical_extended_json(&reply, NULL);
printf("%s\n", str);
bson_free(str);
} else {
fprintf(stderr, "Command failed: %s\n", error.message);
}
bson_destroy(command);
bson_destroy(&reply);

For a full list of aggregation stages, see Aggregation Stages in the MongoDB Server manual.

集計パイプラインの組み立てと例については、 マニュアルの「 集計パイプライン MongoDB Server」を参照してください。

To learn more about explaining MongoDB operations, see Explain Output and Query Plans in the MongoDB Server manual.

Cドライバーを使用して集計操作を実行する方法の詳細については、次のAPIドキュメントを参照してください。