Monitor Business KPIs With MongoDB Charts

Ankur Raina
4 min readMar 5, 2022

MongoDB is the default choice for bringing your giant ideas to life. Your business is flourishing with customer orders, and you are in need of beautiful dashboards for your teams to have a visibility into the most important KPIs to deliver amazing customer experience.

My friend is in a similar situation running a light speed food delivery business and would like to be able to answer queries such as:

How many orders were out for delivery between 5.00 pm and 8.00 pm IST yesterday?

Let’s help him.

His food orders collection looks like below:

Fig: Document in the orders collection

The fields of our interest are orderTime & status. status can have 4 values: submitted, processing, outfordelivery and delivered. When the order is created, the status is “submitted”. At every stage in the fulfilment of this order, the value of the status field changes accordingly.

To answer the type of questions asked earlier, we will need to have the time of each change captured as well. We would also like to create real time dashboards to keep an eye on the orders as they move through the fulfilment process.

For better reasons which may not be evident in this simple example, we will not overload our collection with the change data, but instead, we will capture changes in a separate collection.

MongoDB 5.0+ has support for native Timeseries collections which are highly optimised for this type of workload. We will create a new timeseries collection called bizmetrics for this. To capture the status changes, we will use Atlas Triggers.

Capture Changes and Ingest in Timeseries

We can use Atlas console to create the timeseries collection (it can also be done through mongo shell). We will use the default granularity of “Seconds” and status as the metaField.

We will create an Atlas trigger on orders collection watching for inserts and updates. The trigger will run the following function which takes the change event, gets the fullDocument and clusterTime fields from it and inserts that in bizmetrics collection:

exports = async function(changeEvent) {const fullDocument = changeEvent.fullDocument;
fullDocument.changeTime = new Date(Date(changeEvent.clusterTime.t));
console.log(JSON.stringify(fullDocument))const ts_collection = context.services.get("SizingCalc").db("peek").collection("bizmetrics");var res = await ts_collection.insertOne(fullDocument)
console.log(res)
return
};

We now have 4 events for each order in bizmetrics collection as they go through the fulfilment process.

Now, we will expose bizmetrics as the data source to MongoDB Charts.

And create a beautiful dashboard using this data. This dashboard will be useful for business operation teams.

Now finally, let’s answer our original question:

Try out this filter yourself in the public dashboard below:

https://charts.mongodb.com/charts-ankur_raina-jqukv/public/dashboards/acbef57c-8f41-41e2-bf38-9ce3101d8139

Send a clap if you enjoyed reading this article.

Code Link:

https://github.com/Ankur10gen/FoodDeliveryMock

Note: I’m using Realm Functions to generate and process the mock data. I’ll post the code for these Functions, Atlas Triggers, Mocking of the Source Data and Charts on Github. I’ll update this article with the link when it’s ready to share. These charts can also be embedded in your web applications. I’ll share some details around this as well. Cheers.

(Atlas Triggers like this can also be used to deal with Data Historization problems.)

--

--