16
16
17
17
package documentation ;
18
18
19
+ import com .mongodb .client .MongoChangeStreamCursor ;
19
20
import com .mongodb .client .MongoClient ;
20
21
import com .mongodb .client .MongoClients ;
21
22
import com .mongodb .client .MongoCollection ;
@@ -69,7 +70,7 @@ public static void main(final String[] args) {
69
70
System .out .println ("1. Initial document from the Change Stream:" );
70
71
71
72
// Create the change stream cursor.
72
- MongoCursor <ChangeStreamDocument <Document >> cursor = collection .watch ().iterator ();
73
+ MongoChangeStreamCursor <ChangeStreamDocument <Document >> cursor = collection .watch ().cursor ();
73
74
74
75
// Insert a test document into the collection.
75
76
collection .insertOne (Document .parse ("{username: 'alice123', name: 'Alice'}" ));
@@ -86,7 +87,7 @@ public static void main(final String[] args) {
86
87
System .out .println ("2. Document from the Change Stream, with lookup enabled:" );
87
88
88
89
// Create the change stream cursor.
89
- cursor = collection .watch ().fullDocument (FullDocument .UPDATE_LOOKUP ).iterator ();
90
+ cursor = collection .watch ().fullDocument (FullDocument .UPDATE_LOOKUP ).cursor ();
90
91
91
92
// Update the test document.
92
93
collection .
updateOne (
Document .
parse (
"{username: 'alice123'}" ),
Document .
parse (
"{$set : { email: '[email protected] '}}" ));
@@ -117,7 +118,7 @@ public static void main(final String[] args) {
117
118
);
118
119
119
120
// Create the change stream cursor with $match.
120
- cursor = collection .watch (pipeline ).fullDocument (FullDocument .UPDATE_LOOKUP ).iterator ();
121
+ cursor = collection .watch (pipeline ).fullDocument (FullDocument .UPDATE_LOOKUP ).cursor ();
121
122
122
123
// Forward to the end of the change stream
123
124
next = cursor .tryNext ();
@@ -146,11 +147,11 @@ public static void main(final String[] args) {
146
147
System .out .println ("4. Document from the Change Stream including a resume token:" );
147
148
148
149
// Get the resume token from the last document we saw in the previous change stream cursor.
149
- BsonDocument resumeToken = next .getResumeToken ();
150
+ BsonDocument resumeToken = cursor .getResumeToken ();
150
151
System .out .println (resumeToken );
151
152
152
153
// Pass the resume token to the resume after function to continue the change stream cursor.
153
- cursor = collection .watch ().resumeAfter (resumeToken ).iterator ();
154
+ cursor = collection .watch ().resumeAfter (resumeToken ).cursor ();
154
155
155
156
// Insert a test document.
156
157
collection .insertOne (Document .parse ("{test: 'd'}" ));
0 commit comments