'Aggregate Pipeline to exclude collections from db.watch() MongoDB
I am using MongoDB's changeStreams to watch for changes in my database. My database has 17 collections. I'd like to watch every collection for changes except for 2 of them. 15 different changeStreams would not be ideal if I could create one changeStream for the database, excluding two collections. This pipeline is how I wish it worked.
const pipeline = [{ $match: { name: { $ne: "rules" } } },
{ $match: { name: { $ne: "notifications" } } }];
const db = client.db("myDatabase");
const changeStream = db.watch(pipeline);
Solution 1:[1]
You cannot use the pipeline to filter out collections via name from the database watcher itself. According to mongo's manual, pipeline is to "Specify a pipeline to filter/modify the change events output". If you notice within your change events, there is an ns property with the database and collection that the change occurred in. You can filter your db.watch to exclude certain matches of this ns property. To correct the pipeline in the question above:
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sam Bailor |
