How to Send a Weekly MongoDB Product Performance Digest by Email
Build a Spojit workflow that runs every Monday morning, aggregates an orders collection in MongoDB to rank your best and worst selling products, shapes the result into a clean CSV table, and emails the digest to your merchandising managers.
What This Integration Does
Merchandising teams need a regular, no-effort snapshot of which products are pulling their weight and which are stalling. Instead of someone exporting orders by hand and pasting numbers into a spreadsheet every week, this workflow does the whole job on a schedule. It reads last week's orders straight from your MongoDB orders collection, sums revenue and units per product, ranks the top and bottom performers, formats everything into a CSV table, and delivers it to a fixed list of recipients. The result is a dependable Monday digest that lands in inboxes before the team starts planning the week.
The workflow is driven by a Schedule trigger, so nothing external has to call it: Spojit runs it on a cron expression in your chosen timezone, and the trigger output is simply {{ scheduledAt }}. From there the data flows in one direction: the MongoDB aggregate tool returns ranked rows, a Transform node tidies those rows and adds computed fields with the math connector, the csv connector turns the rows into a CSV string, and the Send Email node delivers it. The workflow is read-only against your database and stateless between runs, so each Monday produces a fresh independent digest. If a run fails or is re-run manually, it simply recomputes the same window of data with no duplicated side effects.
Prerequisites
- A MongoDB connection added under Connections with read access to the database that holds your orders. See the MongoDB connector article for setup.
- An orders collection whose documents include a per-line-item product reference (for example
productIdandproductName), a quantity, a line revenue figure, and an order date you can filter on. - Each merchandising manager's email address added to your org allowlist under Settings → General → Email recipients so the Send Email node can reach them.
- Knowledge of your collection's field names. The aggregation pipeline below assumes line items in a
lineItemsarray; adjust the field paths to match your schema.
Step 1: Add a Schedule trigger for the weekly run
Create a new workflow and set the trigger to Schedule. A Schedule trigger uses a 5-field Unix cron expression plus an IANA timezone. To run every Monday at 7:00 AM Sydney time, enter the cron expression 0 7 * * 1 and set the timezone to Australia/Sydney. A single trigger can hold more than one schedule if you want, for example, a second weekday run, but one entry is enough here. The trigger output is {{ scheduledAt }}, which you can carry into the email subject so recipients can see exactly when the digest was generated.
Step 2: Aggregate last week's orders in MongoDB
Add a Connector node in Direct mode on the MongoDB connector and choose the aggregate tool. Set collection to your orders collection name (for example orders) and paste a pipeline into the pipeline field that filters to the last seven days, unwinds line items, groups by product, and sorts by revenue. A pipeline like the following ranks products by total revenue and units:
[
{ "$match": { "createdAt": { "$gte": { "$date": "2026-06-14T00:00:00Z" } } } },
{ "$unwind": "$lineItems" },
{ "$group": {
"_id": "$lineItems.productId",
"productName": { "$first": "$lineItems.productName" },
"units": { "$sum": "$lineItems.quantity" },
"revenue": { "$sum": "$lineItems.lineTotal" }
} },
{ "$sort": { "revenue": -1 } }
]
The aggregate tool returns the result documents as an array. Bind the node output to a variable such as ranking so later steps can reference {{ ranking }}. Computing the exact rolling-seven-day boundary is covered in the next step.
Step 3: Compute the date window with the date connector
Hardcoding a date inside the pipeline means it never moves. To keep the window rolling, add a Connector node in Direct mode on the date connector before Step 2 and use subtract to take 7 days off now, then format the result into an ISO string. Bind it to weekStart and reference {{ weekStart }} inside the $match stage of your pipeline in place of the literal date. This way every Monday the workflow looks back exactly one week from the moment it runs. Place this date node directly after the trigger and feed its output into the MongoDB aggregate node.
Step 4: Shape the ranking and add computed fields
Add a Transform node to reshape {{ ranking }} into the exact columns you want in the email table: a clean product name, units sold, revenue, and a share-of-total percentage. To compute each product's share of total revenue, use the math connector. First add a Connector node in Direct mode on the math connector with the sum tool over the revenue values to get the period total, binding it to totalRevenue. Then, for the percentage, use the math connector's percentage tool to express each product's revenue against {{ totalRevenue }}. In the Transform node, build an array of row objects shaped like this:
[
{
"product": "{{ item.productName }}",
"units": "{{ item.units }}",
"revenue": "{{ item.revenue }}",
"share_pct": "{{ item.sharePct }}"
}
]
You can also slice the ranking into a top group and a bottom group here so the digest highlights both ends. Bind the shaped array to rows.
Step 5: Convert the rows into a CSV table
Add a Connector node in Direct mode on the csv connector and choose the from-json tool. Set data to {{ rows }} (the array of objects from Step 4). Use the columns field to pin the column order and headers explicitly so the output is predictable:
["product", "units", "revenue", "share_pct"]
Leave header on so the first CSV row carries the column names, and keep the default delimiter of a comma. The from-json tool returns a single CSV string. Bind it to digestCsv so the email step can drop it straight into the body. If you want a quick top-and-bottom view rather than the full list, run the csv connector's slice tool on the result to keep just the first and last rows.
Step 6: Email the digest to merchandising managers
Add a Send Email node. Set Recipients to your merchandising managers as a comma-separated list (each address must be on the org allowlist). Set the Subject to something dated, for example Weekly product performance digest - {{ scheduledAt }}. In the Body, introduce the report and then include the table by referencing {{ digestCsv }}:
Hi team,
Here is this week's product performance ranking, top to bottom by revenue:
{{ digestCsv }}
Generated automatically by Spojit.
Set If sending fails to Fail the workflow so a delivery problem surfaces in your execution history rather than passing silently. Leave Reply-To at its default (the workflow owner) unless you want replies routed elsewhere. The Send Email node counts toward your monthly email allowance.
Tips
- If you want the CSV as a downloadable file rather than inline text, attach it: the Send Email node and the Attachment node both work with base64 content, so encode
{{ digestCsv }}with the encoding connector'sbase64-encodetool first. - Keep the aggregation lean: add a
$projectstage to return only the fields you need, and a$limitif your catalog is large, so the digest stays readable. - Use the math connector's
roundorcurrencytool on revenue figures before the CSV step so the table shows clean money values instead of long decimals. - Ask Miraxa, the intelligent layer across your automation, to scaffold this for you with a prompt like "Add a MongoDB aggregate node that groups orders by product and sorts by revenue, then convert the result to CSV and email it." Then fine-tune the pipeline in the properties panel.
Common Pitfalls
- Timezone drift: the cron expression runs in the timezone you set on the trigger, but your date math in Step 3 uses UTC unless you specify otherwise. Align both so "last week" means the same window your team expects.
- Schema assumptions: the pipeline assumes line items live in a
lineItemsarray withquantityandlineTotal. If your documents store orders differently, adjust the$unwindand$grouppaths or the run returns empty results. - Recipients not on the allowlist: the Send Email node only delivers to addresses on the org allowlist. A new manager's address must be added under Settings → General → Email recipients first.
- Empty weeks: if no orders fall in the window,
aggregatereturns an empty array and the CSV will have only headers. Add a Condition node to skip the email or send a "no activity" note when{{ ranking }}is empty.
Testing
Before turning on the schedule, validate the logic on a small scope. Add a temporary Manual trigger (or use the Run button) and run the workflow once, then open the execution in your history and inspect each step output: confirm the aggregate step returns the products you expect, that {{ rows }} has the right columns, and that {{ digestCsv }} looks correct. Narrow the $match window to a single recent day, or point the recipients at your own address first, so you can eyeball the formatting without emailing the whole team. Once the digest reads cleanly, switch the trigger back to Schedule and enable the workflow.