Search code examples
mongodbchangestream

Change streams mongodb duplications


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


Solution

  • 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.