Change stream watching the same target

Hi,

i am trying to c dynamically create change streams. And i am wondering if there is way to avoid create change streams watching the same target?

db.collection('Test' + ).watch().
        on('change', data => console.log(new Date(), data));

db.collection('Test' + ).watch().
        on('change', data => console.log(new Date(), data));

If there is now some change in Test collection i will receive notification twice. Since I am sending notifications next to SQS i will have duplicated messages.

Is there a way check if change stream with target ‘xy’ already exist, to avoid have same watchers ?

Thank you

Hello @Jan_Ambroz, welcome to the MongoDB community forum.

I think you can track the collection by its name in your application. You can either store the collection name in a data structure (e.g., an array) or even in a collection and check the store if the collection is being watched. This means, your application includes logic to store, and then remove the name of the collection depending upon its being watched or not.