diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
index 18f48b574..dadd535e1 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
import org.bson.Document;
@@ -177,6 +178,48 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
return new ProjectionOperation(this.projections, FieldProjection.from(fields, true));
}
+ /**
+ * Includes the current {@link ProjectionOperation} as an array with given name.
+ * If you want to specify array values directly use {@link #andArrayOf(Object...)}.
+ *
+ * @param name the target property name.
+ * @return new instance of {@link ProjectionOperation}.
+ * @since 2.2
+ */
+ public ProjectionOperation asArray(String name) {
+
+ return new ProjectionOperation(Collections.emptyList(),
+ Collections.singletonList(new ArrayProjection(Fields.field(name), (List) this.projections)));
+ }
+
+ /**
+ * Includes the given values ({@link Field field references}, {@link AggregationExpression expression}, plain values)
+ * as an array.
+ * The target property name needs to be set via {@link ArrayProjectionOperationBuilder#as(String)}.
+ *
+ * @param values must not be {@literal null}.
+ * @return new instance of {@link ArrayProjectionOperationBuilder}.
+ * @throws IllegalArgumentException if the required argument it {@literal null}.
+ * @since 2.2
+ */
+ public ArrayProjectionOperationBuilder andArrayOf(Object... values) {
+
+ ArrayProjectionOperationBuilder builder = new ArrayProjectionOperationBuilder(this);
+
+ for (Object value : values) {
+
+ if (value instanceof Field) {
+ builder.and((Field) value);
+ } else if (value instanceof AggregationExpression) {
+ builder.and((AggregationExpression) value);
+ } else {
+ builder.and(value);
+ }
+ }
+
+ return builder;
+ }
+
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
@@ -1743,4 +1786,95 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
return context.getMappedObject(projections, type);
}
}
+
+ /**
+ * @author Christoph Strobl
+ * @since 2.2
+ */
+ public static class ArrayProjectionOperationBuilder {
+
+ private ProjectionOperation target;
+ private final List