@@ -100,15 +100,31 @@ describe('examples(change-stream):', function() {
100
100
// Start Changestream Example 3
101
101
const collection = db . collection ( 'inventory' ) ;
102
102
const changeStream = collection . watch ( ) ;
103
- const change1 = await changeStream . next ( ) ;
104
103
105
- const resumeAfter = change1 . _id ;
106
- changeStream . close ( ) ;
104
+ let resumeToken , newChangeStream ;
105
+ changeStream . on ( 'change' , next => {
106
+ resumeToken = next . _id ;
107
+ changeStream . close ( ) ;
107
108
108
- const newChangeStream = collection . watch ( { resumeAfter } ) ;
109
- const change2 = await newChangeStream . next ( ) ;
109
+ newChangeStream = collection . watch ( { resumeAfter } ) ;
110
+ newChangeStream . on ( 'change' , next => {
111
+ // process next document
112
+ } ) ;
113
+ } ) ;
110
114
// End Changestream Example 3
111
115
116
+ // Start Changestream Example 3 Alternative
117
+ const changeStreamIterator = collection . watch ( ) ;
118
+ const change1 = await changeStreamIterator . next ( ) ;
119
+
120
+ const resumeAfter = change1 . _id ;
121
+ changeStreamIterator . close ( ) ;
122
+
123
+ const newChangeStreamIterator = collection . watch ( { resumeAfter } ) ;
124
+ const change2 = await newChangeStreamIterator . next ( ) ;
125
+ // End Changestream Example 3 Alternative
126
+
127
+ await newChangeStreamIterator . close ( ) ;
112
128
await newChangeStream . close ( ) ;
113
129
114
130
expect ( change1 ) . to . have . nested . property ( 'fullDocument.a' , 1 ) ;
@@ -128,8 +144,9 @@ describe('examples(change-stream):', function() {
128
144
{ $match : { 'fullDocument.username' : 'alice' } } ,
129
145
{ $addFields : { newField : 'this is an added field!' } }
130
146
] ;
147
+
131
148
const collection = db . collection ( 'inventory' ) ;
132
- const changeStream = collection . watch ( { fullDocument : 'updateLookup' } ) ;
149
+ const changeStream = collection . watch ( pipeline ) ;
133
150
changeStream . on ( 'change' , next => {
134
151
// process next document
135
152
} ) ;
0 commit comments