Skip to content

DATAMONGO-2311 - Add shortcut to project with AggregationExpression to Aggregation. #769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2311-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2311-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2311-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2311-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,29 @@ public static ProjectionOperation project(Fields fields) {
return new ProjectionOperation(fields);
}

/**
* Creates a new {@link ProjectionOperation} adding the given {@link AggregationExpression} as projection.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ProjectionOperation}.
* @throws IllegalArgumentException if the required argument is {@literal null}.
* @since 2.2
*/
public static ProjectionOperation project(AggregationExpression expression) {

return new ProjectionOperation(Fields.fields()) {

@Override
public Document toDocument(AggregationOperationContext context) {

Document target = super.toDocument(context);
Document $project = target.get("$project", Document.class);
$project.putAll(expression.toDocument(context));
return target;
}
};
}

/**
* Factory method to create a new {@link UnwindOperation} for the field with the given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,13 @@ public void runMatchOperationCriteriaThroughQueryMapperForUntypedAggregation() {
assertThat(groupResults.getMappedResults().size()).isEqualTo(4);
}

@Test // DATAMONGO-2311
public void projectWithAggregationExpression() {

assertThat(project(ctx -> new Document("field", 1)).toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(new Document("$project", new Document("field", 1)));
}

private void createUsersWithReferencedPersons() {

mongoTemplate.dropCollection(User.class);
Expand Down