MongoDB aggregation pipeline output change stream

Viewed 238

I am using MongoDB's aggregation pipeline to generate a new collection B containing aggregated results from collection A. To this purpose I use the $out stage. Every time I run the aggregation pipeline, new documents might be added, some might be updated and some removed.

I would now like to have a change stream over the aggregated collection B in order to be notified when the aggregation generates different results from the previous one (i.e. at least one insertion/update/remove).

However, if I use the $out stage, the collection is recreated on every execution and I get a rename and invalidate change and then the stream is closed. I can use start_after with a resume token to open the stream again but I am not notified of the changes (rename and invalidate).

I tried using $merge to avoid recreating the collection. The change stream is working as I expect but I can no longer delete old documents from collection B.

Is there a way to make my use case work (i.e. the result of the aggregation pipeline is the new content of the collection + get change notification for insert/remove/update from previous collection content)?

1 Answers

$out does not diff the new result set vs the previous contents of a collection. It drops the previous contents and inserts new documents.

Therefore there is nothing in MongoDB that knows which documents were added to B and which were removed. I don't see how you would be able to get this information via a change stream on B.

You are going to need to come up with another solution I'm afraid.

Related