如何在C#驱动程序中设置MongoDB Change Stream'OperationType'? [英] How to set MongoDB Change Stream 'OperationType' in the C# driver?

查看:204
本文介绍了如何在C#驱动程序中设置MongoDB Change Stream'OperationType'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当运行新的MongDB Server版本3.6并尝试向集合中添加Change Stream监视以获取有关新插入和文档更新的通知时,我仅收到更新通知,而不是插入通知.

When running the new MongDB Server, version 3.6, and trying to add a Change Stream watch to a collection to get notifications of new inserts and updates of documents, I only receive notifications for updates, not for inserts.

这是我尝试添加手表的默认方式:

This is the default way I have tried to add the watch:

IMongoDatabase mongoDatabase = mongoClient.GetDatabase("Sandbox");
IMongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>("TestCollection");
var changeStream = collection.Watch().ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;

然后我从MongoDB下载了C#源代码,以了解他们是如何做到的.他们查看更改流监视的测试代码,创建一个新文档(插入),然后立即更改该文档(更新),然后设置更改流监视以接收更新"通知. 没有关于如何监视插入"通知的示例.

Then I downloaded the C# source code from MongoDB to see how they did this. Looking at their test code for change stream watches, they create a new document(Insert) and then change that document right away(Update) and THEN set up the Change Stream watch to receive an 'update' notification. No example is given on how to watch for 'insert' notifications.

我已经在MongoDB网站和SO上查看了Java和NodeJS示例,这看起来很简单,并且定义了一种查看插入和更新的方式:

I have looked at the Java and NodeJS examples, both on MongoDB website and SO, which seems to be straight forward and defines a way to see both Inserts and Updates:

var changeStream = collection.watch({ '$match': { $or: [ { 'operationType': 'insert' }, { 'operationType': 'update' } ] } });

C#驱动程序的API截然不同,我以为他们会为Java和NodeJS保留相同的C#API.我发现C#可以做同样的事情的例子很少或很少.

The API for the C# driver is vastly different, I would have assumed they would have kept the same API for C# as Java and NodeJS. I found no or very few examples for C# to do the same thing.

我最接近的是以下尝试,但是仍然失败,并且C#版本的文档非常有限(或者我找不到正确的位置).设置如下:

The closest I have come is with the following attempt but still fails and the documentation for the C# version is very limited (or I have not found the right location). Setup is as follows:

String json = "{ '$match': { 'operationType': { '$in': ['insert', 'update'] } } }";
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };

PipelineDefinition<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>> pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match(Builders<ChangeStreamDocument<BsonDocument>>.Filter.Text(json,"json"));

然后运行下面的语句将引发异常:

Then running the statement below throws an Exception:

{命令聚合失败:仅允许将$ match与$ text用作 第一个管道阶段.}

{"Command aggregate failed: $match with $text is only allowed as the first pipeline stage."}

其他过滤器选项也没有起作用,并且我还没有找到一种仅将JSON作为字符串输入来设置"operationType"的方法.

No other Filter options has worked either, and I have not found a way to just enter the JSON as a string to set the 'operationType'.

var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;

我在这里的唯一目标是能够使用C#驱动程序设置"operationType".有人知道我在做什么错吗,或者已经使用C#驱动程序尝试过并成功了吗?

My only goal here is to be able to set the 'operationType' using the C# driver. Does anyone know what I am doing wrong or have tried this using the C# driver and had success?

尽管阅读了大量网页,但对MongoDB驱动程序的C#版本的了解却很少,我非常困惑! 任何帮助将不胜感激.

After reading though a large number of webpages, with very little info on the C# version of the MongoDB driver, I am very stuck! Any help would be much appreciated.

推荐答案

这里是我用来更新集合Watch的代码示例,以检索事件",而不仅仅是文档更新.

Here is a sample of code I've used to update the collection Watch to retrieve "events" other than just document updates.

IMongoDatabase sandboxDB = mongoClient.GetDatabase("Sandbox");
IMongoCollection<BsonDocument> collection = sandboxDB.GetCollection<BsonDocument>("TestCollection");

//Get the whole document instead of just the changed portion
ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };

//The operationType can be one of the following: insert, update, replace, delete, invalidate
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match("{ operationType: { $in: [ 'replace', 'insert', 'update' ] } }");

var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();    //Blocks until a document is replaced, inserted or updated in the TestCollection
ChangeStreamDocument<BsonDocument> next = changeStream.Current;
enumerator.Dispose();

EmptyPiplineDefinition ... Match()参数也可以是:

The EmptyPiplineDefinition...Match() argument could also be:

"{ $or: [ {operationType: 'replace' }, { operationType: 'insert' }, { operationType: 'update' } ] }"

如果要使用$ or命令,或

If you wanted to use the $or command, or

"{ operationType: /^[^d]/  }"

在那里放一些正则表达式.最后一句话是说,我希望所有的operationTypes都不要以字母"d"开头.

to throw a little regex in there. This last one is saying, I want all operationTypes unless they start with the letter 'd'.

这篇关于如何在C#驱动程序中设置MongoDB Change Stream'OperationType'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