📖 Scenario: You are managing a MongoDB Atlas database for an online bookstore. You want to automate some tasks when data changes happen in your collections.
🎯 Goal: Build a simple Atlas trigger setup that reacts to changes in the books collection and logs the changed document's title.
📋 What You'll Learn
Create a books collection with sample documents
Define a trigger configuration variable for the collection name
Write a trigger function that logs the title of the changed book document
Complete the trigger setup with event type and full function code
💡 Why This Matters
🌍 Real World
Atlas triggers automate actions in MongoDB Atlas when data changes, such as sending notifications or updating related data.
💼 Career
Understanding Atlas triggers is useful for database administrators and backend developers who want to automate workflows and maintain data integrity.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books that is a list of dictionaries. Add these exact entries: {'_id': 1, 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald'} and {'_id': 2, 'title': '1984', 'author': 'George Orwell'}.
MongoDB
Hint
Use a list with two dictionaries exactly as shown.
2
Define a trigger configuration variable
Create a variable called trigger_config and set it to a dictionary with the key collection and value 'books'.
MongoDB
Hint
Use a dictionary with key 'collection' and value 'books'.
3
Write the trigger function to log changed book titles
Define a function called log_changed_title that takes one parameter change_event. Inside, assign the variable title to change_event['fullDocument']['title'].
MongoDB
Hint
Define a function with the exact name and parameter, then assign the title from the change event.
4
Complete the trigger setup with event type and function body
Add a line inside log_changed_title that logs the title by assigning log_message = f"Changed book title: {title}". Then create a variable trigger as a dictionary with keys event set to 'change', collection set to trigger_config['collection'], and function set to log_changed_title.
MongoDB
Hint
Assign the log message inside the function and create the trigger dictionary with the specified keys and values.
Practice
(1/5)
1. What is the main purpose of an Atlas trigger in MongoDB?
easy
A. To create new collections in the database
B. To manually update documents in a collection
C. To backup the database automatically
D. To automatically run code when certain database events happen
Solution
Step 1: Understand what triggers do
Atlas triggers run code automatically when specific events occur in the database, like inserts or updates.
Step 2: Compare options with this purpose
Only To automatically run code when certain database events happen describes automatic code running on database events, which matches the trigger's purpose.
Final Answer:
To automatically run code when certain database events happen -> Option D
Quick Check:
Atlas triggers = automatic code on events [OK]
Hint: Triggers run code automatically on database events [OK]
Common Mistakes:
Thinking triggers are for manual updates
Confusing triggers with backups
Assuming triggers create collections
2. Which of the following is the correct way to define a function for an Atlas trigger?
easy
A. function triggerFunction() { console.log('Triggered'); }
B. def triggerFunction(): print('Triggered')
C. triggerFunction() => { console.log('Triggered'); }
D. func triggerFunction() { print('Triggered') }
Solution
Step 1: Identify the language used in Atlas triggers
Atlas triggers use JavaScript functions to define actions.
Step 2: Check which option is valid JavaScript function syntax
function triggerFunction() { console.log('Triggered'); } uses standard JavaScript function syntax. Options A and D use Python and Go syntax, and C is invalid arrow function syntax.
Final Answer:
function triggerFunction() { console.log('Triggered'); } -> Option A
Quick Check:
Atlas triggers use JavaScript functions [OK]
Hint: Atlas triggers use JavaScript function syntax [OK]
Common Mistakes:
Using Python or other language syntax
Incorrect arrow function syntax
Missing function keyword in JavaScript
3. Given this Atlas trigger function, what will be logged when a new document is inserted?
exports = function(changeEvent) {
console.log('Document inserted with _id:', changeEvent.fullDocument._id);
};
medium
A. Document inserted with _id: [actual document _id]
B. No output because console.log is not supported
C. Error: changeEvent is not defined
D. Document inserted with _id: undefined
Solution
Step 1: Understand the changeEvent object in insert triggers
When a document is inserted, changeEvent.fullDocument contains the new document, including its _id.
Step 2: Analyze the console.log statement
The function logs the string plus the _id from the inserted document, so it outputs the actual _id value.
Final Answer:
Document inserted with _id: [actual document _id] -> Option A
Quick Check:
changeEvent.fullDocument._id logs inserted document ID [OK]
Hint: Insert triggers have fullDocument with new data [OK]
Common Mistakes:
Assuming _id is undefined
Thinking changeEvent is undefined
Believing console.log doesn't work in triggers
4. You wrote this Atlas trigger function but it does not run as expected: