如何在 C# 驱动程序中设置 MongoDB 更改流 'OperationType'?
How to set MongoDB Change Stream 'OperationType' in the C# driver?
当 运行 新的 MongDB 服务器 3.6 版,并尝试将 Change Stream 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#源代码,看看他们是怎么做到的。查看更改流监视的测试代码,他们创建了一个新文档(插入),然后立即更改该文档(更新),然后设置更改流监视以接收 'update' 通知。
没有给出有关如何监视 'insert' 通知的示例。
我在 MongoDB 网站和 SO 上查看了 Java 和 NodeJS 示例,这似乎很简单,并且定义了一种查看插入和更新的方法:
var changeStream = collection.watch({ '$match': { $or: [ { 'operationType': 'insert' }, { 'operationType': 'update' } ] } });
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"));
然后运行下面的语句抛出异常:
{"Command aggregate failed: $match with $text is only allowed as the
first pipeline stage."}
也没有其他过滤器选项起作用,而且我还没有找到一种方法来仅输入 JSON 作为字符串来设置 'operationType'.
var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;
我在这里的唯一目标是能够使用 C# 驱动程序设置 'operationType'。有谁知道我做错了什么或使用 C# 驱动程序尝试过并成功了吗?
看了很多网页,关于MongoDB驱动C#版本的资料很少,很卡!
任何帮助将不胜感激。
这是我用来更新集合 Watch 以检索 "events" 而不仅仅是文档更新的代码示例。
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() 参数也可以是:
"{ $or: [ {operationType: 'replace' }, { operationType: 'insert' }, { operationType: 'update' } ] }"
如果您想使用 $or 命令,或者
"{ operationType: /^[^d]/ }"
在其中加入一些正则表达式。最后一个是说,我想要所有的操作类型,除非它们以字母 'd'.
开头
当 运行 新的 MongDB 服务器 3.6 版,并尝试将 Change Stream 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#源代码,看看他们是怎么做到的。查看更改流监视的测试代码,他们创建了一个新文档(插入),然后立即更改该文档(更新),然后设置更改流监视以接收 'update' 通知。 没有给出有关如何监视 'insert' 通知的示例。
我在 MongoDB 网站和 SO 上查看了 Java 和 NodeJS 示例,这似乎很简单,并且定义了一种查看插入和更新的方法:
var changeStream = collection.watch({ '$match': { $or: [ { 'operationType': 'insert' }, { 'operationType': 'update' } ] } });
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"));
然后运行下面的语句抛出异常:
{"Command aggregate failed: $match with $text is only allowed as the first pipeline stage."}
也没有其他过滤器选项起作用,而且我还没有找到一种方法来仅输入 JSON 作为字符串来设置 'operationType'.
var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;
我在这里的唯一目标是能够使用 C# 驱动程序设置 'operationType'。有谁知道我做错了什么或使用 C# 驱动程序尝试过并成功了吗?
看了很多网页,关于MongoDB驱动C#版本的资料很少,很卡! 任何帮助将不胜感激。
这是我用来更新集合 Watch 以检索 "events" 而不仅仅是文档更新的代码示例。
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() 参数也可以是:
"{ $or: [ {operationType: 'replace' }, { operationType: 'insert' }, { operationType: 'update' } ] }"
如果您想使用 $or 命令,或者
"{ operationType: /^[^d]/ }"
在其中加入一些正则表达式。最后一个是说,我想要所有的操作类型,除非它们以字母 'd'.
开头