Blog
Trigger an AI Agent When a MongoDB Collection Changes
Tutorial
ยท
Quick answer: To trigger an AI agent when a MongoDB collection changes, add the MongoDB Trigger to a BuildShip workflow and click Connect to generate a webhook URL. In the MongoDB dashboard, create a Database trigger on the collection you want to watch, set the event type to Function, and paste the script from the MongoDB Trigger docs, with your webhook URL on line 5. Add the axios and crypto dependencies, then run the function once. It should return true if the workflow is already shipped. When a catalog document changes, the agent can refresh its search data and log which fields changed. A general chat agent cannot receive the change event, update your index or leave a log of the run.

A product catalog only helps if search stays in step with it. Someone changes a title, a price or an image on a document. If the search index updates once a night, shoppers still find the old name until morning. A chat agent can rewrite a product blurb if you paste the document in, but it cannot receive the change, update the index or record which fields actually moved.
That is the job of an AI agent with a backend. The MongoDB change is the trigger. The agent refreshes the search document. BuildShip keeps a log of both.
This is not the same job as using MongoDB as a tool from an agent that someone has already started. We have a separate guide on building AI agent tools with MongoDB. This post is the other direction: a collection change starts the agent.
Why a collection-change agent beats a nightly reindex
The MongoDB Trigger parses the webhook payload from a MongoDB trigger, so a BuildShip workflow can run when something changes in a linked collection, database or deployment. The docs also mention that MongoDB can run a trigger on a schedule. This guide is the database trigger: the event is the change itself.
A general chat agent cannot do this on its own:
It has no URL that a MongoDB function can call.
It cannot upsert a search document, compare fields or write a change log.
It leaves no step-level record of which document changed or what was reindexed.
The agent is useful because not every change needs the same work. A typo in a title should refresh search text. A new description might need new embeddings. A stock-count change may not belong in a search document at all, and rebuilding vectors on every inventory tick would be slow and expensive. The agent can look at which fields changed, update only what search needs, and log the rest.
This is the same kind of โdatabase listenerโ story as the Supabase and Firestore triggers, on a MongoDB stack.
What the MongoDB Trigger actually does
The BuildShip side of this trigger is simple: you add it, click Connect, and you get a webhook URL. The trigger does not create the MongoDB trigger for you. You set that up in the MongoDB dashboard, with a Function that posts the change event to the URL, including a signature header so BuildShip can check the request.
The documented function uses axios to POST the change event, and crypto to create an HMAC SHA-256 signature. The signature is sent as X-Webhook-Signature. In the sample script, the secret is the last segment of the webhook URL (the trigger id). You paste the webhook URL on line 5 of the script, add both dependencies in the MongoDB function, and click Run. If the BuildShip workflow is shipped and the URL is right, the run should return true.
A few things to be clear about:
Choose a Database trigger in MongoDB, not a Scheduled trigger, for this agent. A scheduled MongoDB trigger is a different job.
MongoDB sends a different payload shape for different operations. After you update a document, use Get Data in BuildShip to map the fields you need.
There is no output mapping on this trigger. It is an event trigger. Writes back to MongoDB or to a search index happen in later steps.
If you also use BuildShipโs MongoDB nodes to read or write data, those nodes have their own connection settings, including network access on the MongoDB project. That is separate from the triggerโs webhook. A common production setup is to allow the BuildShip nodes to reach the cluster (the MongoDB node docs mention adding
0.0.0.0/0under Network Access if connections fail) while the trigger itself only receives what the Atlas function posts. Do not treat the webhook URL as a database connection string.
The body you receive is the MongoDB change event the function posted. Typical fields on a database change event include an operation type (such as insert, update or delete) and the document itself. If you enable document pre-images on the MongoDB trigger, you can also receive the document as it was before the change, which is how you log exactly which fields moved. If pre-images are off, refresh the search document from the new document and record the operation type instead of a field-level diff.
A concrete case: refresh search when a catalog item changes
Take a catalog collection, products, that backs both the admin app and a search index. When a merchandiser edits a title, a description or the tags, the search document should follow, and someone should be able to see what changed.
Here is the path I would build:
A document in
productsis inserted, updated or deleted. The MongoDB database trigger runs the function, which POSTs the change event to BuildShip.The MongoDB Trigger starts the workflow. You map the operation type, the document id and the new document, plus the previous document if you have a pre-image.
A Branch node continues for
productsonly, and skips operations you do not care about. A delete should remove the search document, not try to embed it.For updates, compare the new document with the previous one, if you have it, and list the fields that changed. If only a stock count changed, you can skip the search refresh and write a short log row.
If the title, description or tags changed (or this is an insert), build or refresh the search document. If you use vector search, generate new embeddings from the text fields in a later node, then upsert them into your search index. Do not invent a product name, a category or a price that is not already in the document.
Write a change-log row with the product id, the operation, the field list and a timestamp.
If the same change event is delivered twice, the upsert and the log key (document id plus change event id, or a timestamp you store) should be idempotent.
The model, if you use one, is there to rewrite a search blurb or to summarise what changed for the log. The upsert and the field comparison are ordinary backend work.

