Search in sources :

Example 16 with AggregationUpdate

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

the class MongoTemplateUpdateTests method aggregateUpdateWithSetToValue.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithSetToValue() {
    Book one = new Book();
    one.id = 1;
    one.author = new Author("John", "Backus");
    template.insertAll(Arrays.asList(one));
    AggregationUpdate update = AggregationUpdate.update().set("author").toValue(new Author("Ada", "Lovelace"));
    template.update(Book.class).matching(Query.query(Criteria.where("id").is(one.id))).apply(update).all();
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"author\" : {\"first\" : \"Ada\", \"last\" : \"Lovelace\"}, \"_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 17 with AggregationUpdate

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

the class MongoTemplateUpdateTests method versionedAggregateUpdateTouchingVersionProperty.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void versionedAggregateUpdateTouchingVersionProperty() {
    Versioned source = template.insert(Versioned.class).one(new Versioned("id-1", "value-0"));
    AggregationUpdate update = AggregationUpdate.update().set(SetOperation.builder().set("value").toValue("changed").and().set("version").toValue(10L));
    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", 10L).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 18 with AggregationUpdate

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

the class MongoTemplateUpdateTests method aggregateUpdateWithReplaceWith.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithReplaceWith() {
    Book one = new Book();
    one.id = 1;
    one.author = new Author("John", "Backus");
    Book two = new Book();
    two.id = 2;
    two.author = new Author("Grace", "Hopper");
    template.insertAll(Arrays.asList(one, two));
    AggregationUpdate update = AggregationUpdate.update().replaceWith(ReplaceWithOperation.replaceWithValueOf("author"));
    template.update(Book.class).apply(update).all();
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"first\" : \"John\", \"last\" : \"Backus\"}"), org.bson.Document.parse("{\"_id\" : 2, \"first\" : \"Grace\", \"last\" : \"Hopper\"}"));
}
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 19 with AggregationUpdate

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

the class MongoTemplateUpdateTests method aggregateUpdateWithReplaceWithNewObject.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithReplaceWithNewObject() {
    Book one = new Book();
    one.id = 1;
    one.author = new Author("John", "Backus");
    Book two = new Book();
    two.id = 2;
    two.author = new Author("Grace", "Hopper");
    template.insertAll(Arrays.asList(one, two));
    AggregationUpdate update = AggregationUpdate.update().replaceWith(new Author("Ada", "Lovelace"));
    template.update(Book.class).matching(Query.query(Criteria.where("id").is(one.id))).apply(update).all();
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"first\" : \"Ada\", \"last\" : \"Lovelace\", \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Author\"}"), org.bson.Document.parse("{\"_id\" : 2, \"author\" : {\"first\" : \"Grace\", \"last\" : \"Hopper\"}, \"_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)

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