EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Triggers

MongoDB Triggers

Updated March 6, 2023

MongoDB Triggers

Definition of MongoDB Triggers

Mongodb trigger allows us to execute the logic as per schedule; MongoDB provides the three types of triggers, i.e., database triggers, authentication triggers, and schedules triggers. Database triggers are used to automatically respond to any document updation, insertion, and deletion; authentication triggers are used to execute when new user creation, deletion, and authentication, and Scheduled triggers will execute at the time of pre-defined schedule. MongoDB database triggers are used to listen to the changes from the documents collection, and also it will pass the events of database triggers to the function of the trigger.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Below is the parameter which was used at the time of trigger creation.

{
“Trigger_type”: Name of type of trigger,
“Trigger_name”: Name of trigger,
“Name_of_function”: function name,
“Config”: {
“Name_of_service”: service name,
“database_name”: name of database,
“collection_name”: name of collection,
“database_operations_types”: types of database operations,
“full_document” : <Type is Boolean>,
“Unordered” : <optional>,
“Match expression” : <optional>,
}

Parameter description of MongoDB triggers.

1) Trigger type – This is defined as a type of trigger which was we have used at the time of trigger creation.
2) Trigger name – This parameter is mandatory while creating triggers. It is defined as the name of the trigger.
3) Function name – This is defined as the name of the stitch function in MongoDB. This is a required parameter while creating a new trigger.
4) Database name – This is the database name from which we have selected the collection of creating triggers.
5) Collection name – This is the name of the collection which was used to create a new trigger in MongoDB. The collection name is a mandatory parameter while creating triggers.
6) Service name – This parameter is defined as the name of service which was we have used at the time of creating the trigger in MongoDB.
7) Operation types – This is defined as the name of operations which was we have used at the time of creating a new trigger. Name of database operations like insert, update, and delete.
8) Full document – This is also a required parameter at the time of creating a trigger in MongoDB.
9) Unordered – This is an optional parameter in the MongoDB trigger. The default value of this parameter is false. To enable the value of this parameter, we need to set it true.
10) Match expression – This is also an optional parameter while using a trigger in MongoDB. Match expression is used to configure the trigger to execute when a specified event occurs.

How do triggers work in Mongodb?

MongoDB database triggers is allows us to execute the logic of server-side when any documents will add, deleted, or updated from the database collection.

The main use of MongoDB database triggers is to implement complex data interaction and to update the information.

MongoDB database triggers basically used the change steams to listen to changes from database collection.

Basically, there are three types of triggers available in MongoDB. We can create triggers by using the MongoDB Atlas cluster.

MongoDB Atlas is used to replace command instead of update command while executing update using data explorer.
Scheduled triggers in MongoDB will allow us to execute server-side logic on a regular schedule of intervals or by using the cron expressions.

We can use a schedule trigger to run the report, update the documents at certain intervals of time, or sending an automated email.

While creating a new trigger using MongoDB atlas, we can see a function editor where we can write javascript code that was executed by trigger.

We can restart or resume our trigger after creating on MongoDB atlas cluster.

MongoDB triggers are only available on the MongoDB Atlas cluster, which was running on version 3.6 or later.

MongoDB triggers are used to execute the database and application logic automatically. It is executing automatically either by creating an event or a pre-defined schedule.

MongoDB trigger uses the following operation type at the time of creating a new trigger in MongoDB.

1) Insert
2) Update
3) Delete
4) Replace

  • Insert operation type is defined as adding new documents in the collection when an event occurs.
  • The update operation is defined as update or change existing documents from the collection.
  • Delete operation is defined as delete existing documents from the collection when a specified event occurs.
  • Replace operation is defined as replacing documents from the collection.

Example

The below example shows that create triggers in MongoDB are as follows. In the below example, we have to create the database trigger name as MongoDB_Trigger.

1) Create a trigger in MongoDB

First, we have to define the trigger type, trigger name, enabled event order, and link data source.

Trigger type – Database
Trigger name - MongoDB_Trigger
Enables – ON
Event ordering – ON
Link data source – Cluster

MangoDB triggers 1

Figure – defining the trigger type, trigger name, enabled event order, and link data source.

Second, we have to define the cluster name, operation type, full document, and document preimage.

Cluster name – Cluster0
Database name – sample_training
Collection name – grades
Operation type – Insert
Full document – ON
Document preimage – ON

MangoDB triggers 2

Figure – defining the cluster name, operation type, full document, and document preimage.

In the below image, we have to define the event type and function name.

Code:

Function name – myFunc
Function –
exports = function (changeEvent) {
db.grades.insert ({name: “MongoDB", grades: "A"})
}

MangoDB triggers 3

Figure – defining the event type and function name.

In the below example, we can see that MongoDB_Trigger is created in the MongoDB Atlas cluster.

MangoDB triggers 4

Figure – Trigger is created in MongoDB Atlas cluster.

2) Edit trigger in MongoDB

If suppose we want to edit the already created trigger, we can edit using the following steps. We have to edit the trigger name as MongoDB_Trigger.

First, select the trigger which was we want to edit, then click on the edit trigger button.

example 2

Figure – Edit the trigger which was already created in MongoDB.

We have to edit the database name from sample_training to sample_airbnb and collection name from grades to listings and reviews.

example 2-1

Figure – Edit the database and collection name from MongoDB trigger.

Save all the changes by clicking the save button.

save all

Figure – Save all edited changes of triggers.

3) Delete trigger in MongoDB

We can delete the unused trigger in MongoDB. We have to delete the MongoDB_Trigger. First, select the trigger which was we want to delete, then click on the delete trigger button.

delete

Figure – Delete the trigger in MongoDB.

Click on confirm button, then click on the remove database trigger button.

remove

Figure – Delete trigger in MongoDB.

Conclusion

MongoDB trigger is used to execute the logic of server-side when the collection document is added, deleted, or removed from the database collection. Database triggers, authentication triggers, and scheduled triggers are the types of triggers in MongoDB. Triggers are very useful and important in MongoDB.

Recommended Articles

This is a guide to MongoDB Triggers. Here we discuss the definition, syntax, How triggers work in Mongodb? Examples and code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB group by
  4. MongoDB Database
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1
 20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more