302*_*und 2 mongoose mongodb node.js changestream
我正在尝试使用 mongoose 构造在 MongoDB 中的数据库集合上创建一个监视,
collection.watch({ fullDocument: "updateLookup" })
Run Code Online (Sandbox Code Playgroud)
exports.createWatchOnAuthCollection = (site, type) => {
if (watchedTypes.includes(type + '_' + site)) {
console.log('Ignoring.. Already watch added on this type and site ' + type + '_' + site);
return;
}
dbConnectionPool.getDBConnection('auth_' + site, function (dbConnection) {
if (type == 'unit_status') {
console.log('Trying to add watch on ' + site);
var collection = dbConnection.collection('units');
collection.watch({
fullDocument: "updateLookup"
})
.on('change', function (change) {
handleStatusUpdate(site, change);
})
.on('error', function (error) {
console.log('Got a error reply')
});
watchedTypes.push(type + '_' + site);
} else if (type == 'iatas') {
}
});
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当我循环此函数调用以在多个集合上创建监视时,只有创建的最新集合上的监视实际工作,并且调用回调,但不会在其他集合上调用。我的调用函数如下
sites.forEach(site => {
controller.createWatchOnAuthCollection(site, watchType);
})
Run Code Online (Sandbox Code Playgroud)
提前致谢..:)
您不能使用同一会话创建多个更改流侦听器。因此,您需要指定不同的会话或使用不同的连接来打开每个流。
另请注意,同时打开多个流可能会对性能产生负面影响,因此建议仅在对象上打开一个流db,connection并过滤掉要监视的集合。例如:
...
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" });
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1209 次 |
| 最近记录: |