简体   繁体   中英

Unable to create watch on multiple collections mongodb

I am trying to create a watch on a collection of DB in MongoDB using mongoose construct,

collection.watch({ fullDocument: "updateLookup" })

My entire function is as below
sites.forEach(site => {
    controller.createWatchOnAuthCollection(site, watchType);
})

The problem I am facing is when I loop this function call for creating a watch on multiple collections, only the watch on the latest collection that is created actually works, and the callback is invoked, but not on the other collections. My invoking function is as below

sites.forEach(site => { controller.createWatchOnAuthCollection(site, watchType); })

Thanks in advance..:)

You cannot use the same session to create multiple change stream listeners. So, you would need to specify different sessions or use a different connection to open each stream.

Also note that having many streams opened concurrently could negatively impact performance, so it is recommended to only open one stream on the db or the connection objects and filter out the collections you want to monitor. For example:

...
collections = [];
sites.forEach(site => {
  // For each collection to watch, add a filter in the collections array
  collections.push({ "db": "auth_" + site, "coll": "units" });
});

// Create a change stream on the deployment and filter only
// the collections we want
client.watch([ { "$match": { "ns": { "$in": collections } } } ],
    { "fullDocument": "updateLookup" });
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM