Search code examples
node.jsmongodbmongoosechangestreammongoose-middleware

How to detect all changes,including deletion, made to a collection in MongoDB/Mongoose?


I have an app that uses MongoDB via mongoose. On the app there is a collection called Notifications. I would like to detect every time there is a change in that collection(including deleting a document) and take the appropriate action. I have read here that:

Like Model.remove(), this function (Model.findOneAndDelete(), Model.deleteOne()), does not trigger pre('remove') or post('remove') hooks.

I have then read here that:

Change streams provide a way for you to listen to all inserts and updates going through your MongoDB database. Note that change streams do not work unless you're connected to a MongoDB replica set.

Does change streams consider deletions as updates? How can I listen to all changes in a db including the deletion of a document?


Solution

  • Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog. Applications can use change streams to subscribe to all data changes on a single collection, a database, or an entire deployment, and immediately react to them. Because change streams use the aggregation framework, applications can also filter for specific changes or transform the notifications at will.

    Few conditions:

    1. Has to be a replica set (even a single node replica set is fine too), since there is no OpLog on a standalone.
    2. Changes mean; Delete, Update, Insert. So yes, once you subscribe, you will get all those events
    3. Reads are not change events. Obviously!
    4. Make sure you persist the resume token somewhere. So you can resume where you left off for e.g. most likely at some point your application which is waiting for change events will crash/fail/throw exceptions. So ideally you'd like to go back in time and continue from the last successful change event.
    5. It runs in the app scope, meaning that the code is written by you in your chosen language that will listen to these events. So you are responsible for managing and maintaining the state of Change Stream subscriber. From MongoDB side, it is just a cursor.