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.