MongoDB Time Series with C++
Rate this tutorial
data is a set of data points collected at regular intervals. It’s a common use case in many industries such as finance, IoT, and telecommunications. MongoDB provides powerful features for handling time series data, and in this tutorial, we will show you how to build a C++ console application that uses MongoDB to store time series data, related to the Air Quality Index (AQI) for a given location. We will also take a look at to visualize the data saved in the time series.
Libraries used in this tutorial:
Launch powershell/terminal as an administrator and execute commands shared below.
This tutorial assumes we are working with x64 architecture. If you are targeting x86, please use this command:
Note: Below warning (if encountered) can be ignored.
The AQI is a measure of the quality of the air in a particular area, with higher numbers indicating worse air quality. The AQI is based on a scale of 0 to 500 and is calculated based on the levels of several pollutants in the air.
Once we have set up a Visual Studio solution, let’s start with adding the necessary headers and writing the helper functions.
- Make sure to include
<cpr/cpr.h>to access methods provided by the cpr library.
Note: Since we installed the cpr library with vcpkg, it automatically adds the needed include paths and dependencies to Visual Studio.
- (URI) to the cluster and create a new environment variable with key as
“MONGODB_URI”and value as the connection string (URI). It’s a good practice to keep the connection string decoupled from the code. Similarly, save the API token obtained in the Prerequisites section with the key as
Navigate to the Solution Explorer panel, right-click on the solution name, and click “Properties.” Go to Configuration Properties > Debugging > Environment to add these environment variables as shown below.
“getAQI”function makes use of the cpr library to make a call to the REST API, fetching the AQI data. The response to the request is then parsed to get the AQI figure.
With all the helper functions in place, let’s write the main function that will drive this application.
- The main function creates/gets the time series collection by specifying the
“create_collection”method. Note: MongoDB creates collections implicitly when you first reference the collection in a command, however a time series collection needs to be created explicitly with “”.
- Every 30 minutes, the program gets the AQI figure and updates it into the time series collection. Feel free to modify the time interval as per your liking by changing the value passed to
When this application is executed, you can see the below activity in the console window.
You can also see the time series collection in Atlas reflecting any change made via the console application.
We can make use of MongoDB Charts to visualize the AQI data and run aggregation on top of it.
Step 2: Click on “Add Chart”.
Step 3: In the “Select Data Source” dialog, go to the “Project” tab and navigate to the time series collection created by our code.
Step 4: Change the chart type to “Continuous Line”. We will use this chart to display the AQI trends over time.
Step 5: Drag and drop the “timestamp” and “aqi” fields into the X axis and Y axis respectively. You can customize the look and feel (like labels, color, and data format) in the “Customize” tab. Click “Save and close” to save the chart.
Step 6: Let’s add another chart to display the maximum AQI — click on “Add Chart” and select the same data source as before.
Step 7: Change the chart type to “Number”.
Step 8: Drag and drop the “aqi” field into “Aggregation” and change Aggregate to “MAX”.
Step 9: We can customize the chart text to change color based on the AQI values. Let’s make the text green if AQI is less than or equal to 100, and red otherwise. We can perform this action with the conditional formatting option under Customize tab.
Step 10: Similarly, we can add charts for minimum and average AQI. The dashboard should finally look something like this:
Tip: Change the dashboard’s auto refresh settings from “Refresh settings” button to choose a refresh time interval of your choice for the charts.
With this article, we covered creating an application in C++ that writes data to a MongoDB time series collection, and used it further to create a MongoDB Charts dashboard to visualize the data in a meaningful way. The application can be further expanded to save other parameters like and temperature.