Triggering an Email From Database Updates with MongoDB Stitch

Eliot Horowitz
2 min readAug 30, 2018

Managing people budgets, salaries, hiring plans, raises, and equity grants for a large growing engineering team can be challenging. State of the art technology for doing so: spreadsheets. So for MongoDB, I decided to build a Stitch app to pull all the data together. That way I can manage the hiring plan, do projections, etc… It’s been quite useful, and it’s fun dogfooding Stitch.

The last feature request I got was to send an email when a new position opened up. Since Stitch Triggers just launched, this was really easy.

I started by creating a trigger in my Stitch app. (If you don’t have a Stitch app, you can create one in 10 seconds, and you don’t have to worry about going down a rathole, you can use Triggers independently of any other feature.)

Then I added an AWS Service (which just required putting in IAM credentials).

I set it up to trigger only for inserts on this collection. The code for the trigger is just this, and we’re done.

exports =  function(changeEvent) {
var hire = changeEvent.fullDocument;
var ses = context.services.get("ses1-eliothorowitz");

var body = "A new hire has been added\n";
body += "Notes: " + hire.notes + "\n";
body += "Job Code: " + hire.job_code + "\n";
body += "Manager: " + hire.managerName + "\n";
body += "Salary: $" + hire.salary.toLocaleString() + "\n";
body += "Start Quarter: " + hire.startQuarter + "\n";

var to = [];
to.push("eliot@mongodb.com");
to.push("stacy@mongodb.com");
var email = {};
email.Source = "hris@auto.erh.io";
email.Destination = { ToAddresses : to };
email.Message = { Subject : { Data : "new hire in hris" },
Body : { Text : { Data : body } } };
ses.SendEmail(email).catch(res => { console.log(
"error sending email to: " + to + " " +
res + " " + EJSON.stringify(res)) } );
};

--

--