Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d73f992

Browse files
committedApr 9, 2025·
Improve the toCollection and related API documentation
JAVA-5833
1 parent 6fa95c2 commit d73f992

File tree

7 files changed

+169
-16
lines changed

7 files changed

+169
-16
lines changed
 

‎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
*

‎driver-sync/src/main/com/mongodb/client/MapReduceIterable.java

+33-6
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
/**
2929
* Iterable for map-reduce.
30-
*
31-
* <p>By default the {@code MapReduceIterable} returns the results inline. You can write map-reduce output to a collection by using the
32-
* {@link MapReduceIterable#collectionName(String)} method.</p>
30+
* <p>
31+
* By default, the {@code MapReduceIterable} produces the results inline. You can write map-reduce output to a collection by using the
32+
* {@link #collectionName(String)} and {@link #toCollection()} methods.</p>
3333
*
3434
* @param <TResult> The type of the result.
3535
* @since 3.0
@@ -39,22 +39,49 @@
3939
public interface MapReduceIterable<TResult> extends MongoIterable<TResult> {
4040

4141
/**
42-
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which must specify a
43-
* non-inline result.
42+
* Aggregates documents to a collection according to the specified map-reduce function with the given options, which must not produce
43+
* results inline. This method is the preferred alternative to {@link #iterator()}, {@link #cursor()}.
4444
*
45-
* @throws IllegalStateException if a collection name to write the results to has not been specified
45+
* @throws IllegalStateException if a {@linkplain #collectionName(String) collection name} to write the results to has not been specified
4646
* @see #collectionName(String)
4747
* @since 3.4
4848
*/
4949
void toCollection();
5050

51+
/**
52+
* Aggregates documents according to the specified map-reduce function with the given options.
53+
* <ul>
54+
* <li>
55+
* If the aggregation produces results inline, then {@linkplain MongoCollection#find() finds all} documents in the
56+
* affected namespace and returns a {@link MongoCursor} over them. You may want to use {@link #toCollection()} instead.</li>
57+
* <li>
58+
* Otherwise, returns a {@link MongoCursor} producing no elements.</li>
59+
* </ul>
60+
*/
61+
@Override
62+
MongoCursor<TResult> iterator();
63+
64+
/**
65+
* Aggregates documents according to the specified map-reduce function with the given options.
66+
* <ul>
67+
* <li>
68+
* If the aggregation produces results inline, then {@linkplain MongoCollection#find() finds all} documents in the
69+
* affected namespace and returns a {@link MongoCursor} over them. You may want to use {@link #toCollection()} instead.</li>
70+
* <li>
71+
* Otherwise, returns a {@link MongoCursor} producing no elements.</li>
72+
* </ul>
73+
*/
74+
@Override
75+
MongoCursor<TResult> cursor();
76+
5177
/**
5278
* Sets the collectionName for the output of the MapReduce
5379
*
5480
* <p>The default action is replace the collection if it exists, to change this use {@link #action}.</p>
5581
*
5682
* @param collectionName the name of the collection that you want the map-reduce operation to write its output.
5783
* @return this
84+
* @see #toCollection()
5885
*/
5986
MapReduceIterable<TResult> collectionName(String collectionName);
6087

‎driver-sync/src/main/com/mongodb/client/MongoIterable.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
*/
3030
public interface MongoIterable<TResult> extends Iterable<TResult> {
3131

32+
/**
33+
* @return A {@link MongoCursor} that must be {@linkplain MongoCursor#close() closed}.
34+
*/
3235
@Override
3336
MongoCursor<TResult> iterator();
3437

3538
/**
3639
* Returns a cursor used for iterating over elements of type {@code TResult}. The cursor is primarily used for change streams.
3740
*
38-
* @return a cursor
41+
* @return a cursor equivalent to that returned from {@link #iterator()}.
3942
* @since 3.11
4043
*/
4144
MongoCursor<TResult> cursor();

0 commit comments

Comments
 (0)
Please sign in to comment.