Skip to content

Commit a19d77c

Browse files
CSHARP-2651: Update change stream docs example for resume token access
1 parent 49d23ae commit a19d77c

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

tests/MongoDB.Driver.Examples/ChangeStreamExamples.cs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
2020
using MongoDB.Driver.Tests;
2121
using System;
22+
using System.Linq;
2223
using System.Threading;
2324
using System.Threading.Tasks;
2425
using Xunit;
@@ -44,10 +45,10 @@ public void ChangeStreamExample1()
4445
.Start();
4546

4647
// Start Changestream Example 1
47-
var enumerator = inventory.Watch().ToEnumerable().GetEnumerator();
48-
enumerator.MoveNext();
49-
var next = enumerator.Current;
50-
enumerator.Dispose();
48+
var cursor = inventory.Watch();
49+
while (cursor.MoveNext() && cursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch
50+
var next = cursor.Current.First();
51+
cursor.Dispose();
5152
// End Changestream Example 1
5253

5354
next.FullDocument.Should().Be(document);
@@ -74,10 +75,10 @@ public void ChangeStreamExample2()
7475

7576
// Start Changestream Example 2
7677
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
77-
var enumerator = inventory.Watch(options).ToEnumerable().GetEnumerator();
78-
enumerator.MoveNext();
79-
var next = enumerator.Current;
80-
enumerator.Dispose();
78+
var cursor = inventory.Watch(options);
79+
while (cursor.MoveNext() && cursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch
80+
var next = cursor.Current.First();
81+
cursor.Dispose();
8182
// End Changestream Example 2
8283

8384
var expectedFullDocument = document.Set("x", 2);
@@ -98,7 +99,7 @@ public void ChangeStreamExample3()
9899
new BsonDocument("x", 2)
99100
};
100101

101-
ChangeStreamDocument<BsonDocument> lastChangeStreamDocument;
102+
IChangeStreamCursor<ChangeStreamDocument<BsonDocument>> previousCursor;
102103
{
103104
new Thread(() =>
104105
{
@@ -107,19 +108,18 @@ public void ChangeStreamExample3()
107108
})
108109
.Start();
109110

110-
var enumerator = inventory.Watch().ToEnumerable().GetEnumerator();
111-
enumerator.MoveNext();
112-
lastChangeStreamDocument = enumerator.Current;
111+
previousCursor = inventory.Watch(new ChangeStreamOptions { BatchSize = 1 });
112+
while (previousCursor.MoveNext() && previousCursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch
113113
}
114114

115115
{
116116
// Start Changestream Example 3
117-
var resumeToken = lastChangeStreamDocument.ResumeToken;
117+
var resumeToken = previousCursor.GetResumeToken();
118118
var options = new ChangeStreamOptions { ResumeAfter = resumeToken };
119-
var enumerator = inventory.Watch(options).ToEnumerable().GetEnumerator();
120-
enumerator.MoveNext();
121-
var next = enumerator.Current;
122-
enumerator.Dispose();
119+
var cursor = inventory.Watch(options);
120+
cursor.MoveNext();
121+
var next = cursor.Current.First();
122+
cursor.Dispose();
123123
// End Changestream Example 3
124124

125125
next.FullDocument.Should().Be(documents[1]);
@@ -161,15 +161,10 @@ public void ChangestreamExample4()
161161
"{ $addFields : { newField : 'this is an added field!' } }");
162162

163163
var collection = database.GetCollection<BsonDocument>("inventory");
164-
using (var changeStream = collection.Watch(pipeline))
164+
using (var cursor = collection.Watch(pipeline))
165165
{
166-
using (var enumerator = changeStream.ToEnumerable().GetEnumerator())
167-
{
168-
if (enumerator.MoveNext())
169-
{
170-
var next = enumerator.Current;
171-
}
172-
}
166+
while (cursor.MoveNext() && cursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch
167+
var next = cursor.Current.First();
173168
}
174169
// End Changestream Example 4
175170
}

0 commit comments

Comments
 (0)