Search in sources :

Example 6 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method useAggregationToFilterMessages.

// DATAMONGO-1803
@Test
public void useAggregationToFilterMessages() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<User> request = // 
    ChangeStreamRequest.builder(messageListener).collection(// 
    "user").filter(newAggregation(match(// 
    new Criteria().orOperator(where("user_name").is("huffyFluffy"), where("user_name").is("jellyBelly"))))).build();
    Subscription subscription = container.register(request, User.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.save(sugarSplashy);
    template.save(huffyFluffy);
    awaitMessages(messageListener);
    List<User> messageBodies = messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(2).doesNotContain(sugarSplashy);
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Criteria(org.springframework.data.mongodb.core.query.Criteria) Test(org.junit.Test)

Example 7 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method mapsTypedAggregationToFilterMessages.

// DATAMONGO-1803
@Test
public void mapsTypedAggregationToFilterMessages() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<User> request = // 
    ChangeStreamRequest.builder().collection(// 
    "user").publishTo(// 
    messageListener).filter(newAggregation(User.class, // 
    match(new Criteria().orOperator(where("userName").is("huffyFluffy"), where("userName").is("jellyBelly"))))).build();
    Subscription subscription = container.register(request, User.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.save(sugarSplashy);
    template.save(huffyFluffy);
    awaitMessages(messageListener);
    List<User> messageBodies = messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(2).doesNotContain(sugarSplashy);
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Criteria(org.springframework.data.mongodb.core.query.Criteria) Test(org.junit.Test)

Aggregations

ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)7 Test (org.junit.Test)6 BsonDocument (org.bson.BsonDocument)4 FullDocument (com.mongodb.client.model.changestream.FullDocument)3 Document (org.bson.Document)3 ChangeStreamRequestOptions (org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions)3 Criteria (org.springframework.data.mongodb.core.query.Criteria)2 Collation (com.mongodb.client.model.Collation)1 ChangeStreamOptions (org.springframework.data.mongodb.core.ChangeStreamOptions)1 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)1 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)1