Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 830
Alex Crist Avatar asked Jul 08 '19 17:07

Alex Crist


People also ask

What is Changestream in MongoDB?

A change stream is a real-time stream of database changes that flows from your database to your application. With change streams, your applications can react—in real time—to data changes in a single collection, a database, or even an entire deployment.

What is resume token in MongoDB?

Resume TokensThe _id value of a change stream event document acts as the resume token: { "_data" : <BinData|hex string> }

How do I view a collection in MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database. To be able to use the command, we'll first need to select a database where at least one collection is stored.


1 Answers

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.

like image 103
kevinadi Avatar answered Oct 20 '22 19:10

kevinadi