Using MongoDB Stitch to Trim Belly Fat

Recently MongoDB debuted Stitch, a serveless platform for their database in the cloud service, Atlas. Stitch offers some neat features such database triggers, which run functions in response to a database event, and services, which allow you to easily integrate with 3rd-party applications. Today we’re going to look at how easy it is to combine these two to create simple but powerful features.

What We're Building

I've set up a very basic HTML page to allow me to input my weight and store it in a MongoDB Atlas cluster. It's based closely on the Basic Blog Stitch tutorial, except instead of storing comments, we're storing my weight measurements. What we're going to add to this existing project is a Stitch trigger that will alert a Slack channel if I log a weight over a pre-defined threshold. With any luck, this kind of public shaming will help me think twice before I have that second slice of pie.


For the purposes of this blog post I'm going to assume you already have a working Stitch app for tracking your weight. Follow the Basic Blog official Stitch tutorial to set one up if you haven’t yet. The full code for my custom HTML page is available on my Github if you'd like to follow along.

Create a New Service

The first thing we're going to do is create a new HTTP service so we can send messages to Slack via its API. Open up your Stitch Admin Console and select the Services option under the "Control" section of the left-hand navigation.


Once on the Services page, hit the Add a Service button to set up a new service. Select HTTP as the type, and name it. I'm going to call mine "wt_http_service." We won't be adding a database webhook or custom rules in this project, so leave those blank.

Create a New Slack Webhook

Before we can create a new function, we need to set up a new incoming webhook in Slack that will allow us to post to Slack channels. Follow the official Slack tutorial for details on setting up a basic webhook. The key thing we need is the webhook URL, which will look something like this:

Create a new Trigger

It’s time to set up the new database trigger that will run every time we insert a new weight measurement into the collection. Click on the Triggers option under the "Atlas Clusters" heading of the left-hand navigation.

On the next page, click the button to Add Database Trigger. Here are the relevant fields to fill out:

  • Trigger Name: I've named mine "weightTrigger."
  • Enabled: Should be active by default, but if not, activate it now.
  • Database Name: This is the DB that contains your weight measurements. In my case it is "Weight."
  • Collection Name: This is the collection that contains your weight measurements. In my case it is "weights."
  • Operation Type: Select "Insert" only.

It's now time to create a new function that will be run by the trigger. In the Linked Function Section of the page select "+ New Function." Doing this will open up an in-browser editor where you can name and write your new function.

Create a New Function

I've gone with "checkWeight" for the name of my function. Here's the function code:

exports = function(changeEvent) {
   const http ="wt_http_service");
   const newWeight = changeEvent.fullDocument.weight;
   const thresholdWeight = 145;
   const slackURL = '';
   const slackMsg = `Uh-oh a weight was posted of ${newWeight}, better lay off the pie`;
   if(newWeight < thresholdWeight){
     return "Weight under threshold";
   } else {
     return http
       .post({ url: slackURL, body: JSON.stringify({"text": slackMsg})})
       .then(resp => {
         return resp;
       .catch(err => console.log(err) );

Here's the sections of the code you will want to change:

  • http service name: In my case it was "wt_http_service."
  • slackURL: Use the webhook URL you received from Slack.
  • thresholdWeight: Set to whatever you feel is appropriate.

Once everything is filled out, click the Save Button.

Wrapping Up

That should be it. Now when we enter a weight above our threshold, everyone in our choosen Slack channel will be notified and I'll promise to have a salad for lunch.

Obviously, we're just scratching the surface with this little project. Hopefully you now see how easy it is to combine triggers and service integrations to create really impactful features. Check out the official tutorials to see what other neat things you can bring to life with MongoDB Stitch.