databasemongodbchangestream

How to get fullDocument from MongoDB changeStream when a document is deleted?


My Code

I have a MongoDB with two collections, Items and Calculations.

Items
  value:  Number
  date:   Date
Calculations
  calculation:  Number
  start_date:   Date
  end_date:     Date

A Calculation is a stored calcluation based off of Item values for all Items in the DB which have dates in between the Calculation's start date and end date.

Mongo Change Streams

I figure a good way to create / update Calculations is to create a Mongo Change Stream on the Items collection which listens for changes to the Items collection to then recalculate relevant Calculations.

The issue is that according to the Mongo Change Event docs, when a document is deleted, the fullDocument field is omitted which would prevent me from accessing the deleted Item's date which would inform which Calculations should be updated.

Question

Is there any way to access the fullDocument of a Mongo Change Event that was fired due to a document deletion?


Solution

  • No I don't believe there is a way. From https://docs.mongodb.com/manual/changeStreams/#event-notification:

    Change streams only notify on data changes that have persisted to a majority of data-bearing members in the replica set.

    When the document was deleted and the deletion was persisted across the majority of the nodes in a replica set, the document has ceased to exist in the replica set. Thus the changestream cannot return something that doesn't exist anymore.

    The solution to your question would be transactions in MongoDB 4.0. That is, you can adjust the Calculations and delete the corresponding Items in a single atomic operation.