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\" }"));
}
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\"}"));
}
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"));
}
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\" }"));
}
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\" }")));
}
Aggregations