BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

MongoDB Time Series with C++

Rishabh Bisht6 min read • Published Feb 10, 2023 • Updated Apr 03, 2024
MongoDBTime seriesC++
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Time series 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 MongoDB Charts to visualize the data saved in the time series.
Libraries used in this tutorial:
This tutorial uses Microsoft Windows 11 and Microsoft Visual Studio 2022 but the code used in this tutorial should work on any operating system and IDE, with minor changes.

Prerequisites

  1. MongoDB Atlas account with a cluster created.
  2. Microsoft Visual Studio setup with MongoDB C and C++ Driver installed. Follow the instructions in Getting Started with MongoDB and C++ to install MongoDB C/C++ drivers and set up the dev environment in Visual Studio.
  3. Your machine’s IP address is whitelisted. Note: You can add 0.0.0.0/0 as the IP address, which should allow access from any machine. This setting is not recommended for production use.
  4. API token is generated using Air Quality Open Data Platform API Token Request Form to fetch AQI for a given location.

Installation: Libraries

Launch powershell/terminal as an administrator and execute commands shared below.
Step 1: Install vcpkg.
Step 2: Install libcpr/cpr.
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.

Building the application

Source code available here
In this tutorial, we will build an Air Quality Index (AQI) monitor that will save the AQI of a given location to a time series collection.
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.
We are going to build a console application from scratch. Follow the steps on how to set up the development environment in Visual Studio from our previous article Getting Started with MongoDB and C++, under the section “Visual Studio: Setting up the dev environment.”

Helper functions

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.
  • Get the connection string (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 “AQICN_TOKEN”.
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.
Setting environment variables in Microsoft Visual Studio
  • “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.
  • “saveToCollection” function saves the given AQI figure to the time series collection. Please note that adding the “timestamp” key-value pair is mandatory. A missing timestamp will lead to an exception being thrown. Check out different “timeseries” Object Fields in Create and Query a Time Series Collection — MongoDB Manual.

The main() function

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 “collection_options” to 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 “create_collection”.
  • 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 “sleep_for”.
When this application is executed, you can see the below activity in the console window.
AQI Monitor application in C++ with MongoDB time series
You can also see the time series collection in Atlas reflecting any change made via the console application.
MongoDB time series in MongoDB Atlas

Visualizing the data with MongoDB Charts

We can make use of MongoDB Charts to visualize the AQI data and run aggregation on top of it.
Step 1: Go to MongoDB Charts and click on “Add Dashboard” to create a new dashboard — name it “AQI Monitor”.
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.
Selecting data source for MongoDB Charts
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.
MongoDB Charts with time series
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”.
MongoDB Charts with time series
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.
Formatting options in MongoDB Charts
Step 10: Similarly, we can add charts for minimum and average AQI. The dashboard should finally look something like this:
MongoDB Charts dashboard
Tip: Change the dashboard’s auto refresh settings from “Refresh settings” button to choose a refresh time interval of your choice for the charts.
MongoDB Charts refresh settings

Conclusion

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 PM2.5 and temperature.
Now that you've learned how to create an application using the MongoDB C++ driver and MongoDB time series, put your new skills to the test by building your own unique application. Share your creation with the community and let us know how it turned out!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Update Array Elements in a Document with MQL Positional Operators


Feb 03, 2023 | 6 min read
Article

How Prisma Introspects a Schema from a MongoDB Database


May 19, 2022 | 8 min read
Industry Event
locationAMSTERDAM, NETHERLANDS | IN-PERSON

React Summit


Jun 14, 2024 - Jun 18, 2024
Code Example

EnSat


Feb 08, 2023 | 3 min read
Table of Contents