Filtering change stream

I’m trying to understand how to filter the results from the change stream. Currently, each insert, update, or delete from the collection is sent to my app:

for await (const change of collection.watch()) {
    console.log(change);
}

Based on the docs, adding $match in the aggregation pipeline should allow me to pick a specific document to watch and ignore every other documents. However, this doesn’t seem to work as any updates done in the collection (whether it is on the target document, or any other document) is still being sent to my app. Can anyone help me with this? Here’s the code:

const pipeline = [ { $match: { "name": "Stella" } } ];
for await (const change of collection.watch(pipeline)) {
   console.log(change);
}

Here’s the sample document schema:

{
   _id: <some-id>,
   name: <some-name>,
   dependents: [
      {
         name: <some-name>,
         relation: <some-relation>,
      },
   ],
   date_time_created: <some-timestamp>,
}

Hi. I would recommend reading through the documentation on Change Events. The TLDR though is that MongoDB returns Change Events and if you want to filter the events returned by watch() you need to “filter” on the “change events”. This has fields like FullDocument, UpdateDescription, etc depending on the event type (update, insert, delete, replace)

Let me know if you have any specific questions but the gist is that you need to update your match expression to filter on these events