[英]To Implement watch operation on multiple mongo databases in c# using changestreams
var options= new ChangeStreamOptions {
FullDocument = ChangeFullStreamOption.UpdateLookup
};
var enumerator = newclient
.GetDatabase(databaseName)
.Watch(options)
.ToEnumerable()
.GetEnumerator();
enumerator.MoveNext();
上面代码是看单库,需要并行看多库的更新
我建议为此使用通道,因为它们旨在同步并行源以进行处理
这看起来像这样
using MongoDB.Bson;
using MongoDB.Driver;
using System.Threading.Channels;
var dblist = new string[]{
"db1",
"db2",
"db3"
};
var options = new ChangeStreamOptions
{
FullDocument = ChangeStreamFullDocumentOption.UpdateLookup
};
var client = new MongoClient();
var watcher = Channel.CreateUnbounded<ChangeStreamDocument<BsonDocument>>();
var tokenSource = new CancellationTokenSource();
List<Task> tasks = new List<Task>();
foreach (var db in dblist)
{
//create a monitor for each database
tasks.Add(
monitorDB(db,client, watcher.Writer, tokenSource.Token)
);
}
//create the processor for your changes
var processor = processChanges(watcher.Reader);
//wait for all the monitors to complete
await Task.WhenAll(tasks);
// when all monitors have ended mark the channel as completed
watcher.Writer.Complete();
//wait for the processorto complete
await processor;
async Task monitorDB(string name, IMongoClient client, ChannelWriter<ChangeStreamDocument<BsonDocument>> writer, CancellationToken token)
{
var enumerator = client
.GetDatabase(name)
.Watch(options, token)
.ToEnumerable()
;
foreach (var change in enumerator)
{
// wait for the channel to be ready to write
await writer.WaitToWriteAsync();
// write the change to the channel
await writer.WriteAsync(change);
//if cancelled exit
if (token.IsCancellationRequested)
break;
}
}
async Task processChanges(ChannelReader<ChangeStreamDocument<BsonDocument>> reader)
{
//while the channel isn't complete, wait for a new document to be received
while(await reader.WaitToReadAsync())
{
//get the waiting document
ChangeStreamDocument<BsonDocument>> doc = await reader.ReadAsync();
//do something with doc ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.