Skip to content

Instantly share code, notes, and snippets.

@timjonesdev
Created August 20, 2019 22:47
Show Gist options
  • Save timjonesdev/a22c903baa23de719d6d124cbde46e79 to your computer and use it in GitHub Desktop.
Save timjonesdev/a22c903baa23de719d6d124cbde46e79 to your computer and use it in GitHub Desktop.
The main implementation for watching changes on a MongoDB Collection
/**
* Watch for changes to the teams collection
*
* @return a subscription to the change stream
*/
public Flux<Team> watchForTeamCollectionChanges() {
// set changestream options to watch for any changes to the businesses collection
ChangeStreamOptions options = ChangeStreamOptions.builder()
.filter(Aggregation.newAggregation(Team.class,
Aggregation.match(
Criteria.where("operationType").is("replace")
)
)).returnFullDocumentOnUpdate().build();
// return a flux that watches the changestream and returns the full document
return reactiveMongoTemplate.changeStream("teams", options, Team.class)
.map(ChangeStreamEvent::getBody)
.doOnError(throwable -> log.error("Error with the teams changestream event: " + throwable.getMessage(), throwable));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment