@@ -42,7 +42,7 @@ await cursor.ForEachAsync(change =>
4242 . Match ( change => change . OperationType == ChangeStreamOperationType . Update ) ;
4343
4444// Opens a change stream and prints the changes as they're received
45- using ( var cursor = await _restaurantsCollection . WatchAsync ( pipeline ) )
45+ using ( var cursor = await collection . WatchAsync ( pipeline ) )
4646{
4747 await cursor . ForEachAsync ( change =>
4848 {
@@ -56,7 +56,7 @@ await cursor.ForEachAsync(change =>
5656 . Match ( change => change . OperationType == ChangeStreamOperationType . Update ) ;
5757
5858// Opens a change streams and print the changes as they're received
59- using ( var cursor = _restaurantsCollection . Watch ( pipeline ) )
59+ using ( var cursor = collection . Watch ( pipeline ) )
6060{
6161 foreach ( var change in cursor . ToEnumerable ( ) )
6262 {
@@ -65,6 +65,115 @@ await cursor.ForEachAsync(change =>
6565}
6666// end-change-stream-pipeline
6767
68+ // start-split-event-helpers-sync
69+ // Fetches the next complete change stream event
70+ private static IEnumerable < ChangeStreamDocument < TDocument > > GetNextChangeStreamEvent < TDocument > (
71+ IEnumerator < ChangeStreamDocument < TDocument > > changeStreamEnumerator )
72+ {
73+ while ( changeStreamEnumerator . MoveNext ( ) )
74+ {
75+ var changeStreamEvent = changeStreamEnumerator . Current ;
76+ if ( changeStreamEvent . SplitEvent != null )
77+ {
78+ var fragment = changeStreamEvent ;
79+ while ( fragment . SplitEvent . Fragment < fragment . SplitEvent . Of )
80+ {
81+ changeStreamEnumerator . MoveNext ( ) ;
82+ fragment = changeStreamEnumerator . Current ;
83+ MergeFragment ( changeStreamEvent , fragment ) ;
84+ }
85+ }
86+ yield return changeStreamEvent ;
87+ }
88+ }
89+
90+ // Merges a fragment into the base event
91+ private static void MergeFragment < TDocument > (
92+ ChangeStreamDocument < TDocument > changeStreamEvent ,
93+ ChangeStreamDocument < TDocument > fragment )
94+ {
95+ foreach ( var element in fragment . BackingDocument )
96+ {
97+ if ( element . Name != "_id" && element . Name != "splitEvent" )
98+ {
99+ changeStreamEvent . BackingDocument [ element . Name ] = element . Value ;
100+ }
101+ }
102+ }
103+ // end-split-event-helpers-sync
104+
105+ // start-split-event-helpers-async
106+ // Fetches the next complete change stream event
107+ private static async IAsyncEnumerable < ChangeStreamDocument < TDocument > > GetNextChangeStreamEventAsync < TDocument > (
108+ IAsyncCursor < ChangeStreamDocument < TDocument > > changeStreamCursor )
109+ {
110+ var changeStreamEnumerator = GetNextChangeStreamEventFragmentAsync ( changeStreamCursor ) . GetAsyncEnumerator ( ) ;
111+ while ( await changeStreamEnumerator . MoveNextAsync ( ) )
112+ {
113+ var changeStreamEvent = changeStreamEnumerator . Current ;
114+ if ( changeStreamEvent . SplitEvent != null )
115+ {
116+ var fragment = changeStreamEvent ;
117+ while ( fragment . SplitEvent . Fragment < fragment . SplitEvent . Of )
118+ {
119+ await changeStreamEnumerator . MoveNextAsync ( ) ;
120+ fragment = changeStreamEnumerator . Current ;
121+ MergeFragment ( changeStreamEvent , fragment ) ;
122+ }
123+ }
124+ yield return changeStreamEvent ;
125+ }
126+ }
127+
128+ private static async IAsyncEnumerable < ChangeStreamDocument < TDocument > > GetNextChangeStreamEventFragmentAsync < TDocument > (
129+ IAsyncCursor < ChangeStreamDocument < TDocument > > changeStreamCursor )
130+ {
131+ while ( await changeStreamCursor . MoveNextAsync ( ) )
132+ {
133+ foreach ( var changeStreamEvent in changeStreamCursor . Current )
134+ {
135+ yield return changeStreamEvent ;
136+ }
137+ }
138+ }
139+
140+ // Merges a fragment into the base event
141+ private static void MergeFragment < TDocument > (
142+ ChangeStreamDocument < TDocument > changeStreamEvent ,
143+ ChangeStreamDocument < TDocument > fragment )
144+ {
145+ foreach ( var element in fragment . BackingDocument )
146+ {
147+ if ( element . Name != "_id" && element . Name != "splitEvent" )
148+ {
149+ changeStreamEvent . BackingDocument [ element . Name ] = element . Value ;
150+ }
151+ }
152+ }
153+ // end-split-event-helpers-async
154+
155+ // start-split-change-event-sync
156+ var pipeline = new EmptyPipelineDefinition < ChangeStreamDocument < Restaurant > > ( )
157+ . ChangeStreamSplitLargeEvent ( ) ;
158+
159+ using var cursor = collection . Watch ( pipeline ) ;
160+ foreach ( var completeEvent in GetNextChangeStreamEvent ( cursor . ToEnumerable ( ) . GetEnumerator ( ) ) )
161+ {
162+ Console . WriteLine ( "Received the following change: " + completeEvent . BackingDocument ) ;
163+ }
164+ // end-split-change-event-sync
165+
166+ // start-split-change-event-async
167+ var pipeline = new EmptyPipelineDefinition < ChangeStreamDocument < Restaurant > > ( )
168+ . ChangeStreamSplitLargeEvent ( ) ;
169+
170+ using var cursor = await collection . WatchAsync ( pipeline ) ;
171+ await foreach ( var completeEvent in GetNextChangeStreamEventAsync ( cursor ) )
172+ {
173+ Console . WriteLine ( "Received the following change: " + completeEvent . BackingDocument ) ;
174+ }
175+ // end-split-change-event-async
176+
68177// start-change-stream-post-image
69178var pipeline = new EmptyPipelineDefinition < ChangeStreamDocument < Restaurant > > ( )
70179 . Match ( change => change . OperationType == ChangeStreamOperationType . Update ) ;
@@ -74,7 +183,7 @@ await cursor.ForEachAsync(change =>
74183 FullDocument = ChangeStreamFullDocumentOption . UpdateLookup ,
75184} ;
76185
77- using ( var cursor = _restaurantsCollection . Watch ( pipeline , options ) )
186+ using ( var cursor = collection . Watch ( pipeline , options ) )
78187{
79188 foreach ( var change in cursor . ToEnumerable ( ) )
80189 {
@@ -92,7 +201,7 @@ await cursor.ForEachAsync(change =>
92201 FullDocument = ChangeStreamFullDocumentOption . UpdateLookup ,
93202} ;
94203
95- using var cursor = await _restaurantsCollection . WatchAsync ( pipeline , options ) ;
204+ using var cursor = await collection . WatchAsync ( pipeline , options ) ;
96205await cursor . ForEachAsync ( change =>
97206{
98207 Console . WriteLine ( change . FullDocument . ToBsonDocument ( ) ) ;
0 commit comments