INNER CODE UNIT · JavaScript
monitorListingsUsingStreamAPI
mongodb-developer/nodejs-quickstart · changeStreams.js:137
async function monitorListingsUsingStreamAPI(client, timeInMs = 60000, pipeline = []) {
const collection = client.db('sample_airbnb').collection('listingsAndReviews');
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#watch for the watch() docs
const changeStream = collection.watch(pipeline);
// See https://mongodb.github.io/node-mongodb-native/3.6/api/ChangeStream.html#stream for the stream() docs
changeStream.stream().pipe(
new stream.Writable({
objectMode: true,
write: function (doc, _, cb) {
console.log(doc);
cb();
}
})
);
// Wait the given amount of time and then close the change stream