MongoDB Change Streams Spring Boot Application (language: Kotlin)

I was just wondering if anyone can help me with my question. I want to use Change Streams to observe my collection if any change (CRUD) has occurred, then send a message to rabbitmq broker (including fields id and market). Notice: I’m using a Spring Boot application with Kotlin. I did not find any similiar solutions so far. Thank you in advance!

Hello Max: @wan has suggested this could be nice example in Java for you to get started, but easy enough that you can port in Kotlin.

Oops!

try (MongoClient mongoClient = MongoClients.create(uri)) {

            MongoDatabase database = mongoClient.getDatabase("sample_mflix");
            MongoCollection<Document> collection = database.getCollection("movies");

            List<Bson> pipeline = Arrays.asList( Aggregates.match( Filters.in("operationType",                             
            Arrays.asList("insert", "update"))));
            ChangeStreamIterable<Document> changeStream = database.watch(pipeline)
                .fullDocument(FullDocument.UPDATE_LOOKUP);
            // variables referenced in a lambda must be final; final array gives us a mutable integer
            final int[] numberOfEvents = {0};
            changeStream.forEach(event -> {
            System.out.println("Received a change to the collection: " + event);
                if (++numberOfEvents[0] >= 2) {
                  System.exit(0);
                }
            });
        }

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.