MongoDB Trigger
The MongoDB Trigger parses the incoming webhook payload from the MongoDB webhook triggers. In other words, the BuildShip Workflow runs every time there are any changes in the linked MongoDB Collection, Database, or Deployment. Alternatively, the workflow can also be made to run based on a given schedule (or a CRON expression), however, this documentation is for setting up a Database Trigger.
Prerequisites ✅
- A MongoDB Project. If you don't have one set up, click here (opens in a new tab) to set one up.
Trigger Configuration
-
Navigate to the Connect section. Select (or search for) the MongoDB Trigger and add it to the flow.
-
This trigger doesn't require any configuration. Click on the "Connect" button to generate the Webhook URL.
-
Copy the generated Webhook URL and check out the next section to set up the MongoDB Trigger.
Setting up the MongoDB Trigger
-
Navigate to your MongoDB Dashboard and navigate to the "Triggers" section.
-
Pick your desired type of trigger from Database - which will listen to changes to your database, or Scheduled - which will trigger the event at the chosen schedule. For this example, we will be setting up a Database Trigger.
-
Follow the instructions and set up your source details.
-
Under the Function section, select "Function" as the Event Type.
-
Inside the Function script editor, paste the following script. Copy the Webhook URL from the MongoDB Trigger in your BuildShip workflow, and paste it on
line 5
:
import axios from 'axios';
import crypto from 'crypto';
exports = async function (changeEvent) {
const webhookUrl = ''; // Add your webhook URL here
const triggerId = webhookUrl.split('/').pop();
try {
const signature = generateSignature(changeEvent, triggerId);
const res = await axios.post(webhookUrl, changeEvent, {
headers: {
'X-Webhook-Signature': signature,
},
});
console.log(JSON.stringify(changeEvent));
return true;
} catch (error) {
console.error('Error sending webhook:', error);
return false;
}
};
function generateSignature(data, secret) {
const hmac = crypto.createHmac('sha256', secret);
hmac.update(JSON.stringify(data));
return hmac.digest('hex');
}
-
Click on the "Add Dependency" button and add
axios
, then hit "Add". This will install the dependency for the function execution. Similarly, add a dependency for thecrypto
package. -
Once this is done, click on the "▶︎ Run" button at the bottom. If set up correctly and your BuildShip workflow is shipped, this should return
true
as the output on the console.
The code snippet uses the axios
library to send the POST request to the webhook URL, and the crypto
library to
generate a signature for the webhook payload. The signature is included in the request headers to ensure the
authenticity of the request.
Troubleshooting
If you encounter any issues while setting up the MongoDB Trigger, here are some common troubleshooting tips:
-
Double-check the Webhook URL: Ensure that you've copied the correct Webhook URL from BuildShip and pasted it into the MongoDB Function script.
-
Verify dependencies: Make sure you've added the required
axios
andcrypto
dependencies to the MongoDB Function. -
Review the logs: Check the MongoDB Function logs for any error messages or helpful information that can assist in troubleshooting.
Input / Output Mapping and Get Data
You can map your MongoDB Trigger payload for the subscribed events to be passed as inputs to your workflow. MongoDB sends a different structure of payload for different events.
You can also use the Get Data
button to fetch the latest data from the MongoDB API for the selected event type. To use this feature, we just need to trigger the specified event, for example: update an existing entry in your MongoDB Collection to trigger the update
event.
This trigger does not have any output mapping as it is an event-based trigger.
Learn more about how to map the workflow inputs and use the Get Data feature.
Need Help?
- 💬Join BuildShip Community
An active and large community of no-code / low-code builders. Ask questions, share feedback, showcase your project and connect with other BuildShip enthusiasts.
- 🙋Hire a BuildShip Expert
Need personalized help to build your product fast? Browse and hire from a range of independent freelancers, agencies and builders - all well versed with BuildShip.
- 🛟Send a Support Request
Got a specific question on your workflows / project or want to report a bug? Send a us a request using the "Support" button directly from your BuildShip Dashboard.
- ⭐️Feature Request
Something missing in BuildShip for you? Share on the #FeatureRequest channel on Discord. Also browse and cast your votes on other feature requests.