Atlas triggers in MongoDB are used to:
Think about what happens automatically when data changes or at specific times.
Atlas triggers automatically execute functions when certain database events occur or on a schedule, enabling automation without manual intervention.
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?
The fullDocument field contains the inserted document.
The changeEvent.fullDocument holds the entire inserted document, so accessing _id returns the inserted document's ID.
Choose the correct cron expression to schedule an Atlas trigger to run at the start of every hour.
Atlas uses 6-field cron expressions: seconds, minutes, hours, day of month, month, day of week.
The correct 6-field cron expression for running at the start of every hour is 0 0 * * * * (0 seconds, 0 minutes, every hour).
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?
Think about how to detect if the trigger caused the update to avoid re-triggering.
Adding a flag field to mark updates done by the trigger lets the function skip processing those updates, preventing infinite loops.
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?
Check the condition inside the if statement carefully.
The code uses a single equals sign = which assigns 'active' to doc.status instead of comparing it. This makes the condition always true, so it always returns 'Active document'.