Schedule reports using MongoDB Charts

If you’re running a business and using MongoDB as your database, you might be interested in tracking your sales over time to spot trends and make better decisions. MongoDB Atlas provides a powerful tool: Atlas Charts. In this post, we’ll show you how to use it to create a scheduled report of your sales.
Let’s start using this data model as an example for a sale:

{
 "date": "2023-01-03",
 "item": "mug",
 "quantity": 2,
 "price": 10
}

1. Adding some data for the reports

But to run this example we need to have some data on our database, so let’s create a function to add some random data using the data model that I show above, if you don’t know MongoDB has a JS interpreter so you can go to Mongosh or using Compass that could be the easiest way and use the terminal that it’s integrated on the tool. you need to click below to see the Mongo terminal.

After you see the terminal you need to run these commands to create a new database and the sales collection(You can do this with Compass UI Also if you are familiar with it).

//Create new store database
use store

// Create a new collection called "sales"
db.createCollection("sales")

Now you can copy and paste the function to load some data, remember that MongoDB has a JS interpreter and you can run this script the times that you want to create new documents of sales.

const products = ["mug", "bag", "shirt"];

function randomInt(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

//Create mockData for random product with random quantity and price
function createMockSalesDataForDay(date, products) {
 const salesData = [];
 for (let product of products) {
   const quantity = randomInt(1, 10);
   const price = randomInt(5, 20);
   salesData.push({
     date: date.toISOString(),
     item: product,
     quantity: quantity,
     price: price
   });
 }
 return salesData;
}

const today = new Date();
const salesData = createMockSalesDataForDay(today, products);
db.sales.insertMany(salesData);

2. Create some charts

Now we have data, it’s time to create some charts to be included in our report, let’s create a chart:

  1. Open the Atlas dashboard and navigate to your project.
  2. Click on “Charts” in the left-hand menu.
  3. Click on “New Chart”.
  4. Select the database and collection that contains your sales data.
  5. Choose the type of chart you want to create. For this example, we will use a grouped column chart.
  6. Choose the fields you want to include in your chart. For this example, we will include the “item” field and the “quantity” field.

We also choose a Donut chart, but we did something different here. We created a new field that is the result of multiplying quantity and price to get how products are giving more earnings.

To create this kind of calculated field you need to click on the “+” button next to the fields.

Add the name and the expression to create a calculated field, I used the $multiply operator to create a total amount of sales by item.

Customize your chart by selecting colors, fonts, and other design elements.

3. Schedule the report

Now that you have created your chart, you can use Atlas Charts to automatically send a report of your sales data. To schedule a report, follow these steps:

  • Open the Atlas dashboard and navigate to your project.
  • Click on “Charts” in the left-hand menu.
  • Find the dashboard you want to use for your report and click on.
  • Select “Schedule Report”.
  • Choose the frequency and time for your report.
  • Select the recipients for your report. You can choose to send the report to one or more email addresses.
  • Customize the content of your report by adding a title and a message.

In conclusion Atlas Charts is a powerful tool that allows you to create charts and graphs based on the data stored in your MongoDB database. By using Atlas Charts, you can automatically send reports of your sales data to one or more email addresses, making it easy to keep track of your business’s performance, even running filters on data that you show or even creating virtual fields.

Contact us to schedule your consultation.

Delbridge is a privately held global company with offices in Canada, the USA, Costa Rica, and Romania.

Delbridge Solutions specializes in providing Corporate Performance Management, Sales Performance Management, and Data & Software Engineering.

888.866.6176

 info@delbridge.solutions

Join the Delbridge Community!