Search in sources :

Example 1 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class MongoTemplateUpdateTests method aggregateUpdateWithUnset.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithUnset() {
    Book antelopeAntics = new Book();
    antelopeAntics.id = 1;
    antelopeAntics.title = "Antelope Antics";
    antelopeAntics.isbn = "0001122223334";
    antelopeAntics.author = new Author("Auntie", "An");
    antelopeAntics.stock = new ArrayList<>();
    antelopeAntics.stock.add(new Warehouse("A", 5));
    antelopeAntics.stock.add(new Warehouse("B", 15));
    Book beesBabble = new Book();
    beesBabble.id = 2;
    beesBabble.title = "Bees Babble";
    beesBabble.isbn = "999999999333";
    beesBabble.author = new Author("Bee", "Bumble");
    beesBabble.stock = new ArrayList<>();
    beesBabble.stock.add(new Warehouse("A", 2));
    beesBabble.stock.add(new Warehouse("B", 5));
    template.insertAll(Arrays.asList(antelopeAntics, beesBabble));
    AggregationUpdate update = AggregationUpdate.update().unset("isbn", "stock");
    template.update(Book.class).apply(update).all();
    // 
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{ \"_id\" : 1, \"title\" : \"Antelope Antics\", \"author\" : { \"last\" : \"An\", \"first\" : \"Auntie\" }, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Book\" }"), org.bson.Document.parse("{ \"_id\" : 2, \"title\" : \"Bees Babble\", \"author\" : { \"last\" : \"Bumble\", \"first\" : \"Bee\" }, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Book\" }"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 2 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class MongoTemplateUpdateTests method aggregateUpdateWithSet.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
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));
    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();
    // 
    assertThat(collection(Score.class).find(new org.bson.Document()).into(new ArrayList<>())).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.MongoTemplateUpdateTests$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.MongoTemplateUpdateTests$Score\"}"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Document(org.springframework.data.mongodb.core.mapping.Document) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 3 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class MongoTemplateUpdateTests method versionedAggregateUpdateWithSet.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void versionedAggregateUpdateWithSet() {
    Versioned source = template.insert(Versioned.class).one(new Versioned("id-1", "value-0"));
    AggregationUpdate update = AggregationUpdate.update().set("value").toValue("changed");
    template.update(Versioned.class).matching(Query.query(Criteria.where("id").is(source.id))).apply(update).first();
    assertThat(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1).into(new ArrayList<>())).containsExactly(new org.bson.Document("_id", source.id).append("version", 1L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Versioned"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Document(org.springframework.data.mongodb.core.mapping.Document) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 4 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class MongoTemplateUpdateTests method aggregationUpdateUpsertsCorrectly.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
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();
    assertThat(all(Book.class)).containsExactly(org.bson.Document.parse("{\"_id\" : 1, \"title\" : \"The Burning White\" }"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 5 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method updateShouldMapAggregationUnsetToDomainType.

// DATAMONGO-2331
@Test
void updateShouldMapAggregationUnsetToDomainType() {
    AggregationUpdate update = AggregationUpdate.update();
    update.unset("name");
    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("{ $unset : \"firstname\" }")));
}
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