0
0
MongoDBquery~10 mins

Change stream events (insert, update, delete) in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to watch for insert events in a MongoDB collection.

MongoDB
const changeStream = collection.watch([{ $match: { operationType: [1] } }]);
Drag options to blanks, or click blank then click option'
A"insert"
B"delete"
C"update"
D"replace"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' instead of 'insert' will watch for update events, not inserts.
Forgetting quotes around the operationType value causes syntax errors.
2fill in blank
medium

Complete the code to listen for delete events from the change stream.

MongoDB
changeStream.on('change', (change) => { if (change.operationType === [1]) { console.log('Document deleted'); } });
Drag options to blanks, or click blank then click option'
A"insert"
B"update"
C"replace"
D"delete"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' or 'insert' will not detect delete events.
Missing quotes around the string causes errors.
3fill in blank
hard

Fix the error in the code to correctly filter for update events in the change stream pipeline.

MongoDB
const pipeline = [{ $match: { operationType: [1] } }];
Drag options to blanks, or click blank then click option'
A"update"
Bupdate
Cupdates
D"updates"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted update causes syntax errors.
Using plural 'updates' is incorrect; the correct value is 'update'.
4fill in blank
hard

Fill both blanks to create a change stream that listens for insert and delete events.

MongoDB
const pipeline = [{ $match: { operationType: { $in: [[1], [2]] } } }];
Drag options to blanks, or click blank then click option'
A"insert"
B"update"
C"delete"
D"replace"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings causes errors.
Including wrong operation types like 'update' or 'replace' changes the filter.
5fill in blank
hard

Fill all three blanks to log the document key, operation type, and full document on insert events.

MongoDB
changeStream.on('change', (change) => { if (change.operationType === [1]) { console.log('Key:', change.[2]); console.log('Full Document:', change.[3]); } });
Drag options to blanks, or click blank then click option'
A"insert"
BdocumentKey
CfullDocument
Dns
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' or 'delete' instead of 'insert' changes the event type.
Accessing 'ns' instead of 'documentKey' or 'fullDocument' gives namespace info, not document data.