Complete the code to watch for insert events in a MongoDB collection.
const changeStream = collection.watch([{ $match: { operationType: [1] } }]);The operationType filter should be set to "insert" to watch for insert events.
Complete the code to listen for delete events from the change stream.
changeStream.on('change', (change) => { if (change.operationType === [1]) { console.log('Document deleted'); } });
To detect delete events, check if operationType equals "delete".
Fix the error in the code to correctly filter for update events in the change stream pipeline.
const pipeline = [{ $match: { operationType: [1] } }];The operationType value must be a string with quotes, so use "update".
Fill both blanks to create a change stream that listens for insert and delete events.
const pipeline = [{ $match: { operationType: { $in: [[1], [2]] } } }];The $in operator filters for multiple operation types. Use "insert" and "delete" to watch for those events.
Fill all three blanks to log the document key, operation type, and full document on insert events.
changeStream.on('change', (change) => { if (change.operationType === [1]) { console.log('Key:', change.[2]); console.log('Full Document:', change.[3]); } });
For insert events, operationType is "insert". The document key is in documentKey, and the full inserted document is in fullDocument.