Change streams mongodb duplications

628 views Asked by At

I am trying 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

1

There are 1 answers

0
D. SM On BEST ANSWER

Change streams are a broadcast mechanism (one publisher (the database), many subscribers).

If you desire a queue, you need to be using a queuing system and enqueue work items into it instead of storing them into collections and watching the collections with change streams.