Skip to content

Commit 09596ef

Browse files
committed
Improve the toCollection and related API documentation
JAVA-5833
1 parent 6fa95c2 commit 09596ef

File tree

10 files changed

+202
-21
lines changed

10 files changed

+202
-21
lines changed

driver-kotlin-coroutine/src/main/kotlin/com/mongodb/kotlin/client/coroutine/AggregateFlow.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ public class AggregateFlow<T : Any>(private val wrapped: AggregatePublisher<T>)
6262
public fun timeoutMode(timeoutMode: TimeoutMode): AggregateFlow<T> = apply { wrapped.timeoutMode(timeoutMode) }
6363

6464
/**
65-
* Aggregates documents according to the specified aggregation pipeline, which must end with a $out or $merge stage.
65+
* Aggregates documents according to the specified aggregation pipeline, which must end with an `$out` or `$merge`
66+
* stage. Calling this method is a preferred alternative to consuming this [AggregateFlow].
6667
*
67-
* @throws IllegalStateException if the pipeline does not end with a $out or $merge stage
68+
* @throws IllegalStateException if the pipeline does not end with an `$out` or `$merge` stage
6869
* @see [$out stage](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out/)
6970
* @see [$merge stage](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/)
7071
*/
@@ -214,5 +215,11 @@ public class AggregateFlow<T : Any>(private val wrapped: AggregatePublisher<T>)
214215
public suspend inline fun <reified R : Any> explain(verbosity: ExplainVerbosity? = null): R =
215216
explain(R::class.java, verbosity)
216217

218+
/**
219+
* Requests [AggregateFlow] to start streaming data according to the specified aggregation pipeline.
220+
* - If the aggregation pipeline ends with an `$out` or `$merge` stage, then finds all documents in the affected
221+
* namespace and emits them. You may want to use [toCollection] instead.
222+
* - Otherwise, emits no values.
223+
*/
217224
public override suspend fun collect(collector: FlowCollector<T>): Unit = wrapped.asFlow().collect(collector)
218225
}

