Not receiving any ChangeStreamDocuments after performing an update to a collection using .net/c# driver

Is there a way to debug why a watched collection is not returning any change stream document when iterating through the change stream cursor using the .net/C# driver? I’m not receiving any exception, and the code just keeps iterating through the batches of documents(MoveNext()) without any document in those batches (cursor.Current.Count() ).

Prior to posting here, I checked if I could watch the exact same collection using mongoDB shell(mongosh), and I was able to successfully receive the update notifications.

I’m using the exact same user to perform the watch from .net/C# and mongosh

Following is a sample C# code used to test :

           var var _cursor = _collection.Watch()
            var updateResult = await _collection.UpdateOneAsync(filter, updateBuilder.Combine(updateList));

                while (_cursor.MoveNext() && _cursor.Current.Count() == 0)
                {
                    foreach (var item in _cursor.Current)
                    {
                        Console.WriteLine("Hello!");
                        Console.WriteLine(item);
                    }
                }
            _cursor.dispose();

            return updateResult.ModifiedCount == 1;

You have two var definitions in the first line. Also the while condition has .count() == 0 in the condition, so if document come through it’ll jump over the while loop surely?

so, it was a logic error, which I failed to realize in this case. I was too hung up on checking the count of each batch of change stream documents, which I think was unnecessary. The corrected code that actually worked in my case is as follows:

            var _cursor = _collection.Watch()
            var updateResult = await _collection.UpdateOneAsync(filter, updateBuilder.Combine(updateList));

                while (_cursor.MoveNext())
                {
                    foreach (var item in _cursor.Current)
                    {
                        Console.WriteLine("Hello!");
                        Console.WriteLine(item);
                    }
                }
            _cursor.dispose();

            return updateResult.ModifiedCount == 1;

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.