如何在C#驱动程序中设置MongoDB Change Stream'ManageType'?

Mic*_*erg 6 c# mongodb mongodb-csharp-2.0 mongodb-.net-driver

运行新的MongDB服务器版本3.6,并尝试将更改流监视添加到集合以获取新插入和文档更新的通知时,我只接收更新通知,而不是插入.

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

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

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

我已经查看了MongoDB网站和SO上的Java和NodeJS示例,这些示例似乎很简单,并定义了一种查看插入和更新的方法:

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

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

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

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"));
Run Code Online (Sandbox Code Playgroud)

然后运行以下语句会抛出异常:

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

没有其他过滤器选项也有效,我还没有找到一种方法只需输入JSON作为字符串来设置'operationType'.

var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;
Run Code Online (Sandbox Code Playgroud)

我唯一的目标是能够使用C#驱动程序设置'operationType'.有谁知道我做错了或者用C#驱动程序试过这个并且成功了吗?

在阅读了大量网页后,关于MongoDB驱动程序的C#版本的信息很少,我非常困难!任何帮助将非常感激.

Jer*_*ers 6

这是我用来更新集合 Watch 以检索“事件”而不仅仅是文档更新的代码示例。

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();
Run Code Online (Sandbox Code Playgroud)

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

"{ $or: [ {operationType: 'replace' }, { operationType: 'insert' }, { operationType: 'update' } ] }"
Run Code Online (Sandbox Code Playgroud)

如果你想使用 $or 命令,或者

"{ operationType: /^[^d]/  }"
Run Code Online (Sandbox Code Playgroud)

在那里扔一点正则表达式。最后一个是说,我想要所有操作类型,除非它们以字母 'd' 开头。