The collection change is the event. Search should follow the document, not a nightly job.
Set up the trigger
In BuildShip, create a workflow, open the Connect tab and add MongoDB Trigger. The trigger does not need extra fields. Click Connect and copy the webhook URL.
In the MongoDB dashboard, open Triggers and create a Database trigger. Point it at the project, database and collection you want to watch, such as
products.Under Function, set the event type to Function. Paste the script from the MongoDB Trigger documentation, and put your webhook URL on line 5.
Click Add Dependency and add
axios, then addcryptothe same way.Ship the BuildShip workflow, then click Run on the MongoDB function. You should see
truein the console if the URL and dependencies are right.Update a test document in the collection. In BuildShip, click Get Data and map the operation type, document id and document fields you need.
If the Run fails, check the webhook URL, confirm both dependencies are listed, and read the MongoDB function logs. The docsโ troubleshooting section starts with those three checks. Also confirm the BuildShip workflow has been shipped: a Connect URL that points at a workflow still in draft will not return true. After a successful Run, do a real document update as well, because Get Data reads the latest event, not the functionโs test output alone.
Build the catalog-search agent
Split deletes from writes. A delete should remove the search document and write a log row. It should not call a model.
Diff before you embed. If pre-images are available, compare the two documents in a script node and produce a list of changed fields. Use that list in a Branch: refresh search only when text fields that the index uses have changed.
Refresh the search document. Upsert the latest title, description, tags and any embedding you generate into the search index you already use. If you generate embeddings, do that from the document text, in a dedicated step, and log the product id. If a field is missing, skip it. Do not invent catalogue copy to fill a blank.
Optional summary for the log. An AI node (keyless nodes run on your BuildShip credits, or you can bring your own provider key) can turn the field list into a one-line change note:
> You write a one-line change log for a product catalog. Using only the list of changed fields and the new values provided, summarise what changed in one sentence. Do not mention fields that are not in the list. Do not invent a reason for the change.
Keep it idempotent. If MongoDB retries the function, the same change should upsert the same search document and not create a second log row for the same event id.
If you would rather not wire the canvas by hand, click Build with AI to open Compass and describe the flow in plain language. It builds on your actual canvas, so you can still test and edit every step. For a longer recrawl across related collections, the workflow can hand off to an AI agent built with Agent Max.

The upsert and the field-level log are the point of the agent.

A stock-count change should not rebuild embeddings. A title change should.
Test it, ship it and change one real product
Update a test product title, then confirm the webhook fired, the search document changed and a log row listed
title.Update only a stock field and confirm search was left alone, with a log that says so.
Delete a test document and confirm the search document was removed.
Trigger the same update twice and confirm you do not get two log rows for one change.
Click Ship when you are happy with a dry run against a development collection, not against the live catalog.
Open the logging panel at the bottom of the workspace to see what happened. You can switch from the Workflow view to the Node view to read the logs for a single step, and test runs show live logs as they happen. Production logs usually show up within about 45 seconds, and the refresh icon loads them straight away. To add your own messages, use the Log Message to Console node, or call logging.log inside any nodeโs code.
See the logging docs and the node failure docs for retries. Use version history before you change a live indexing prompt.
Built for teams that run this in production
Catalog listeners become load-bearing, so a few things matter beyond the first test:
Full code access. Open any node to read or change its JavaScript or TypeScript, including the way you verify the signature if you need to inspect it.
Team access control. Role-based access lets you decide who can view a workflow, who can edit it and who can ship it.
Your own Google Cloud. Workflows run on Google Cloud, and you can run them in your own Google Cloud project as an enterprise option.
A log per change. When someone asks why a product disappeared from search, you can open the run and see the event, the field list and the upsert.
For the wider pattern of starting an agent from a database change rather than from a timer, see Why AI Agents Need Listeners, Not Cron Jobs. If you already call MongoDB from an agent as a tool, keep that as a separate workflow, as in the MongoDB agent tools guide.
FAQ
Can I trigger an AI agent when a MongoDB collection changes?
Yes. Add the MongoDB Trigger, copy the webhook URL, and create a Database trigger in MongoDB that posts the change event to that URL with the documented function. An AI node in the same workflow can then refresh search data or write a change note, while other nodes upsert the index.
Does BuildShip create the MongoDB trigger for me?
No. Connect generates the webhook URL. You create the Database trigger in the MongoDB dashboard and paste the documented function, with that URL on line 5, plus the axios and crypto dependencies.
How do I test the connection?
Ship the BuildShip workflow, then click Run on the MongoDB function. It should return true. After that, update a document and use Get Data in BuildShip to map the payload.
What if I want a scheduled MongoDB job instead?
MongoDB also offers scheduled triggers. The BuildShip trigger docs mention that option, but this guide is the database trigger, where the event is a collection change. A clock-based job is a different workflow, and a different kind of trigger.
How much does it cost to try?
BuildShip plans start with a 7-day trial, and a card is required to start it. See the pricing page for plans and credits.
Start building
Start your 7-day trial (a card is required), add the MongoDB Trigger, and update a document in a development collection. If you are building catalog search for a larger team, talk to us about enterprise.





