Search code examples
mongodbaggregatepipelinechangestream

Aggregate Pipeline to exclude collections from db.watch() MongoDB


I am using MongoDB's changeStreams to watch for changes in my database. I'd like to watch every collection for changes except two. Something like this:

const pipeline = [{ $match: { name: { $ne: "excludedCollection1" } } }, 
    { $match: { name: { $ne: "excludedCollection2" } } }];
const db = client.db("myDatabase");
const changeStream = db.watch(pipeline);

However, this code does not exclude the two collections.


Solution

  • You cannot filter out collections via collection name from the piepline itself. According to mongo's manual, pipeline is used to "Specify a pipeline to filter/modify the change events output". If you notice within your change events, there is an ns property that gives the namespace of the change. You can use your pipeline to exclude matches of this ns property:

      const pipeline = [
      {
        $match: {
          $and: [
            {
              ns: {
                $ne: {
                  db: "myDatabase",
                  coll: "notifications",
                },
              },
            },
            {
              ns: {
                $ne: {
                  db: "myDatabase",
                  coll: "rules",
                },
              },
            },
          ],
        },
      },
    ];
    const db = client.db("myDatabase");
    const changeStream = db.watch(pipeline);