Skip to content

Commit 28bc4d7

Browse files
DOCSP-18158 updated change stream example (#256)
* updated change stream example
1 parent 8a9acae commit 28bc4d7

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

source/code-snippets/usage-examples/changeStream.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,41 @@ const uri = "<connection string uri>";
55

66
const client = new MongoClient(uri);
77

8+
const simulateAsyncPause = () =>
9+
new Promise(resolve => {
10+
setTimeout(() => resolve(), 1000);
11+
});
12+
813
let changeStream;
914
async function run() {
1015
try {
1116
await client.connect();
12-
const database = client.db("sample_mflix");
13-
const movies = database.collection("movies");
17+
const database = client.db("insertDB");
18+
const collection = database.collection("haikus");
1419

15-
// open a Change Stream on the "movies" collection
16-
changeStream = movies.watch();
20+
// open a Change Stream on the "haikus" collection
21+
changeStream = collection.watch();
1722

1823
// set up a listener when change events are emitted
1924
changeStream.on("change", next => {
2025
// process any change event
2126
console.log("received a change to the collection: \t", next);
2227
});
2328

24-
// use a timeout to ensure the listener is registered before the insertOne
25-
// operation is called.
26-
await new Promise(resolve => {
27-
setTimeout(async () => {
28-
await movies.insertOne({
29-
test: "sample movie document",
30-
});
31-
// wait to close `changeStream` after the listener receives the event
32-
setTimeout(async () => {
33-
resolve(
34-
await changeStream.close(() => console.log("closed the change stream"))
35-
);
36-
}, 1000);
37-
}, 1000);
29+
await simulateAsyncPause();
30+
31+
await collection.insertOne({
32+
title: "Record of a Shriveled Datum",
33+
content: "No bytes, no problem. Just insert a document, in MongoDB",
3834
});
35+
36+
await simulateAsyncPause();
37+
38+
await changeStream.close();
39+
40+
console.log("closed the change stream");
3941
} finally {
4042
await client.close();
4143
}
4244
}
43-
run().catch(console.dir);
45+
run().catch(console.dir);

source/usage-examples/changeStream.txt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ change stream emits **change event** documents when they occur.
1414
The ``watch()`` method optionally takes an **aggregation pipeline** which consists of an array of **aggregation stages**
1515
as the first parameter. The aggregation stages filter and transform the change events.
1616

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
1818
15, filtering all others out.
1919

2020
.. code-block:: javascript
@@ -82,23 +82,23 @@ methods presented above:
8282
Example
8383
-------
8484

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
8787
print change events that occur on the collection.
8888

8989
First, open the change stream on the collection and then define a callback
9090
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.
9292

9393
To generate the change event on the collection, let's use ``insertOne()``
9494
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.
9797

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.
102102

103103
The timers used in this example are only necessary for this demonstration
104104
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:
133133
:copyable: false
134134

135135
received a change to the collection: {
136-
_id: {
137-
_data: '825EC...'
138-
},
136+
_id: { _data: '825EC...' },
139137
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(...) }
144142
}
145143
closed the change stream
146144

0 commit comments

Comments
 (0)