File tree Expand file tree Collapse file tree 2 files changed +12
-6
lines changed
fundamentals/crud/read-operations
includes/fundamentals/code-snippets/change-streams Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -63,8 +63,14 @@ changes in the connected MongoDB deployment.
63
63
Example
64
64
~~~~~~~
65
65
66
- The following code example shows how to open a change stream and print
67
- change stream events whenever the data in the collection changes:
66
+ This example shows how to open a change stream on the
67
+ ``myColl`` collection and print change stream events as they occur.
68
+
69
+ The driver stores change stream events in a variable of type
70
+ ``ChangeStreamIterable``. In the following example, we specify that the
71
+ driver should populate the ``ChangeStreamIterable`` object with ``Document``
72
+ types. As a result, the driver stores individual change stream events as
73
+ ``ChangeStreamDocument`` objects.
68
74
69
75
.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
70
76
:language: java
@@ -81,7 +87,7 @@ following text:
81
87
Received a change: ChangeStreamDocument{
82
88
operationType='insert',
83
89
resumeToken={"_data": "825EC..."},
84
- namespace=myDb.myChangeStreamCollection ,
90
+ namespace=myDb.myColl ,
85
91
...
86
92
}
87
93
Original file line number Diff line number Diff line change @@ -42,11 +42,11 @@ public static void main(String [] args){
42
42
// ChangeStreams.allFullDocumentOptions(collection);
43
43
// ChangeStreams.aggregationExample(collection);
44
44
}
45
- private static void openExample (MongoCollection < Document > collection ) {
45
+ private static void openExample (MongoDatabase database ) {
46
46
// begin openChangeStreamExample
47
- // The collection variable references a MongoCollection instance
48
- ChangeStreamIterable <Document > changeStream = collection .watch ();
47
+ MongoCollection <Document > collection = database .getCollection ("myColl" );
49
48
49
+ ChangeStreamIterable <Document > changeStream = collection .watch ();
50
50
changeStream .forEach (event ->
51
51
System .out .println ("Received a change: " + event ));
52
52
// end openChangeStreamExample
You can’t perform that action at this time.
0 commit comments