Skip to content

Commit c59ae31

Browse files
Feedback
1 parent d5a4ec8 commit c59ae31

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

source/fundamentals/crud/read-operations/change-streams.txt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Overview
2121
--------
2222

2323
In this guide, you can learn how to use a **change stream** to monitor real-time
24-
changes to your database. A change stream is a {+mdb-server+} feature that
24+
changes to your data. A change stream is a {+mdb-server+} feature that
2525
allows your application to subscribe to data changes on a collection, database,
2626
or deployment.
2727

@@ -65,7 +65,7 @@ classes:
6565
- ``Collection``: To monitor changes in the collection
6666

6767
The following example opens a change stream on the ``restaurants`` collection
68-
and outputs the operation type of changes as they occur. Select the
68+
and outputs the changes as they occur. Select the
6969
:guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the corresponding
7070
code.
7171

@@ -88,26 +88,24 @@ code.
8888
:language: csharp
8989

9090
To begin watching for changes, run the application. Then, in a separate
91-
application or shell, modify the ``restaurants`` collection. The following
92-
example updates a document with a ``name`` field value of ``Blarney Castle``:
91+
application or shell, modify the ``restaurants`` collection. Updating a document
92+
with the ``"name"`` value of ``"Blarney Castle"`` results in the following
93+
change stream output:
9394

94-
.. _csharp-change-stream-update:
95+
.. code-block:: sh
9596

96-
.. literalinclude:: /includes/code-examples/change-streams/change-streams.cs
97-
:start-after: start-modify-document
98-
:end-before: end-modify-document
99-
:language: csharp
100-
101-
When you update the collection, the change stream application prints the type of change
102-
as it occurs.
97+
{ "_id" : { "_data" : "..." }, "operationType" : "update", "clusterTime" : Timestamp(...),
98+
"wallTime" : ISODate("..."), "ns" : { "db" : "sample_restaurants", "coll" : "restaurants" },
99+
"documentKey" : { "_id" : ObjectId("...") }, "updateDescription" : { "updatedFields" : { "cuisine" : "Irish" },
100+
"removedFields" : [], "truncatedArrays" : [] } }
103101

104102
Modify the Change Stream Output
105103
-------------------------------
106104

107105
You can pass the ``pipeline`` parameter to the ``Watch()`` and ``WatchAsync()``
108106
methods to modify the change stream output. This parameter allows you to watch
109107
for only specified change events. Create the pipeline by using the
110-
``EmptyPipelineDefinition`` class, and appending the relevant aggregation stage methods.
108+
``EmptyPipelineDefinition`` class and appending the relevant aggregation stage methods.
111109

112110
You can specify the following aggregation stages in the ``pipeline`` parameter:
113111

@@ -121,7 +119,8 @@ You can specify the following aggregation stages in the ``pipeline`` parameter:
121119
- ``$unset``
122120

123121
To learn how to build an aggregation pipeline by using the
124-
``PipelineDefinitionBuilder`` class, see :ref:`csharp-builders-aggregation`.
122+
``PipelineDefinitionBuilder`` class, see :ref:`csharp-builders-aggregation` in
123+
the Operations with Builders guide.
125124

126125
The following example uses the ``pipeline`` parameter to open a change stream
127126
that records only update operations. Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to see the
@@ -164,7 +163,7 @@ of ``Watch()`` and ``WatchAsync()``:
164163
:widths: 30 70
165164
:header-rows: 1
166165

167-
* - Property
166+
* - Option
168167
- Description
169168

170169
* - ``FullDocument``
@@ -200,7 +199,7 @@ of ``Watch()`` and ``WatchAsync()``:
200199
| ``StartAtOperationTime`` is mutually exclusive with ``ResumeAfter`` and ``StartAfter``.
201200

202201
* - ``MaxAwaitTime``
203-
- | The maximum amount of time, in milliseconds, the server waits for new
202+
- | Specifies the maximum amount of time, in milliseconds, the server waits for new
204203
data changes to report to the change stream cursor before returning an
205204
empty batch. Defaults to 1000 milliseconds.
206205

@@ -211,14 +210,14 @@ of ``Watch()`` and ``WatchAsync()``:
211210
cursor and set this parameter to ``True``.
212211