driver-kotlin-coroutine/src/main/kotlin/com/mongodb/kotlin/client/coroutine/MapReduceFlow.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import org.bson.conversions.Bson
3333
/**
3434
* Flow implementation for map reduce operations.
3535
*
36+
* By default, the [MapReduceFlow] emits the results inline. You can write map-reduce output to a collection by using
37+
* the [collectionName] and [toCollection] methods.
38+
*
3639
* Note: Starting in MongoDB 5.0, map-reduce is deprecated, prefer Aggregation instead
3740
*
3841
* @param T The type of the result.
@@ -65,9 +68,10 @@ public class MapReduceFlow<T : Any>(private val wrapped: MapReducePublisher<T>)
6568

6669
/**
6770
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which
68-
* must specify a non-inline result.
71+
* must not emit results inline. Calling this method is a preferred alternative to consuming this [MapReduceFlow].
6972
*
7073
* @throws IllegalStateException if a collection name to write the results to has not been specified
74+
* @see collectionName
7175
*/
7276
public suspend fun toCollection() {
7377
wrapped.toCollection().awaitFirstOrNull()
@@ -80,6 +84,7 @@ public class MapReduceFlow<T : Any>(private val wrapped: MapReducePublisher<T>)
8084
*
8185
* @param collectionName the name of the collection that you want the map-reduce operation to write its output.
8286
* @return this
87+
* @see toCollection
8388
*/
8489
public fun collectionName(collectionName: String): MapReduceFlow<T> = apply {
8590
wrapped.collectionName(collectionName)
@@ -205,5 +210,12 @@ public class MapReduceFlow<T : Any>(private val wrapped: MapReducePublisher<T>)
205210
*/
206211
public fun collation(collation: Collation?): MapReduceFlow<T> = apply { wrapped.collation(collation) }
207212

213+
/**
214+
* Requests [MapReduceFlow] to start streaming data according to the specified map-reduce function with the given
215+
* options.
216+
* - If the aggregation produces results inline, then finds all documents in the affected namespace and emits them.
217+
* You may want to use [toCollection] instead.
218+
* - Otherwise, emits no values.
219+
*/
208220
public override suspend fun collect(collector: FlowCollector<T>): Unit = wrapped.asFlow().collect(collector)
209221
}

driver-kotlin-sync/src/main/kotlin/com/mongodb/kotlin/client/AggregateIterable.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,23 @@ public class AggregateIterable<T : Any>(private val wrapped: JAggregateIterable<
6161
}
6262

6363
/**
64-
* Aggregates documents according to the specified aggregation pipeline, which must end with a $out or $merge stage.
64+
* Aggregates documents according to the specified aggregation pipeline, which must end with an `$out` or `$merge`
65+
* stage. This method is the preferred alternative to [cursor].
6566
*
66-
* @throws IllegalStateException if the pipeline does not end with a $out or $merge stage
67+
* @throws IllegalStateException if the pipeline does not end with an `$out` or `$merge` stage
6768
* @see [$out stage](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out/)
6869
* @see [$merge stage](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/)
6970
*/
7071
public fun toCollection(): Unit = wrapped.toCollection()
7172

73+
/**
74+
* Aggregates documents according to the specified aggregation pipeline.
75+
* - If the aggregation pipeline ends with an `$out` or `$merge` stage, then finds all documents in the affected
76+
* namespace and returns a [MongoCursor] over them. You may want to use [toCollection] instead.
77+
* - Otherwise, returns a [MongoCursor] producing no elements.
78+
*/
79+
public override fun cursor(): MongoCursor<T> = super.cursor()
80+
7281
/**
7382
* Enables writing to temporary files. A null value indicates that it's unspecified.
7483
*

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/AggregatePublisher.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
package com.mongodb.reactivestreams.client;
1818

1919
import com.mongodb.ExplainVerbosity;
20+
import com.mongodb.MongoNamespace;
2021
import com.mongodb.annotations.Alpha;
2122
import com.mongodb.annotations.Reason;
2223
import com.mongodb.client.cursor.TimeoutMode;
24+
import com.mongodb.client.model.Aggregates;
2325
import com.mongodb.client.model.Collation;
26+
import com.mongodb.client.model.MergeOptions;
2427
import com.mongodb.lang.Nullable;
2528
import org.bson.BsonValue;
2629
import org.bson.Document;
2730
import org.bson.conversions.Bson;
2831
import org.reactivestreams.Publisher;
32+
import org.reactivestreams.Subscriber;
2933

3034
import java.util.concurrent.TimeUnit;
3135

@@ -83,13 +87,31 @@ public interface AggregatePublisher<TResult> extends Publisher<TResult> {
8387
AggregatePublisher<TResult> bypassDocumentValidation(@Nullable Boolean bypassDocumentValidation);
8488

8589
/**
86-
* Aggregates documents according to the specified aggregation pipeline, which must end with a $out stage.
90+
* Aggregates documents according to the specified aggregation pipeline, which must end with an
91+
* {@link Aggregates#out(String, String) $out} or {@link Aggregates#merge(MongoNamespace, MergeOptions) $merge} stage.
92+
* Calling this method and then {@linkplain Publisher#subscribe(Subscriber) subscribing} to the returned {@link Publisher}
93+
* is a preferred alternative to {@linkplain #subscribe(Subscriber) subscribing} to this {@link AggregatePublisher}.
8794
*
95+
* @throws IllegalStateException if the pipeline does not end with an {@code $out} or {@code $merge} stage
8896
* @return an empty publisher that indicates when the operation has completed
8997
* @mongodb.driver.manual aggregation/ Aggregation
9098
*/
9199
Publisher<Void> toCollection();
92100

101+
/**
102+
* Requests {@link AggregatePublisher} to start streaming data according to the specified aggregation pipeline.
103+
* <ul>
104+
* <li>
105+
* If the aggregation pipeline ends with an {@link Aggregates#out(String, String) $out} or
106+
* {@link Aggregates#merge(MongoNamespace, MergeOptions) $merge} stage,
107+
* then {@linkplain MongoCollection#find() finds all} documents in the affected namespace and produces them.
108+
* You may want to use {@link #toCollection()} instead.</li>
109+
* <li>
110+
* Otherwise, produces no elements.</li>
111+
* </ul>
112+
*/
113+
void subscribe(Subscriber<? super TResult> s);
114+
93115
/**
94116
* Sets the collation options
95117
*

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/MapReducePublisher.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616

1717
package com.mongodb.reactivestreams.client;
1818

19-
2019
import com.mongodb.annotations.Alpha;
2120
import com.mongodb.annotations.Reason;
2221
import com.mongodb.client.cursor.TimeoutMode;
2322
import com.mongodb.client.model.Collation;
2423
import com.mongodb.lang.Nullable;
2524
import org.bson.conversions.Bson;
2625
import org.reactivestreams.Publisher;
26+
import org.reactivestreams.Subscriber;
2727

2828
import java.util.concurrent.TimeUnit;
2929

3030
/**
3131
* Publisher for map reduce.
32+
* <p>
33+
* By default, the {@code MapReducePublisher} produces the results inline. You can write map-reduce output to a collection by using the
34+
* {@link #collectionName(String)} and {@link #toCollection()} methods.</p>
3235
*
3336
* @param <TResult> The type of the result.
3437
* @since 1.0
@@ -44,6 +47,7 @@ public interface MapReducePublisher<TResult> extends Publisher<TResult> {
4447
*
4548
* @param collectionName the name of the collection that you want the map-reduce operation to write its output.
4649
* @return this
50+
* @see #toCollection()
4751
*/
4852
MapReducePublisher<TResult> collectionName(String collectionName);
4953

@@ -152,14 +156,29 @@ public interface MapReducePublisher<TResult> extends Publisher<TResult> {
152156
MapReducePublisher<TResult> bypassDocumentValidation(@Nullable Boolean bypassDocumentValidation);
153157

154158
/**
155-
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which must specify a
156-
* non-inline result.
159+
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which must not produce
160+
* results inline. Calling this method and then {@linkplain Publisher#subscribe(Subscriber) subscribing} to the returned
161+
* {@link Publisher} is a preferred alternative to {@linkplain #subscribe(Subscriber) subscribing} to this {@link MapReducePublisher}.
157162
*
158163
* @return an empty publisher that indicates when the operation has completed
164+
* @throws IllegalStateException if a {@linkplain #collectionName(String) collection name} to write the results to has not been specified
165+
* @see #collectionName(String)
159166
* @mongodb.driver.manual aggregation/ Aggregation
160167
*/
161168
Publisher<Void> toCollection();
162169

170+
/**
171+
* Requests {@link MapReducePublisher} to start streaming data according to the specified map-reduce function with the given options.
172+
* <ul>
173+
* <li>
174+
* If the aggregation produces results inline, then {@linkplain MongoCollection#find() finds all} documents in the
175+
* affected namespace and produces them. You may want to use {@link #toCollection()} instead.</li>
176+
* <li>
177+
* Otherwise, produces no elements.</li>
178+
* </ul>
179+
*/
180+
void subscribe(Subscriber<? super TResult> s);
181+
163182
/**
164183
* Sets the collation options
165184
*

driver-scala/src/main/scala/org/mongodb/scala/AggregateObservable.scala

+24-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.mongodb.scala.bson.BsonValue
2525
import org.mongodb.scala.bson.DefaultHelper.DefaultsTo
2626
import org.mongodb.scala.bson.conversions.Bson
2727
import org.mongodb.scala.model.Collation
28+
import org.reactivestreams.Subscriber
2829

2930
import scala.concurrent.duration.Duration
3031
import scala.reflect.ClassTag
@@ -192,9 +193,13 @@ case class AggregateObservable[TResult](private val wrapped: AggregatePublisher[
192193
}
193194

194195
/**
195-
* Aggregates documents according to the specified aggregation pipeline, which must end with a `\$out` stage.
196+
* Aggregates documents according to the specified aggregation pipeline, which must end with an `\$out` or `\$merge` stage.
197+
* Calling this method and then `subscribing` to the returned [[SingleObservable]]
198+
* is a preferred alternative to subscribing to this [[AggregateObservable]].
196199
*
197200
* [[https://www.mongodb.com/docs/manual/aggregation/ Aggregation]]
201+
*
202+
* @throws java.lang.IllegalStateException if the pipeline does not end with an `\$out` or `\$merge` stage
198203
* @return an Observable that indicates when the operation has completed.
199204
*/
200205
def toCollection(): SingleObservable[Unit] = wrapped.toCollection()
@@ -257,5 +262,23 @@ case class AggregateObservable[TResult](private val wrapped: AggregatePublisher[
257262
)(implicit e: ExplainResult DefaultsTo Document, ct: ClassTag[ExplainResult]): SingleObservable[ExplainResult] =
258263
wrapped.explain[ExplainResult](ct, verbosity)
259264

265+
/**
266+
* Requests [[AggregateObservable]] to start streaming data according to the specified aggregation pipeline.
267+
*
268+
* - If the aggregation pipeline ends with an `\$out` or `\$merge` stage,
269+
* then finds all documents in the affected namespace and produces them.
270+
* You may want to use [[toCollection]] instead.
271+
* - Otherwise, produces no elements.
272+
*/
260273
override def subscribe(observer: Observer[_ >: TResult]): Unit = wrapped.subscribe(observer)
274+
275+
/**
276+
* Requests [[AggregateObservable]] to start streaming data according to the specified aggregation pipeline.
277+
*
278+
* - If the aggregation pipeline ends with an `\$out` or `\$merge` stage,
279+
* then finds all documents in the affected namespace and produces them.
280+
* You may want to use [[toCollection]] instead.
281+
* - Otherwise, produces no elements.
282+
*/
283+
override def subscribe(observer: Subscriber[_ >: TResult]): Unit = wrapped.subscribe(observer)
261284
}

driver-scala/src/main/scala/org/mongodb/scala/MapReduceObservable.scala

+26-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ import com.mongodb.client.model.MapReduceAction
2323
import com.mongodb.reactivestreams.client.MapReducePublisher
2424
import org.mongodb.scala.bson.conversions.Bson
2525
import org.mongodb.scala.model.Collation
26+
import org.reactivestreams.Subscriber
2627

2728
import scala.concurrent.duration.Duration
2829

2930
/**
3031
* Observable for map reduce.
3132
*
33+
* By default, the [[MapReduceObservable]] produces the results inline. You can write map-reduce output to a collection by using the
34+
* [[collectionName]] and [[toCollection]] methods.
35+
*
3236
* @define docsRef https://www.mongodb.com/docs/manual/reference
3337
*
3438
* @tparam TResult The type of the result.
@@ -44,6 +48,7 @@ case class MapReduceObservable[TResult](wrapped: MapReducePublisher[TResult]) ex
4448
*
4549
* @param collectionName the name of the collection that you want the map-reduce operation to write its output.
4650
* @return this
51+
* @see [[toCollection]]
4752
*/
4853
def collectionName(collectionName: String): MapReduceObservable[TResult] = {
4954
wrapped.collectionName(collectionName)
@@ -214,11 +219,14 @@ case class MapReduceObservable[TResult](wrapped: MapReducePublisher[TResult]) ex
214219
}
215220

216221
/**
217-
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which must specify a
218-
* non-inline result.
222+
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which must not produce
223+
* results inline. Calling this method and then subscribing to the returned [[SingleObservable]] is a preferred alternative to
224+
* subscribing to this [[MapReduceObservable]].
219225
*
220226
* @return an Observable that indicates when the operation has completed
221227
* [[https://www.mongodb.com/docs/manual/aggregation/ Aggregation]]
228+
* @throws java.lang.IllegalStateException if a collection name to write the results to has not been specified
229+
* @see [[collectionName]]
222230
*/
223231
def toCollection(): SingleObservable[Unit] = wrapped.toCollection()
224232

@@ -246,5 +254,21 @@ case class MapReduceObservable[TResult](wrapped: MapReducePublisher[TResult]) ex
246254
*/
247255
def first(): SingleObservable[TResult] = wrapped.first()
248256

257+
/**
258+
* Requests [[MapReduceObservable]] to start streaming data according to the specified map-reduce function with the given options.
259+
*
260+
* - If the aggregation produces results inline, then finds all documents in the
261+
* affected namespace and produces them. You may want to use [[toCollection]] instead.
262+
* - Otherwise, produces no elements.
263+
*/
249264
override def subscribe(observer: Observer[_ >: TResult]): Unit = wrapped.subscribe(observer)
265+
266+
/**
267+
* Requests [[MapReduceObservable]] to start streaming data according to the specified map-reduce function with the given options.
268+
*
269+
* - If the aggregation produces results inline, then finds all documents in the
270+
* affected namespace and produces them. You may want to use [[toCollection]] instead.
271+
* - Otherwise, produces no elements.
272+
*/
273+
override def subscribe(observer: Subscriber[_ >: TResult]): Unit = wrapped.subscribe(observer)
250274
}

driver-sync/src/main/com/mongodb/client/AggregateIterable.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package com.mongodb.client;
1818

1919
import com.mongodb.ExplainVerbosity;
20+
import com.mongodb.MongoNamespace;
2021
import com.mongodb.annotations.Alpha;
2122
import com.mongodb.annotations.Reason;
2223
import com.mongodb.client.cursor.TimeoutMode;
24+
import com.mongodb.client.model.Aggregates;
2325
import com.mongodb.client.model.Collation;
26+
import com.mongodb.client.model.MergeOptions;
2427
import com.mongodb.lang.Nullable;
2528
import org.bson.BsonValue;
2629
import org.bson.Document;
@@ -38,15 +41,47 @@
3841
public interface AggregateIterable<TResult> extends MongoIterable<TResult> {
3942

4043
/**
41-
* Aggregates documents according to the specified aggregation pipeline, which must end with a $out or $merge stage.
44+
* Aggregates documents according to the specified aggregation pipeline, which must end with an
45+
* {@link Aggregates#out(String, String) $out} or {@link Aggregates#merge(MongoNamespace, MergeOptions) $merge} stage.
46+
* This method is the preferred alternative to {@link #iterator()}, {@link #cursor()}.
4247
*
43-
* @throws IllegalStateException if the pipeline does not end with a $out or $merge stage
48+
* @throws IllegalStateException if the pipeline does not end with an {@code $out} or {@code $merge} stage
4449
* @mongodb.driver.manual reference/operator/aggregation/out/ $out stage
4550
* @mongodb.driver.manual reference/operator/aggregation/merge/ $merge stage
4651
* @since 3.4
4752
*/
4853
void toCollection();
4954

55+
/**
56+
* Aggregates documents according to the specified aggregation pipeline.
57+
* <ul>
58+
* <li>
59+
* If the aggregation pipeline ends with an {@link Aggregates#out(String, String) $out} or
60+
* {@link Aggregates#merge(MongoNamespace, MergeOptions) $merge} stage,
61+
* then {@linkplain MongoCollection#find() finds all} documents in the affected namespace and returns a {@link MongoCursor}
62+
* over them. You may want to use {@link #toCollection()} instead.</li>
63+
* <li>
64+
* Otherwise, returns a {@link MongoCursor} producing no elements.</li>
65+
* </ul>
66+
*/
67+
@Override
68+
MongoCursor<TResult> iterator();
69+
70+
/**
71+
* Aggregates documents according to the specified aggregation pipeline.
72+
* <ul>
73+
* <li>
74+
* If the aggregation pipeline ends with an {@link Aggregates#out(String, String) $out} or
75+
* {@link Aggregates#merge(MongoNamespace, MergeOptions) $merge} stage,
76+
* then {@linkplain MongoCollection#find() finds all} documents in the affected namespace and returns a {@link MongoCursor}
77+
* over them. You may want to use {@link #toCollection()} instead.</li>
78+
* <li>
79+
* Otherwise, returns a {@link MongoCursor} producing no elements.</li>
80+
* </ul>
81+
*/
82+
@Override
83+
MongoCursor<TResult> cursor();
84+
5085
/**
5186
* Enables writing to temporary files. A null value indicates that it's unspecified.
5287
*

0 commit comments

Comments
 (0)