Search in sources :

Example 6 with AggregationUpdate

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" + "     } } } }"));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Test(org.junit.jupiter.api.Test)

Example 7 with AggregationUpdate

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\" ] } } }")));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Test(org.junit.jupiter.api.Test)

Example 8 with AggregationUpdate

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Version(org.springframework.data.annotation.Version) ReplaceWithOperation(org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.assertj.core.api.Assertions) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Collection(java.util.Collection) EqualsAndHashCode(lombok.EqualsAndHashCode) Client(org.springframework.data.mongodb.test.util.Client) SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) Field(org.springframework.data.mongodb.core.mapping.Field) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ArithmeticOperators(org.springframework.data.mongodb.core.aggregation.ArithmeticOperators) Id(org.springframework.data.annotation.Id) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Document(org.springframework.data.mongodb.core.mapping.Document) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 9 with AggregationUpdate

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Version(org.springframework.data.annotation.Version) ReplaceWithOperation(org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.assertj.core.api.Assertions) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Collection(java.util.Collection) EqualsAndHashCode(lombok.EqualsAndHashCode) Client(org.springframework.data.mongodb.test.util.Client) SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) Field(org.springframework.data.mongodb.core.mapping.Field) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ArithmeticOperators(org.springframework.data.mongodb.core.aggregation.ArithmeticOperators) Id(org.springframework.data.annotation.Id) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 10 with AggregationUpdate

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\" ] } } }")));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)19 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)19 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)14 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Document (org.springframework.data.mongodb.core.mapping.Document)9 SetOperation (org.springframework.data.mongodb.core.aggregation.SetOperation)7 MongoClient (com.mongodb.reactivestreams.client.MongoClient)6 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)6 Arrays (java.util.Arrays)6 Collection (java.util.Collection)6 EqualsAndHashCode (lombok.EqualsAndHashCode)6 Assertions (org.assertj.core.api.Assertions)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 Id (org.springframework.data.annotation.Id)6 Version (org.springframework.data.annotation.Version)6 ArithmeticOperators (org.springframework.data.mongodb.core.aggregation.ArithmeticOperators)6 ReplaceWithOperation (org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation)6 Field (org.springframework.data.mongodb.core.mapping.Field)6