Search in sources :

Example 1 with SetOperation

use of org.springframework.data.mongodb.core.aggregation.SetOperation 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\" ] }")));
}
Also used : SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) 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

FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Document (org.bson.Document)1 Test (org.junit.jupiter.api.Test)1 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)1 SetOperation (org.springframework.data.mongodb.core.aggregation.SetOperation)1 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)1