Ashish
Ashish

Reputation: 14707

How to listen to multiple collections using change stream in spring

I wants to collect data from to collection_one, collection_two, collection_three etc, how do I do that ?

       ChangeStreamRequest request = ChangeStreamRequest.builder().collation(Collation.of("collection_one"))
            .filter(Aggregation.newAggregation(match(where("operationType").exists(true))))
            .publishTo(krakenDtoMessageListener)
            .build();
    container.register(request, CollectionOne.class);

Should I create multiple changeStreamRequest or one should be fine ?

Upvotes: 5

Views: 3092

Answers (2)

dnsh
dnsh

Reputation: 3633

While configuring change stream, you can specify filter on collection.

Check below java code,

List<Bson> pipeline = singletonList(Aggregates.match(
    Filters.in("ns.coll", asList("coll1", "coll2", "coll3"))
));

MongoCursor<ChangeStreamDocument<Document>> cursor = db.watch(pipeline).fullDocument(FullDocument.UPDATE_LOOKUP).iterator();

You can write similar pipeline using spring framework.

Upvotes: 2

Sameera Manorathna
Sameera Manorathna

Reputation: 618

Cannot watch for multiple collections. According to mongoDB documentation, there are 3 options. watch a specific collection, watch a database or watch the deployment (all the databases) refer mongo documentation for more details

Upvotes: 0

Related Questions