Aggregation on historic data, based on every change stream on On Premise MongoDB

I have been exploring the change stream API and workarounds for my usecase.
What I have understood is that I can detect any change in my collection via change stream “watch” function. I am trying to figure out that how can I read two or more fields from a collection and apply aggregation in the collection and get some result and merge result to another collection. All this based on every change stream event occurring. Every time a change occurs in the collection, I want to re-aggregate the data and store it in another collection again via upsert.

I can do that by Triggers but these are supported in Atlas Only. What other options can I possibly utilize to get this done on my on Premise MongoDB.

Hello @MWD_Wajih_N_A, Change Stream is the only option for the requirement you have:

Every time a change occurs in the collection, I want to re-aggregate the data and store it in another collection again via upsert.

Yes, I am exploring it. As per my understanding, it will aggregate only the incoming record where as I am looking for a way to re aggregate the whole data.
For Instance, consider There are two collections A and B. There are three fields in A i.e., xid, xinvoice, xamount. Now, one xid can have multiple xinvoice with certain xamount. A want to sum all amounts and store it in B. Now, if any update occur in any of the xinvoice, I want to re aggregate all xinvoice’s amount as per the updated value and store this updated sum in the same field in B.
This aggregation involves the historic data in the collection and updating only changed field. Not just aggregating the change stream document but the whole data.

The Change Events document has the details of the change (e.g., an update event has info about the document key and the updated / removed fields). This is the information you can use to perform further querying (or aggregation) and the update to another collection. You can define a transaction and perform the aggregation and update atomically.