Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5d5869

Browse files
Merge pull request #12 from mongodb-developer/update-change-streams
Update change streams
2 parents 2402c47 + b191b70 commit f5d5869

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

‎changeStreams.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function closeChangeStream(timeInMs = 60000, changeStream) {
7878
async function monitorListingsUsingEventEmitter(client, timeInMs = 60000, pipeline = []) {
7979
const collection = client.db("sample_airbnb").collection("listingsAndReviews");
8080

81-
// See http://bit.ly/Node_watch for the watch() docs
81+
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#watch for the watch() docs
8282
const changeStream = collection.watch(pipeline);
8383

8484
// ChangeStream inherits from the Node Built-in Class EventEmitter (https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_class_eventemitter).
@@ -102,18 +102,25 @@ async function monitorListingsUsingEventEmitter(client, timeInMs = 60000, pipeli
102102
async function monitorListingsUsingHasNext(client, timeInMs = 60000, pipeline = []) {
103103
const collection = client.db("sample_airbnb").collection("listingsAndReviews");
104104

105-
// See http://bit.ly/Node_watch for the watch() docs
105+
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#watch for the watch() docs
106106
const changeStream = collection.watch(pipeline);
107107

108108
// Set a timer that will close the change stream after the given amount of time
109109
// Function execution will continue because we are not using "await" here
110110
closeChangeStream(timeInMs, changeStream);
111111

112112
// We can use ChangeStream's hasNext() function to wait for a new change in the change stream.
113-
// If the change stream is closed, hasNext() will return false so the while loop will exit.
114-
// See http://bit.ly/Node_ChangeStream for the ChangeStream docs.
115-
while (await changeStream.hasNext()) {
116-
console.log(await changeStream.next());
113+
// See https://mongodb.github.io/node-mongodb-native/3.6/api/ChangeStream.html for the ChangeStream docs.
114+
try {
115+
while (await changeStream.hasNext()) {
116+
console.log(await changeStream.next());
117+
}
118+
} catch (error) {
119+
if (changeStream.isClosed()) {
120+
console.log("The change stream is closed. Will not wait on any more changes.")
121+
} else {
122+
throw error;
123+
}
117124
}
118125
}
119126

@@ -127,11 +134,11 @@ async function monitorListingsUsingHasNext(client, timeInMs = 60000, pipeline =
127134
async function monitorListingsUsingStreamAPI(client, timeInMs = 60000, pipeline = []) {
128135
const collection = client.db('sample_airbnb').collection('listingsAndReviews');
129136

130-
// See http://bit.ly/Node_watch for the watch() docs
137+
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#watch for the watch() docs
131138
const changeStream = collection.watch(pipeline);
132139

133-
// See http://bit.ly/Node_pipe for the pipe() docs
134-
changeStream.pipe(
140+
// See https://mongodb.github.io/node-mongodb-native/3.6/api/ChangeStream.html#pipe for the pipe() docs
141+
changeStream.stream().pipe(
135142
new stream.Writable({
136143
objectMode: true,
137144
write: function (doc, _, cb) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /