I have an aggregation pipeline which includes a project like this:
$project: {
start: {
$cond: {
if: {
$eq: ["$start", "EARLY"]
},
then: "$deltastart.start",
else: "$deltastart.end"
}
},...
},...
which works fine in mongo shell. How to express this using the Aggregation framework in Spring-Mongodb? I have seen ProjectionOperationBuilder, ExpressionProjectionOperationBuilder types but not an example how to use them... any suggestions?
I add the same issue and searched on Google and this was the first result I found so I'd like to add for future readers that this feature is now available since version 1.10 RC1 with the
ConditionalOperators.Cond
class.You can read the JavaDoc here.
If using the current Spring Data release which has support for the
$cond
operator via the$project
pipeline, then this can be converted to (untested):For Spring-Data MongoDB version which do not have support for the
$cond
operator in the aggregation operation, there is a workaround which is to implement the AggregationOperation interface to take in a DBObject:Then implement the
$project
operation as a DBObject in the aggregation pipeline that is the same as the one you have:which you can then use in TypeAggregation: