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.
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.
Is there any way to access the fullDocument
of a Mongo Change Event that was fired due to a document deletion?
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.
Resume TokensThe _id value of a change stream event document acts as the resume token: { "_data" : <BinData|hex string> }
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With