集計パイプラインは、データ処理パイプラインの概念をモデル化したデータ集計のフレームワークです。
集計の詳細については、サーバー マニュアルの集計パイプラインを参照してください。
前提条件
このガイドのコード例を実行するには、次のコンポーネントを設定する必要があります。
ドキュメント アセットGithubの
restaurants.jsonファイルのドキュメントが入力されたtest.restaurantsコレクション。次のインポート ステートメントは次のとおりです。
import com.mongodb.reactivestreams.client.MongoClients; import com.mongodb.reactivestreams.client.MongoClient; import com.mongodb.reactivestreams.client.MongoCollection; import com.mongodb.reactivestreams.client.MongoDatabase; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.Accumulators; import com.mongodb.client.model.Projections; import com.mongodb.client.model.Filters; import org.bson.Document;
重要
このガイドでは、カスタムSubscriber実装を使用します。これについては、 カスタム サブスクリプション実装ガイドで説明されています。
MongoDB 配置への接続
まず、MongoDB 配置に接続し、 インスタンスとMongoDatabase MongoCollectionインスタンスを 宣言して定義します。
次のコードは、ポート27017のlocalhostで実行されているスタンドアロンの MongoDB 配置に接続します。 次に、 testデータベースを参照するためのdatabase変数と、 restaurantsコレクションを参照するためのcollection変数を定義します。
MongoClient mongoClient = MongoClients.create(); MongoDatabase database = mongoClient.getDatabase("test"); MongoCollection<Document> collection = database.getCollection("restaurants");
MongoDB 配置への接続の詳細については、「 MongoDB への接続」チュートリアルを参照してください。
集計の実行
集計を実行するには、集計ステージのリストをMongoCollection.aggregate()メソッドに渡します。 このドライバーは、集計ステージのビルダを含むAggregatesヘルパー クラスを提供します。
この例では、集計パイプラインは次のタスクを実行しています。
$matchステージを使用して、categories配列フィールドに要素"Bakery"を含むドキュメントをフィルタリングします。 この例では、Aggregates.match()を使用して$matchステージを構築しています。
$groupステージを使用して、一致するドキュメントをstarsフィールドでグループ化し、starsの個別の値ごとにドキュメントの数を累積します。 この例では、Aggregates.group()を使用して$groupステージを構築し、Accumulators.sum()を使用してアキュムレータ式を構築します。$groupステージ内で使用するアキュムレータ式の場合、ドライバーはAccumulatorsヘルパー クラスを提供します。
collection.aggregate( Arrays.asList( Aggregates.match(Filters.eq("categories", "Bakery")), Aggregates.group("$stars", Accumulators.sum("count", 1)) ) ).subscribe(new PrintDocumentSubscriber());
集計式の使用
$groupアキュムレータ式の場合、ドライバーはAccumulatorsヘルパー クラスを提供します。 その他の集計式は、 Documentクラスを使用して式を手動で構築します。
次の例では、集計パイプラインは$projectステージを使用して、 nameフィールドと、値がcategories配列の最初の要素である計算フィールドfirstCategoryのみを返します。 この例では、 Aggregates.project()とさまざまなProjectionsクラスのメソッドを使用して$projectステージを構築します。
collection.aggregate( Arrays.asList( Aggregates.project( Projections.fields( Projections.excludeId(), Projections.include("name"), Projections.computed( "firstCategory", new Document("$arrayElemAt", Arrays.asList("$categories", 0)) ) ) ) ) ).subscribe(new PrintDocumentSubscriber());
集計の説明
集計パイプラインを$explainするには、 AggregatePublisher.explain()メソッドを呼び出します。
collection.aggregate( Arrays.asList( Aggregates.match(Filters.eq("categories", "Bakery")), Aggregates.group("$stars", Accumulators.sum("count", 1)))) .explain() .subscribe(new PrintDocumentSubscriber());