Overview
このガイドでは、 Java Reactive Streams ドライバーのビルダを使用してプロジェクションを指定する方法を学びます。
MongoDB はフィールドプロジェクションをサポートしており、クエリから結果を返すときに含めるフィールドと除外するフィールドを指定します。 MongoDB のプロジェクションは、いくつかの基本的なルールに従います。
_idフィールドは明示的に除外されない限り、常に含まれます含めるフィールドを指定すると、
_idフィールドを除く他のすべてのフィールドが暗黙的に除外されます除外するフィールドを指定すると、クエリ結果内のそのフィールドのみが削除されます
プロジェクションの仕組みの詳細については、マニュアルの「 クエリ結果からのプロジェクト フィールド 」チュートリアルを参照してください。
Projectionsクラスは、すべてのMongoDBプロジェクション演算子の静的ファクトリー メソッドを提供します。各メソッドはBSON型のインスタンスを返します。このインスタンスは、プロジェクション を必要とする任意のメソッドに渡すことができます。
Tip
サンプル ドキュメントと例
次のセクションでは、 projection_buildersというサンプル コレクションに対してクエリとプロジェクション操作を実行する例を紹介します。 各セクションでは、 collectionという名前の変数を使用して、 projection_buildersコレクションのMongoCollectionインスタンスを参照します。
コレクションには、2018 年と 2019 年の月間平均温度を摂氏単位で表す次のドキュメントが含まれています。
{ "year" : 2018, "type" : "even number but not a leap year", "temperatures" : [ { "month" : "January", "avg" : 9.765 }, { "month" : "February", "avg" : 9.675 }, { "month" : "March", "avg" : 10.004 }, { "month" : "April", "avg" : 9.983 }, { "month" : "May", "avg" : 9.747 }, { "month" : "June", "avg" : 9.65 }, { "month" : "July", "avg" : 9.786 }, { "month" : "August", "avg" : 9.617 }, { "month" : "September", "avg" : 9.51 }, { "month" : "October", "avg" : 10.042 }, { "month" : "November", "avg" : 9.452 }, { "month" : "December", "avg" : 9.86 } ] }, { "year" : 2019, "type" : "odd number, can't be a leap year", "temperatures" : [ { "month" : "January", "avg" : 10.023 }, { "month" : "February", "avg" : 9.808 }, { "month" : "March", "avg" : 10.43 }, { "month" : "April", "avg" : 10.175 }, { "month" : "May", "avg" : 9.648 }, { "month" : "June", "avg" : 9.686 }, { "month" : "July", "avg" : 9.794 }, { "month" : "August", "avg" : 9.741 }, { "month" : "September", "avg" : 9.84 }, { "month" : "October", "avg" : 10.15 }, { "month" : "November", "avg" : 9.84 }, { "month" : "December", "avg" : 10.366 } ] }
プロジェクション操作
次のセクションには、利用可能なプロジェクション操作と、 Projectionsクラスを使用してそれらを構築する方法に関する情報が記載されています。
包含
1 つ以上のフィールドを含めるように指定するには、 include()メソッドを使用します。
次の例には、 yearフィールドと(暗黙的に) _idフィールドが含まれています。
Bson filter = Filters.empty(); Bson projection = include("year"); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}
次の例には、year、type、および暗黙的に _id フィールドが含まれています。
Bson filter = Filters.empty(); Bson projection = include("year", "type"); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}
Exclusion
1 つ以上のフィールドの除外を指定するには、 exclude()メソッドを使用します。
次の例では、 temperaturesフィールドが除外されています。
Bson filter = Filters.empty(); Bson projection = exclude("temperatures"); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}
次の例では、type フィールドとtemperatures フィールドが除外されています。
Bson filter = Filters.empty(); Bson projection = exclude("temperatures", "type"); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}
プロジェクションの組み合わせ
複数のプロジェクションを結合するには、 fields()メソッドを使用します。
次の例には、 yearフィールドとtypeフィールドが含まれ、 _idフィールドは除外されています。
Bson filter = Filters.empty(); Bson projection = fields(include("year", "type"), exclude("_id")); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"year": 2018, "type": "even number but not a leap year"} {"year": 2019, "type": "odd number, can't be a leap year"}
除外: _id
excludeId()便利メソッドを使用して、 _idフィールドの除外を指定します。
Bson filter = Filters.empty(); Bson projection = fields(include("year", "type"), excludeId()); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"year": 2018, "type": "even number but not a leap year"} {"year": 2019, "type": "odd number, can't be a leap year"}
配列要素一致のプロジェクション
elemMatch(String, Bson)メソッド バリアントを使用して、指定されたクエリフィルターに一致する配列の最初の要素を含む配列プロジェクションを指定します。 このフィルタリングは、クエリフィルター(指定された場合)に一致するすべてのドキュメントが取得された後に発生します。
注意
一致の数に関係なく、指定されたクエリフィルターに一致する最初の要素のみが含まれます。
次の例では、 avgフィールドが 10.1 より大きい temperatures 配列の最初の要素を投影します。
Bson filter = Filters.empty(); Bson projection = fields(include("year"), elemMatch("temperatures", Filters.gt("avg", 10.1))); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018} {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}
操作のクエリ部分で一致条件を指定した場合、 elemMatch(String)メソッド バリアントを使用して、配列の最初の要素を含める位置プロジェクションを指定します。 クエリフィルターに一致するドキュメントのみが検索されます。
重要
MongoDBでは、4.4 より前のバージョンでは、指定された配列フィールドがクエリフィルターに表示される必要があります。MongoDB 4.4 以降では、クエリフィルターに表示されない配列フィールドで位置プロジェクトを使用できます。
次の例えでは、 temperatures配列の最初の要素をプロジェクションします。
Bson filter = Filters.gt("temperatures.avg", 10.1); Bson projection = fields(include("year"), elemMatch("temperatures")); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}
配列スライスのプロジェクション
配列のスライスをプロジェクションするには、 slice()メソッドを使用します。
次の例では、 temperatures配列の最初の6 つの要素をプロジェクションします。
Bson filter = Filters.empty(); Bson projection = slice("temperatures", 6); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build()))) .blockLast();
{ "_id": { "$oid": "6042f1bc8ee6fa2a84d2be69" }, "year": 2018, "type": "even number but not a leap year", "temperatures": [ ... <January-June temperature nested documents> ] } { "_id": { "$oid": "6042f1bc8ee6fa2a84d2be6a" }, "year": 2019, "type": "odd number, can't be a leap year", "temperatures": [ ... <January-June temperature nested documents> ] }
次の例では、 temperatures配列の最初の6 つの要素をスキップし、次の6つのプロジェクションをプロジェクションします。
Bson filter = Filters.empty(); Bson projection = slice("temperatures", 6, 6); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build()))) .blockLast();
{ "_id": { "$oid": "6042f1bc8ee6fa2a84d2be69" }, "year": 2018, "type": "even number but not a leap year", "temperatures": [ ... <July-December temperature nested documents> ] } { "_id": { "$oid": "6042f1bc8ee6fa2a84d2be6a" }, "year": 2019, "type": "odd number, can't be a leap year", "temperatures": [ ... <July-December temperature nested documents> ] }
テキスト スコアのプロジェクション
テキスト クエリのスコアのプロジェクションを指定するには、 metaTextScore()メソッドを使用します。
次の例では、テキスト スコアをscoreフィールドの値としてプロジェクションします。
Bson filter = Filters.text("even number"); Bson projection = fields(include("year"), metaTextScore("score")); FindPublisher<Document> findPublisher = collection.find(filter).projection(projection); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{"_id": {"$oid": "6042f1bc8ee6fa2a84d2be69"}, "year": 2018, "score": 1.25} {"_id": {"$oid": "6042f1bc8ee6fa2a84d2be6a"}, "year": 2019, "score": 0.625}