Getting old values using change stream

Recently I learned about change streams and how powerful they are, but I’m in need of getting the previous values before an update. However, I seem to have learned through some research that it’s impossible to get the previous values? So this got me thinking about what alternatives are there in order to retrieve those previous values?

What I want to achieve is a logging system such as, “Record A field has changed from {old_value} to {new_value}.” I’m using to push these updates to a react front-end client. The updates to records would be happening from a completely different system and not on the same blackened server where the change streams would be listening so I won’t be able to query the document before updating.

So I started to think of a different solution…maybe I could have two databases? One contains the old records and the other the updated records but this sounds like a duplication of data. And I can’t imagine having thousands of records.

I need some guidance as I really don’t know what the best option is? Is there really no way you can use change streams and get the previous values? Is it possible to somehow query the document before a change stream event? Thank you.

1 Like

Hi @seniorcluckers welcome to the community!

There is a ticket for the feature you need in SERVER-36941

Unfortunately while waiting for this ticket to be resolved, the old values would need to be handled by the app. This would be very workload dependent, so I’m afraid there’s no “best” way to achieve this. While having two databases sounds overkill, it might be the correct solution for some applications. Alternatively, you could record the previous value in the same document (using something like {field_x: <current value>, field_x_old: <old value>} or similar). Those are just some possibilities off the top of my head, so it may not be applicable to you, or not even a good solution :slight_smile:

Best regards