0
0
MongoDBquery~30 mins

Atlas triggers overview in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Atlas Triggers Overview
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Assign the log message inside the function and create the trigger dictionary with the specified keys and values.