Search code examples
c#mongodbchangestream

Unable to get MongoDB Change stream documents to appear in a watcher service


I have a service that needs to watch a collection on a Mongo DB to create changes in the system. I have managed to establish a connection to a replica set using the C# driver and I'm using the following code to test the change stream.

public async Task WatchLoopAsync()
{
    var options = new ChangeStreamOptions
    {
        FullDocument = ChangeStreamFullDocumentOption.UpdateLookup,
    };
            
    using (var cursor = await _collection.WatchAsync(options))
    {
        _logger.LogInformation("Watching collection {String}", 
            _deployments.CollectionNamespace);
                
        await cursor.ForEachAsync(changeStreamDocument =>
        {
            var document = changeStreamDocument.FullDocument;
            _logger.LogInformation("Received document: {String}", 
                document.ToString());
        });
    }
}

The first log appears stating that it is watching the collection with the correct namespace. I then add a document to the collection expecting to see something log as "Received document: ..." but nothing logs.

I followed the async pattern given in the documentation here.


Solution

  • The solution to my particular problem was that the change stream receiving code was throwing an exception when it couldn't match a property "name" to an object property of "Name" due to the property casing conventions in C#.

    To fix this, I used the following code snippet:

    var conventionPack = new ConventionPack
    {
        new CamelCaseElementNameConvention()
    };
    ConventionRegistry.Register("camelCase", conventionPack, t => true);
    

    This registers the DB driver to use the camel casing conventions when mapping entities to BSON.