0

I've started dealing with MongoDB change streams and have set up some streams to watch for changes on around 30 databases. I need to watch only two collections and I was wondering if I can write some pipe or filter to watch collections with the name or part of the name provided. For example I get the collection name from the namespaceDocument of ChangeStreamDocument, and I wanna watch collections that are equal to "players" for example, on all of the databases.I'm watching the whole server for changes, but now i get notification for every collection that is changed and I don't want that, only specific collections.

 final ChangeStreamPublisher<Document> publisher = client.watch(Arrays.asList(
            Aggregates.match(
                    Filters.in("operationType", Arrays.asList("insert", "update", "delete"))
            ))).fullDocument(FullDocument.UPDATE_LOOKUP);

I was thinking if I could put some filter there to make the stream watch only collections with corresponding names.

Documentatin: https://docs.mongodb.com/manual/changeStreams/

CyberZujo
  • 1
  • 1

1 Answers1

0

According to the Documentation, you can set watch on a deployment, database or collection. So, perhaps you can set watch on collections named 'players'. The other alternative would be to set additional filters in Aggregates.Match. Each changestream event returns a namespace object called 'ns', which has the database and collection name participating in the change.

user5336
  • 161
  • 5