Complete the code to start watching changes on the collection.
const changeStream = collection.[1]();The watch() method starts a change stream to listen for real-time updates on a collection.
Complete the code to handle change events from the change stream.
changeStream.on('[1]', next => { console.log(next); });
The 'change' event is emitted when a change occurs in the watched collection.
Fix the error in the code to properly close the change stream when done.
await changeStream.[1]();The close() method properly closes the change stream to release resources.
Fill both blanks to filter change events to only insert operations.
const pipeline = [
{ $match: { 'operationType': [1] } }
];
const changeStream = collection.watch(pipeline, { [2]: false });The pipeline filters for 'insert' operations only. The option fullDocument: false disables returning the full changed document.
Fill all three blanks to create a change stream that watches updates and prints the updated fields.
const pipeline = [
{ $match: { 'operationType': [1] } }
];
const changeStream = collection.watch(pipeline);
changeStream.on('[2]', change => {
console.log(change.updateDescription.[3]);
});The pipeline filters for 'update' operations. The event listened to is 'change'. The updatedFields property shows which fields were changed.