0

I've seen examples here of people doing this same thing, but for some reason I'm getting an error.

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);
    }

}

I know next.DocumentKey isn't exactly correct, but the problem is with a.Id. I'm getting the following error:

Error CS1061 'BsonDocument' does not contain a definition for 'Id' and no accessible extension method 'Id' accepting a first argument of type 'BsonDocument' could be found (are you missing a using directive or an assembly reference?)

What am I doing wrong? Thanks!

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
Steve
  • 11
  • 1
  • The collection you are retrieving in `colDest` variable, does not seem to have `id` property. Did you check it in database ? – Manoj Choudhari Jan 22 '20 at 16:33
  • It seems that the a is expected to be of type BsonDocument, not IMongoCollection. I think what confuses me most is the fact this answer seems to suggest this should work: https://stackoverflow.com/questions/8867032/how-to-remove-one-document-by-id-using-the-official-c-sharp-driver-for-mongo?noredirect=1&lq=1 – Steve Jan 22 '20 at 19:03

0 Answers0