213212
* - ``BatchSize``
214-
- | The maximum number of change events to return in each batch of the
213+
- | Specifies the maximum number of change events to return in each batch of the
215214
response from the MongoDB cluster.
216215

217216
* - ``Collation``
218-
- | The collation to use for the change stream cursor.
217+
- | Specifies the collation to use for the change stream cursor.
219218

220219
* - ``Comment``
221-
- | A comment to attach to the operation.
220+
- | Specifies a comment to attach to the operation.
222221

223222
.. _csharp-change-stream-pre-post-image:
224223

@@ -284,6 +283,15 @@ code.
284283
:end-before: end-change-stream-post-image
285284
:language: csharp
286285

286+
Running the preceding code example and updating a document with the ``"name"``
287+
value of ``"Blarney Castle"`` results in the following change stream output:
288+
289+
.. code-block:: sh
290+
291+
{ "_id" : ObjectId("..."), "name" : "Blarney Castle", "restaurant_id" : "40366356",
292+
"cuisine" : "Traditional Irish", "address" : { "building" : "202-24", "coord" : [-73.925044200000002, 40.5595462],
293+
"street" : "Rockaway Point Boulevard", "zipcode" : "11697" }, "borough" : "Queens", "grades" : [...] }
294+
287295
To learn more about pre-images and post-images, see
288296
:manual:`Change Streams with Document Pre- and Post-Images </changeStreams#change-streams-with-document-pre--and-post-images>`
289297
in the {+mdb-server+} manual.

source/includes/code-examples/change-streams/change-streams.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
var database = client.GetDatabase("sample_restaurants");
33
var collection = database.GetCollection<Restaurant>("restaurants");
44

5-
// Open a change stream and print the changes as they're received
5+
// Opens a change stream and prints the changes as they're received
66
using (var cursor = collection.Watch())
77
{
88
foreach (var change in cursor.ToEnumerable())
99
{
10-
Console.WriteLine("Received the following type of change: " + change.OperationType);
10+
Console.WriteLine("Received the following type of change: " + change.BackingDocument);
1111
}
1212
}
1313
// end-open-change-stream
@@ -16,11 +16,11 @@
1616
var database = client.GetDatabase("sample_restaurants");
1717
var collection = database.GetCollection<Restaurant>("restaurants");
1818

19-
// Open a change stream and print the changes as they're received
19+
// Opens a change streams and print the changes as they're received
2020
using var cursor = await collection.WatchAsync();
2121
await cursor.ForEachAsync(change =>
2222
{
23-
Console.WriteLine("Received the following type of change: " + change.OperationType);
23+
Console.WriteLine("Received the following type of change: " + change.BackingDocument);
2424
});
2525
// end-open-change-stream-async
2626

@@ -41,7 +41,7 @@ await cursor.ForEachAsync(change =>
4141
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<Restaurant>>()
4242
.Match(change => change.OperationType == ChangeStreamOperationType.Update);
4343

44-
// Open a change stream and print the changes as they're received
44+
// Opens a change stream and prints the changes as they're received
4545
using (var cursor = await _restaurantsCollection.WatchAsync(pipeline))
4646
{
4747
await cursor.ForEachAsync(change =>
@@ -55,7 +55,7 @@ await cursor.ForEachAsync(change =>
5555
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<Restaurant>>()
5656
.Match(change => change.OperationType == ChangeStreamOperationType.Update);
5757

58-
// Open a change stream and print the changes as they're received
58+
// Opens a change streams and print the changes as they're received
5959
using (var cursor = _restaurantsCollection.Watch(pipeline))
6060
{
6161
foreach (var change in cursor.ToEnumerable())
@@ -78,7 +78,7 @@ await cursor.ForEachAsync(change =>
7878
{
7979
foreach (var change in cursor.ToEnumerable())
8080
{
81-
Console.WriteLine(change);
81+
Console.WriteLine(changeFullDocument.ToBsonDocument());
8282
}
8383
}
8484
// end-change-stream-post-image
@@ -95,6 +95,6 @@ await cursor.ForEachAsync(change =>
9595
using var cursor = await _restaurantsCollection.WatchAsync(pipeline, options);
9696
await cursor.ForEachAsync(change =>
9797
{
98-
Console.WriteLine(change);
98+
Console.WriteLine(changeFullDocument.ToBsonDocument());
9999
});
100100
// end-change-stream-post-image-async

0 commit comments

Comments
 (0)