Per Steffensen
Per Steffensen

Reputation: 633

Mongo Change Stream giving me ALL changes to the collection?

Reading about MongoDB Change Streams. Guess it can be used to get ALL changes to a collection streamed to "my component". But that may introduce a significant overhead. It am actually only interested in a very small subset of the changes. I have read that you can filter (using $match), but how is that working

Just looking for a simple a) or b) answer. But a little additional comments will not hurt.

P.S. The changes I am interested in from a collection changes dynamically, so it will not be a solution to make sure that all "interesting" documents goes in to a dedicated collection and then just stream changes from that. But if it is not too expensive it can create/delete stream-change-watches per "aspect" I am interested in as they change. One established stream-change-watch does not necessarily have to support changing its filter.

Upvotes: 1

Views: 3366

Answers (1)

Wan B.
Wan B.

Reputation: 18835

Just looking for a simple a) or b) answer. But a little additional comments will not hurt.

MongoDB Change Streams is implemented as an aggregation pipeline stage: $changeStream. This is why you can also utilise the power of aggregation pipeline by stringing different pipeline stages.i.e. $match.

As you may know, the aggregation pipeline is performed server-side. Which answers your question; the filter $match is performed on the server.

Please note that all events that are returned by change streams are at least committed to the majority of the replica set nodes (durable). This means, that the aggregation is only executed on the node you're opening change stream against.

The changes I am interested in from a collection changes dynamically,

Depending on your use case, I would suggest to filter based on field that does not change dynamically.

You may find Using Change Streams to Keep Up with Your Data useful.

Upvotes: 1

Related Questions