0
0
MongoDBquery~10 mins

Atlas triggers overview 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 create a trigger that runs on document insert.

MongoDB
exports = function(changeEvent) { if (changeEvent.operationType === '[1]') { console.log("Document inserted"); } };
Drag options to blanks, or click blank then click option'
Areplace
Bupdate
Cdelete
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' or 'delete' instead of 'insert'.
2fill in blank
medium

Complete the code to access the full document after an update in a trigger.

MongoDB
exports = function(changeEvent) { const fullDoc = changeEvent.[1]; console.log(fullDoc); };
Drag options to blanks, or click blank then click option'
AdocumentKey
BfullDocument
CupdateDescription
Dns
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'documentKey' which only has the document ID.
3fill in blank
hard

Fix the error in the trigger code to correctly check for a delete operation.

MongoDB
exports = function(changeEvent) { if (changeEvent.operationType === '[1]') { console.log("Document deleted"); } };
Drag options to blanks, or click blank then click option'
Adrop
Bremove
Cdelete
Ddestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' or 'drop' which are not valid operationType values.
4fill in blank
hard

Fill both blanks to filter trigger events only for updates on the 'status' field.

MongoDB
exports = function(changeEvent) { if (changeEvent.operationType === '[1]' && changeEvent.updateDescription.updatedFields.[2] !== undefined) { console.log("Status field updated"); } };
Drag options to blanks, or click blank then click option'
Aupdate
Binsert
Cstatus
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' instead of 'update' or wrong field names.
5fill in blank
hard

Fill all three blanks to log the document ID and new value of 'score' after an update.

MongoDB
exports = function(changeEvent) { const docId = changeEvent.[1]._id; const newScore = changeEvent.updateDescription.updatedFields.[2]; console.log(`Document ID: ${docId}, New Score: ${newScore}`); if (changeEvent.operationType === '[3]') { /* additional logic */ } };
Drag options to blanks, or click blank then click option'
AdocumentKey
Bscore
Cupdate
DfullDocument
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fullDocument' for ID or wrong operationType.