use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method updateShouldPassOnUnsetCorrectly.
// DATAMONGO-2331
@Test
void updateShouldPassOnUnsetCorrectly() {
SetOperation setOperation = SetOperation.builder().set("status").toValue("Modified").and().set("comments").toValue(Fields.fields("misc1").and("misc2").asList());
AggregationUpdate update = AggregationUpdate.update();
update.set(setOperation);
update.unset("misc1", "misc2");
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(Arrays.asList(Document.parse("{ $set: { status: \"Modified\", comments: [ \"$misc1\", \"$misc2\" ] } }"), Document.parse("{ $unset: [ \"misc1\", \"misc2\" ] }")));
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUpdateTests method versionedAggregateUpdateTouchingVersionProperty.
// DATAMONGO-2331
@Test
public void versionedAggregateUpdateTouchingVersionProperty() {
Versioned source = new Versioned("id-1", "value-0");
template.insert(Versioned.class).one(source).then().as(StepVerifier::create).verifyComplete();
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().then().as(StepVerifier::create).verifyComplete();
Flux.from(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1)).collectList().as(StepVerifier::create).consumeNextWith(it -> {
assertThat(it).containsExactly(new org.bson.Document("_id", source.id).append("version", 10L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Versioned"));
}).verifyComplete();
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUpdateTests method aggregateUpdateWithReplaceWith.
// DATAMONGO-2331
@Test
public 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)).then().as(StepVerifier::create).verifyComplete();
;
AggregationUpdate update = AggregationUpdate.update().replaceWith(ReplaceWithOperation.replaceWithValueOf("author"));
template.update(Book.class).apply(update).all().then().as(StepVerifier::create).verifyComplete();
all(Book.class).collectList().as(StepVerifier::create).consumeNextWith(it -> {
assertThat(it).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"first\" : \"John\", \"last\" : \"Backus\"}"), org.bson.Document.parse("{\"_id\" : 2, \"first\" : \"Grace\", \"last\" : \"Hopper\"}"));
}).verifyComplete();
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUpdateTests method versionedAggregateUpdateWithSet.
// DATAMONGO-2331
@Test
public void versionedAggregateUpdateWithSet() {
Versioned source = new Versioned("id-1", "value-0");
template.insert(Versioned.class).one(source).then().as(StepVerifier::create).verifyComplete();
AggregationUpdate update = AggregationUpdate.update().set("value").toValue("changed");
template.update(Versioned.class).matching(Query.query(Criteria.where("id").is(source.id))).apply(update).first().then().as(StepVerifier::create).verifyComplete();
Flux.from(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1)).collectList().as(StepVerifier::create).consumeNextWith(it -> {
assertThat(it).containsExactly(new org.bson.Document("_id", source.id).append("version", 1L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Versioned"));
}).verifyComplete();
}
use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUpdateTests method aggregateUpdateWithUnset.
// DATAMONGO-2331
@Test
public 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)).then().as(StepVerifier::create).verifyComplete();
AggregationUpdate update = AggregationUpdate.update().unset("isbn", "stock");
template.update(Book.class).apply(update).all().then().as(StepVerifier::create).verifyComplete();
all(Book.class).collectList().as(StepVerifier::create).consumeNextWith(it -> {
//
assertThat(it).containsExactlyInAnyOrder(org.bson.Document.parse("{ \"_id\" : 1, \"title\" : \"Antelope Antics\", \"author\" : { \"last\" : \"An\", \"first\" : \"Auntie\" }, \"_class\" : \"org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Book\" }"), org.bson.Document.parse("{ \"_id\" : 2, \"title\" : \"Bees Babble\", \"author\" : { \"last\" : \"Bumble\", \"first\" : \"Bee\" }, \"_class\" : \"org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Book\" }"));
}).verifyComplete();
}
Aggregations