Skip to content

Commit ff4cfe4

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

File tree

5 files changed

+116
-13
lines changed

5 files changed

+116
-13
lines changed

Diff for: 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 calling {@link #subscribe(Subscriber)} on 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
*

Diff for: 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
*

Diff for: 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
*

Diff for: 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

Diff for: driver-sync/src/main/com/mongodb/client/MongoIterable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface MongoIterable<TResult> extends Iterable<TResult> {
3535
/**
3636
* Returns a cursor used for iterating over elements of type {@code TResult}. The cursor is primarily used for change streams.
3737
*
38-
* @return a cursor
38+
* @return a cursor equivalent to that returned from {@link #iterator()}.
3939
* @since 3.11
4040
*/
4141
MongoCursor<TResult> cursor();

0 commit comments

Comments
 (0)