0
0
MongoDBquery~20 mins

Atlas triggers overview in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Atlas Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of an Atlas trigger?

Atlas triggers in MongoDB are used to:

ACreate backups of the database on demand
BManually update documents in the database
CAutomatically run functions in response to database events or scheduled times
DManage user authentication and roles
Attempts:
2 left
💡 Hint

Think about what happens automatically when data changes or at specific times.

query_result
intermediate
2:00remaining
What output does this trigger function produce when a document is inserted?

Consider an Atlas trigger function that logs the inserted document's _id field:

exports = function(changeEvent) {
  const docId = changeEvent.fullDocument._id;
  return `Inserted document ID: ${docId}`;
};

What will be the output if a document with _id value 12345 is inserted?

AInserted document ID: null
BInserted document ID: undefined
CError: fullDocument is not defined
DInserted document ID: 12345
Attempts:
2 left
💡 Hint

The fullDocument field contains the inserted document.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a scheduled trigger to run every hour?

Choose the correct cron expression to schedule an Atlas trigger to run at the start of every hour.

A0 0 0 * *
B0 0 * * * *
C0 * * * *
D0 0 0 * * *
Attempts:
2 left
💡 Hint

Atlas uses 6-field cron expressions: seconds, minutes, hours, day of month, month, day of week.

optimization
advanced
2:00remaining
How to optimize a trigger function that reads and writes to the same collection?

You have an Atlas trigger that listens to inserts and updates the same document in the collection. What is the best way to avoid infinite loops caused by the trigger firing itself?

AUse a field flag in the document to check if the update is from the trigger and skip processing if true
BDisable the trigger before updating and enable it after
CUse a different collection for updates
DIncrease the trigger's timeout setting
Attempts:
2 left
💡 Hint

Think about how to detect if the trigger caused the update to avoid re-triggering.

🔧 Debug
expert
2:00remaining
What error occurs with this trigger function code?

Analyze this Atlas trigger function:

exports = function(changeEvent) {
  const doc = changeEvent.fullDocument;
  if (doc.status === 'active') {
    return 'Active document';
  } else {
    return 'Inactive document';
  }
};

What error or issue will this code cause when the trigger runs?

AAlways returns 'Active document' due to assignment instead of comparison
BSyntaxError because of missing semicolon
CTypeError because fullDocument is undefined
DReferenceError because 'doc' is not declared
Attempts:
2 left
💡 Hint

Check the condition inside the if statement carefully.