Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Lambda表达式删除Mongo文档,c#_C#_Mongodb_Lambda_Bson - Fatal编程技术网

使用Lambda表达式删除Mongo文档,c#

使用Lambda表达式删除Mongo文档,c#,c#,mongodb,lambda,bson,C#,Mongodb,Lambda,Bson,我在这里看到过人们做同样事情的例子,但出于某种原因,我得到了一个错误 IMongoDatabase db = Globals.gdbSourceDB.GetDatabase(Globals.gsWatchDatabase); ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup }; var pipeline =

我在这里看到过人们做同样事情的例子,但出于某种原因,我得到了一个错误

IMongoDatabase db = Globals.gdbSourceDB.GetDatabase(Globals.gsWatchDatabase);

ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };

var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match("{ operationType: { $in: [ 'replace', 'insert', 'update', 'delete' ] } }");
var changeStream = db.Watch(pipeline, options).ToEnumerable().GetEnumerator();
while (changeStream.MoveNext())
{
    ChangeStreamDocument<BsonDocument> next = changeStream.Current;

    IMongoDatabase dbDest;
    IMongoCollection<BsonDocument> colDest;

    dbDest = Globals.gdbDestDB.GetDatabase(Globals.gsWatchDatabase);
    colDest = dbDest.GetCollection<BsonDocument>(next.CollectionNamespace.CollectionName);

    if (next.OperationType.ToString() == "Delete")
    {
        //Delete the same record
        var retDel = colDest.DeleteOne(a => a.Id == next.DocumentKey);
    }

}
IMongoDatabase db=Globals.gdbSourceDB.GetDatabase(Globals.gsWatchDatabase);
ChangeStreamOptions options=new ChangeStreamOptions(){FullDocument=ChangeStreamFullDocumentOption.UpdateLookup};
var pipeline=new emptypepelinedefinition().Match({operationType:{$in:['replace','insert','update','delete']}});
var changeStream=db.Watch(管道,选项).ToEnumerable().GetEnumerator();
while(changeStream.MoveNext())
{
changestreamdocumentnext=changeStream.Current;
IMONGODBDEST数据库;
伊蒙哥罗最冷;
dbDest=Globals.gdbDestDB.GetDatabase(Globals.gsWatchDatabase);
colDest=dbDest.GetCollection(next.CollectionNamespace.CollectionName);
if(next.OperationType.ToString()=“删除”)
{
//删除相同的记录
var retDel=colDest.DeleteOne(a=>a.Id==next.DocumentKey);
}
}
我知道
下一步。DocumentKey
不完全正确,但问题在于a.Id。我得到以下错误:

错误CS1061“BsonDocument”不包含“Id”的定义,并且 没有可访问的扩展方法“Id”接受类型为的第一个参数 找不到“BsonDocument”(您是否缺少using指令或 装配参考?)

我做错了什么?
谢谢

colDest
变量中检索的集合似乎没有
id
属性。你们在数据库中检查过了吗?看来a应该是BsonDocument类型,而不是IMongoCollection类型。我认为最让我困惑的是,这个答案似乎表明这应该是可行的: