37
37
/**
38
38
* An {@code Aggregation} is a representation of a list of aggregation steps to be performed by the MongoDB Aggregation
39
39
* Framework.
40
- *
40
+ *
41
41
* @author Tobias Trelle
42
42
* @author Thomas Darimont
43
43
* @author Oliver Gierke
44
+ * @author Alessio Fachechi
44
45
* @since 1.3
45
46
*/
46
47
public class Aggregation {
@@ -65,7 +66,7 @@ public class Aggregation {
65
66
66
67
/**
67
68
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
68
- *
69
+ *
69
70
* @param operations must not be {@literal null} or empty.
70
71
*/
71
72
public static Aggregation newAggregation (List <? extends AggregationOperation > operations ) {
@@ -74,7 +75,7 @@ public static Aggregation newAggregation(List<? extends AggregationOperation> op
74
75
75
76
/**
76
77
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
77
- *
78
+ *
78
79
* @param operations must not be {@literal null} or empty.
79
80
*/
80
81
public static Aggregation newAggregation (AggregationOperation ... operations ) {
@@ -84,7 +85,7 @@ public static Aggregation newAggregation(AggregationOperation... operations) {
84
85
/**
85
86
* Returns a copy of this {@link Aggregation} with the given {@link AggregationOptions} set. Note that options are
86
87
* supported in MongoDB version 2.6+.
87
- *
88
+ *
88
89
* @param options must not be {@literal null}.
89
90
* @return
90
91
* @since 1.6
@@ -97,7 +98,7 @@ public Aggregation withOptions(AggregationOptions options) {
97
98
98
99
/**
99
100
* Creates a new {@link TypedAggregation} for the given type and {@link AggregationOperation}s.
100
- *
101
+ *
101
102
* @param type must not be {@literal null}.
102
103
* @param operations must not be {@literal null} or empty.
103
104
*/
@@ -107,7 +108,7 @@ public static <T> TypedAggregation<T> newAggregation(Class<T> type, List<? exten
107
108
108
109
/**
109
110
* Creates a new {@link TypedAggregation} for the given type and {@link AggregationOperation}s.
110
- *
111
+ *
111
112
* @param type must not be {@literal null}.
112
113
* @param operations must not be {@literal null} or empty.
113
114
*/
@@ -117,7 +118,7 @@ public static <T> TypedAggregation<T> newAggregation(Class<T> type, AggregationO
117
118
118
119
/**
119
120
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
120
- *
121
+ *
121
122
* @param aggregationOperations must not be {@literal null} or empty.
122
123
*/
123
124
protected Aggregation (AggregationOperation ... aggregationOperations ) {
@@ -137,7 +138,7 @@ protected static List<AggregationOperation> asAggregationList(AggregationOperati
137
138
138
139
/**
139
140
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
140
- *
141
+ *
141
142
* @param aggregationOperations must not be {@literal null} or empty.
142
143
*/
143
144
protected Aggregation (List <AggregationOperation > aggregationOperations ) {
@@ -146,7 +147,7 @@ protected Aggregation(List<AggregationOperation> aggregationOperations) {
146
147
147
148
/**
148
149
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
149
- *
150
+ *
150
151
* @param aggregationOperations must not be {@literal null} or empty.
151
152
* @param options must not be {@literal null} or empty.
152
153
*/
@@ -162,7 +163,7 @@ protected Aggregation(List<AggregationOperation> aggregationOperations, Aggregat
162
163
163
164
/**
164
165
* A pointer to the previous {@link AggregationOperation}.
165
- *
166
+ *
166
167
* @return
167
168
*/
168
169
public static String previousOperation () {
@@ -171,7 +172,7 @@ public static String previousOperation() {
171
172
172
173
/**
173
174
* Creates a new {@link ProjectionOperation} including the given fields.
174
- *
175
+ *
175
176
* @param fields must not be {@literal null}.
176
177
* @return
177
178
*/
@@ -181,7 +182,7 @@ public static ProjectionOperation project(String... fields) {
181
182
182
183
/**
183
184
* Creates a new {@link ProjectionOperation} includeing the given {@link Fields}.
184
- *
185
+ *
185
186
* @param fields must not be {@literal null}.
186
187
* @return
187
188
*/
@@ -191,7 +192,7 @@ public static ProjectionOperation project(Fields fields) {
191
192
192
193
/**
193
194
* Factory method to create a new {@link UnwindOperation} for the field with the given name.
194
- *
195
+ *
195
196
* @param fieldName must not be {@literal null} or empty.
196
197
* @return
197
198
*/
@@ -201,7 +202,7 @@ public static UnwindOperation unwind(String field) {
201
202
202
203
/**
203
204
* Creates a new {@link GroupOperation} for the given fields.
204
- *
205
+ *
205
206
* @param fields must not be {@literal null}.
206
207
* @return
207
208
*/
@@ -211,7 +212,7 @@ public static GroupOperation group(String... fields) {
211
212
212
213
/**
213
214
* Creates a new {@link GroupOperation} for the given {@link Fields}.
214
- *
215
+ *
215
216
* @param fields must not be {@literal null}.
216
217
* @return
217
218
*/
@@ -221,7 +222,7 @@ public static GroupOperation group(Fields fields) {
221
222
222
223
/**
223
224
* Factory method to create a new {@link SortOperation} for the given {@link Sort}.
224
- *
225
+ *
225
226
* @param sort must not be {@literal null}.
226
227
* @return
227
228
*/
@@ -231,7 +232,7 @@ public static SortOperation sort(Sort sort) {
231
232
232
233
/**
233
234
* Factory method to create a new {@link SortOperation} for the given sort {@link Direction} and {@code fields}.
234
- *
235
+ *
235
236
* @param direction must not be {@literal null}.
236
237
* @param fields must not be {@literal null}.
237
238
* @return
@@ -242,7 +243,7 @@ public static SortOperation sort(Direction direction, String... fields) {
242
243
243
244
/**
244
245
* Creates a new {@link SkipOperation} skipping the given number of elements.
245
- *
246
+ *
246
247
* @param elementsToSkip must not be less than zero.
247
248
* @return
248
249
*/
@@ -252,7 +253,7 @@ public static SkipOperation skip(int elementsToSkip) {
252
253
253
254
/**
254
255
* Creates a new {@link LimitOperation} limiting the result to the given number of elements.
255
- *
256
+ *
256
257
* @param maxElements must not be less than zero.
257
258
* @return
258
259
*/
@@ -262,28 +263,48 @@ public static LimitOperation limit(long maxElements) {
262
263
263
264
/**
264
265
* Creates a new {@link MatchOperation} using the given {@link Criteria}.
265
- *
266
+ *
266
267
* @param criteria must not be {@literal null}.
267
268
* @return
268
269
*/
269
270
public static MatchOperation match (Criteria criteria ) {
270
271
return new MatchOperation (criteria );
271
272
}
272
273
274
+ /**
275
+ * Creates a new {@link LookupOperation} for the given fields.
276
+ *
277
+ * @param fields must not be {@literal null}.
278
+ * @return
279
+ */
280
+ public static LookupOperation lookup (String from , String localField , String foreignField , String as ) {
281
+ return lookup (field (from ), field (localField ), field (foreignField ), field (as ));
282
+ }
283
+
284
+ /**
285
+ * Creates a new {@link LookupOperation} for the given {@link Fields}.
286
+ *
287
+ * @param fields must not be {@literal null}.
288
+ * @return
289
+ */
290
+ public static LookupOperation lookup (Field from , Field localField , Field foreignField , Field as ) {
291
+ return new LookupOperation (from , localField , foreignField , as );
292
+ }
293
+
273
294
/**
274
295
* Creates a new {@link Fields} instance for the given field names.
275
- *
276
- * @see Fields#fields(String...)
296
+ *
277
297
* @param fields must not be {@literal null}.
278
298
* @return
299
+ * @see Fields#fields(String...)
279
300
*/
280
301
public static Fields fields (String ... fields ) {
281
302
return Fields .fields (fields );
282
303
}
283
304
284
305
/**
285
306
* Creates a new {@link Fields} instance from the given field name and target reference.
286
- *
307
+ *
287
308
* @param name must not be {@literal null} or empty.
288
309
* @param target must not be {@literal null} or empty.
289
310
* @return
@@ -295,7 +316,7 @@ public static Fields bind(String name, String target) {
295
316
/**
296
317
* Creates a new {@link GeoNearOperation} instance from the given {@link NearQuery} and the{@code distanceField}. The
297
318
* {@code distanceField} defines output field that contains the calculated distance.
298
- *
319
+ *
299
320
* @param query must not be {@literal null}.
300
321
* @param distanceField must not be {@literal null} or empty.
301
322
* @return
@@ -307,7 +328,7 @@ public static GeoNearOperation geoNear(NearQuery query, String distanceField) {
307
328
308
329
/**
309
330
* Returns a new {@link AggregationOptions.Builder}.
310
- *
331
+ *
311
332
* @return
312
333
* @since 1.6
313
334
*/
@@ -317,7 +338,7 @@ public static AggregationOptions.Builder newAggregationOptions() {
317
338
318
339
/**
319
340
* Converts this {@link Aggregation} specification to a {@link DBObject}.
320
- *
341
+ *
321
342
* @param inputCollectionName the name of the input collection
322
343
* @return the {@code DBObject} representing this aggregation
323
344
*/
@@ -331,8 +352,11 @@ public DBObject toDbObject(String inputCollectionName, AggregationOperationConte
331
352
operationDocuments .add (operation .toDBObject (context ));
332
353
333
354
if (operation instanceof FieldsExposingAggregationOperation ) {
355
+ boolean additional = operation instanceof AdditionalFieldsExposingAggregationOperation ? true : false ;
356
+
334
357
FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation ) operation ;
335
- context = new ExposedFieldsAggregationOperationContext (exposedFieldsOperation .getFields (), rootContext );
358
+ context = new ExposedFieldsAggregationOperationContext (exposedFieldsOperation .getFields (), rootContext ,
359
+ additional );
336
360
}
337
361
}
338
362
@@ -356,7 +380,7 @@ public String toString() {
356
380
357
381
/**
358
382
* Simple {@link AggregationOperationContext} that just returns {@link FieldReference}s as is.
359
- *
383
+ *
360
384
* @author Oliver Gierke
361
385
*/
362
386
private static class NoOpAggregationOperationContext implements AggregationOperationContext {
@@ -391,7 +415,7 @@ public FieldReference getReference(String name) {
391
415
392
416
/**
393
417
* Describes the system variables available in MongoDB aggregation framework pipeline expressions.
394
- *
418
+ *
395
419
* @author Thomas Darimont
396
420
* @see http://docs.mongodb.org/manual/reference/aggregation-variables
397
421
*/
@@ -404,7 +428,7 @@ enum SystemVariable {
404
428
/**
405
429
* Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false}
406
430
* otherwise.
407
- *
431
+ *
408
432
* @param fieldRef may be {@literal null}.
409
433
* @return
410
434
*/
0 commit comments