@@ -14,7 +14,7 @@ change stream emits **change event** documents when they occur.
14
14
The ``watch()`` method optionally takes an **aggregation pipeline** which consists of an array of **aggregation stages**
15
15
as the first parameter. The aggregation stages filter and transform the change events.
16
16
17
- In the example below , the ``$match`` stage will match all change event documents with a ``runtime`` value of less than
17
+ In the following snippet , the ``$match`` stage matches all change event documents with a ``runtime`` value of less than
18
18
15, filtering all others out.
19
19
20
20
.. code-block:: javascript
@@ -82,23 +82,23 @@ methods presented above:
82
82
Example
83
83
-------
84
84
85
- The following example opens a change stream on the ``movies `` collection in
86
- the ``sample_mflix `` database. Let's create a listener function to receive and
85
+ The following example opens a change stream on the ``haikus `` collection in
86
+ the ``insertDB `` database. Let's create a listener function to receive and
87
87
print change events that occur on the collection.
88
88
89
89
First, open the change stream on the collection and then define a callback
90
90
on the change stream using the ``on()`` method. Once set, generate a change
91
- event to be emitted by performing a change to the collection.
91
+ event by performing a change to the collection.
92
92
93
93
To generate the change event on the collection, let's use ``insertOne()``
94
94
method to add a new document. Since the ``insertOne()`` may run before the
95
- listener function can register, we use a timer, declared with ``setTimeout()``
96
- to wait one second before executing the insert.
95
+ listener function can register, we use a timer, defined as
96
+ ``simulateAsyncPause`` to wait 1 second before executing the insert.
97
97
98
- We also use a second timer to wait an additional second after the insertion of
99
- the document to provide ample time for the change event to be received and
100
- the for the callback to complete its execution before closing the
101
- ``ChangeStream`` instance using the ``close()`` method.
98
+ We also use ``simulateAsyncPause`` after the insertion of the document
99
+ to provide ample time for the listener function to receive the change
100
+ event and for the callback to complete its execution before
101
+ closing the ``ChangeStream`` instance using the ``close()`` method.
102
102
103
103
The timers used in this example are only necessary for this demonstration
104
104
to make sure there is enough time to register listener and have the
@@ -133,14 +133,12 @@ If you run the preceding example, you should see the following output:
133
133
:copyable: false
134
134
135
135
received a change to the collection: {
136
- _id: {
137
- _data: '825EC...'
138
- },
136
+ _id: { _data: '825EC...' },
139
137
operationType: 'insert',
140
- clusterTime: Timestamp { ... },
141
- fullDocument: { _id: 5ec3 ..., test : 'sample movie document' },
142
- ns: { db: 'sample_mflix ', coll: 'movies ' },
143
- documentKey: { _id: 5ec3 ... },
138
+ clusterTime: new Timestamp { ... },
139
+ fullDocument: { _id: new ObjectId( ...), title : 'Record of a Shriveled Datum', content: 'No bytes, no problem. Just insert a document, in MongoDB ' },
140
+ ns: { db: 'insertDB ', coll: 'haikus ' },
141
+ documentKey: { _id: new ObjectId( ...) }
144
142
}
145
143
closed the change stream
146
144
0 commit comments