use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method updateShouldAllowMultipleAggregationExpressions.
// DATAMONGO-2331
@Test
void updateShouldAllowMultipleAggregationExpressions() {
AggregationUpdate update = //
AggregationUpdate.update().set("average").toValue(//
ArithmeticOperators.valueOf("tests").avg()).set("grade").toValue(//
ConditionalOperators.switchCases(//
CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(90)).then("A"), //
CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(80)).then("B"), //
CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(70)).then("C"), //
CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(60)).then("D")).defaultTo(//
"F"));
template.updateFirst(new BasicQuery("{}"), update, Wrapper.class).subscribe();
ArgumentCaptor<List<Document>> captor = ArgumentCaptor.forClass(List.class);
verify(collection, times(1)).updateOne(any(org.bson.Document.class), captor.capture(), any(UpdateOptions.class));
assertThat(captor.getValue()).containsExactly(Document.parse("{ $set: { average : { $avg: \"$tests\" } } }"), Document.parse("{ $set: { grade: { $switch: {\n" + " branches: [\n" + " { case: { $gte: [ \"$average\", 90 ] }, then: \"A\" },\n" + " { case: { $gte: [ \"$average\", 80 ] }, then: \"B\" },\n" + " { case: { $gte: [ \"$average\", 70 ] }, then: \"C\" },\n" + " { case: { $gte: [ \"$average\", 60 ] }, then: \"D\" }\n" + " ],\n" + " default: \"F\"\n" + " } } } }"));
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method updateShouldAllowAggregationExpressions.
// DATAMONGO-2331
@Test
void updateShouldAllowAggregationExpressions() {
AggregationUpdate update = AggregationUpdate.update().set("total").toValue(ArithmeticOperators.valueOf("val1").sum().and("val2"));
template.updateFirst(new BasicQuery("{}"), update, Wrapper.class).subscribe();
ArgumentCaptor<List<Document>> captor = ArgumentCaptor.forClass(List.class);
verify(collection, times(1)).updateOne(any(org.bson.Document.class), captor.capture(), any(UpdateOptions.class));
assertThat(captor.getValue()).isEqualTo(Collections.singletonList(Document.parse("{ $set : { total : { $sum : [ \"$val1\",\"$val2\" ] } } }")));
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUpdateTests method aggregateUpdateWithSet.
// DATAMONGO-2331
@Test
public void aggregateUpdateWithSet() {
Score score1 = new Score(1, "Maya", Arrays.asList(10, 5, 10), Arrays.asList(10, 8), 0);
Score score2 = new Score(2, "Ryan", Arrays.asList(5, 6, 5), Arrays.asList(8, 8), 8);
template.insertAll(Arrays.asList(score1, score2)).then().as(StepVerifier::create).verifyComplete();
AggregationUpdate update = AggregationUpdate.update().set(//
SetOperation.builder().set("totalHomework").toValueOf(ArithmeticOperators.valueOf("homework").sum()).and().set("totalQuiz").toValueOf(//
ArithmeticOperators.valueOf("quiz").sum())).set(//
SetOperation.builder().set("totalScore").toValueOf(ArithmeticOperators.valueOf("totalHomework").add("totalQuiz").add("extraCredit")));
template.update(Score.class).apply(update).all().then().as(StepVerifier::create).verifyComplete();
Flux.from(collection(Score.class).find(new org.bson.Document())).collectList().as(StepVerifier::create).consumeNextWith(it -> {
//
assertThat(it).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"student\" : \"Maya\", \"homework\" : [ 10, 5, 10 ], \"quiz\" : [ 10, 8 ], \"extraCredit\" : 0, \"totalHomework\" : 25, \"totalQuiz\" : 18, \"totalScore\" : 43, \"_class\" : \"org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Score\"}"), org.bson.Document.parse("{ \"_id\" : 2, \"student\" : \"Ryan\", \"homework\" : [ 5, 6, 5 ], \"quiz\" : [ 8, 8 ], \"extraCredit\" : 8, \"totalHomework\" : 16, \"totalQuiz\" : 16, \"totalScore\" : 40, \"_class\" : \"org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Score\"}"));
}).verifyComplete();
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUpdateTests method aggregationUpdateUpsertsCorrectly.
// DATAMONGO-2331
@Test
public void aggregationUpdateUpsertsCorrectly() {
AggregationUpdate update = AggregationUpdate.update().set("title").toValue("The Burning White");
template.update(Book.class).matching(Query.query(Criteria.where("id").is(1))).apply(update).upsert().then().as(StepVerifier::create).verifyComplete();
all(Book.class).collectList().as(StepVerifier::create).consumeNextWith(it -> {
assertThat(it).containsExactly(org.bson.Document.parse("{\"_id\" : 1, \"title\" : \"The Burning White\" }"));
}).verifyComplete();
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method updateShouldMapAggregationExpressionToDomainType.
// DATAMONGO-2331
@Test
void updateShouldMapAggregationExpressionToDomainType() {
AggregationUpdate update = AggregationUpdate.update().set("name").toValue(ArithmeticOperators.valueOf("val1").sum().and("val2"));
template.updateFirst(new BasicQuery("{}"), update, Jedi.class).subscribe();
ArgumentCaptor<List<Document>> captor = ArgumentCaptor.forClass(List.class);
verify(collection, times(1)).updateOne(any(org.bson.Document.class), captor.capture(), any(UpdateOptions.class));
assertThat(captor.getValue()).isEqualTo(Collections.singletonList(Document.parse("{ $set : { firstname : { $sum:[ \"$val1\",\"$val2\" ] } } }")));
}
Aggregations