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?
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: