如何在多个集合上使用mongodb更改流?

时间:2019-05-28 10:57:32

标签: c# mongodb asynchronous

我试图在C#中使用mongodb编码的更改流,以监视对我的mongo的两个集合所做的插入。

我在一个集合中以及在两个集合中都完成了任务,但是创建了单独的线程,这没有问题,但是我认为这种方法是不正确的。

一个单一的收集解决方案:

range(cells(15,4), cells(15,3+rs.Fields.Count)).CopyFromRecordset rs

两个收集解决方案(线程)

class Program3
{
    static void Main(string[] args){
        CallMain(args).Wait();
        Console.ReadLine();
    }

    static async Task CallMain(string[] args){
        var client = new MongoClient("mongodb://wwww:wwwww@localhost:27017/test");
        var database = client.GetDatabase("test");
        var collection = database.GetCollection<BsonDocument>("Collection1");

        var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.Default};
        var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match("{ operationType: 'insert' }");

        var cursor = collection.Watch<ChangeStreamDocument<BsonDocument>>(pipeline, options);
        var enumerator = cursor.ToEnumerable().GetEnumerator();
        while (enumerator.MoveNext()) {
            ChangeStreamDocument<BsonDocument> doc = enumerator.Current;
            // Do something here with your document
            Console.WriteLine(doc.DocumentKey);
        }
    }   
}

我尝试使用WatchAsync方法代替Watch,但看不到任何区别。 ¿如何处理这两个集合?首选异步方法。

谢谢。

0 个答案:

没有答案