Skip to content

Commit ace01e4

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-861 - Add support for $cond and $ifNull operators in aggregation operations.
We now support $cond and $ifNull operators for projection and grouping operations. ConditionalOperator and IfNullOperators are AggregationExpressions that can be applied to transform or generate values during aggregation. TypedAggregation<InventoryItem> agg = newAggregation(InventoryItem.class, project().and("discount") .transform(ConditionalOperator.newBuilder().when(Criteria.where("qty").gte(250)) .then(30) .otherwise(20)) .and(ifNull("description", "Unspecified")).as("description") ); corresponds to { "$project": { "discount": { "$cond": { "if": { "$gte": [ "$qty", 250 ] }, "then": 30, "else": 20 } }, "description": { "$ifNull": [ "$description", "Unspecified"] } } } Original Pull Request: #385
1 parent 116dda6 commit ace01e4

File tree

11 files changed

+1527
-7
lines changed

11 files changed

+1527
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,68 @@ public static LookupOperation lookup(Field from, Field localField, Field foreign
372372
return new LookupOperation(from, localField, foreignField, as);
373373
}
374374

375+
/**
376+
* Creates a new {@link IfNullOperator} for the given {@code field} and {@code replacement} value.
377+
*
378+
* @param field must not be {@literal null}.
379+
* @param replacement must not be {@literal null}.
380+
* @return never {@literal null}.
381+
* @since 1.10
382+
*/
383+
public static IfNullOperator ifNull(String field, Object replacement) {
384+
return IfNullOperator.newBuilder().ifNull(field).thenReplaceWith(replacement);
385+
}
386+
387+
/**
388+
* Creates a new {@link IfNullOperator} for the given {@link Field} and {@link Field} to obtain a value from.
389+
*
390+
* @param field must not be {@literal null}.
391+
* @param replacement must not be {@literal null}.
392+
* @return never {@literal null}.
393+
* @since 1.10
394+
*/
395+
public static IfNullOperator ifNull(Field field, Field replacement) {
396+
return IfNullOperator.newBuilder().ifNull(field).thenReplaceWith(replacement);
397+
}
398+
399+
/**
400+
* Creates a new {@link IfNullOperator} for the given {@link Field} and {@code replacement} value.
401+
*
402+
* @param field must not be {@literal null}.
403+
* @param replacement must not be {@literal null}.
404+
* @return never {@literal null}.
405+
* @since 1.10
406+
*/
407+
public static IfNullOperator ifNull(Field field, Object replacement) {
408+
return IfNullOperator.newBuilder().ifNull(field).thenReplaceWith(replacement);
409+
}
410+
411+
/**
412+
* Creates a new {@link ConditionalOperator} for the given {@link Field} that holds a {@literal boolean} value.
413+
*
414+
* @param booleanField must not be {@literal null}.
415+
* @param then must not be {@literal null}.
416+
* @param otherwise must not be {@literal null}.
417+
* @return never {@literal null}.
418+
* @since 1.10
419+
*/
420+
public static ConditionalOperator conditional(Field booleanField, Object then, Object otherwise) {
421+
return ConditionalOperator.newBuilder().when(booleanField).then(then).otherwise(otherwise);
422+
}
423+
424+
/**
425+
* Creates a new {@link ConditionalOperator} for the given {@link Criteria}.
426+
*
427+
* @param criteria must not be {@literal null}.
428+
* @param then must not be {@literal null}.
429+
* @param otherwise must not be {@literal null}.
430+
* @return never {@literal null}.
431+
* @since 1.10
432+
*/
433+
public static ConditionalOperator conditional(Criteria criteria, Object then, Object otherwise) {
434+
return ConditionalOperator.newBuilder().when(criteria).then(then).otherwise(otherwise);
435+
}
436+
375437
/**
376438
* Creates a new {@link Fields} instance for the given field names.
377439
*

0 commit comments

Comments
 (0)