# MongoDB Time Series with C++

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:
1. MongoDB C Driver version: 1.23.0
2. MongoDB C++ Driver version: 3.7.0
3. cpr library
4. vcpkg
5. Language standard:  C++17

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.

```
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
```

Step 2: Install libcpr/cpr.

```
./vcpkg install cpr:x64-windows
```

This tutorial assumes we are working with x64 architecture. If you are targeting x86, please use this command:

```
./vcpkg install cpr
```

Note: Below warning (if encountered) can be ignored.

```
# this is heuristically generated, and may not be correct
find_package(cpr CONFIG REQUIRED)
target_link_libraries(main PRIVATE cpr::cpr)
```

## 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 `` 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.

* `“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. 

```
#pragma once

#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

using namespace std;

std::string getEnvironmentVariable(std::string environmentVarKey)
{
char* pBuffer = nullptr;
size_t size = 0;
auto key = environmentVarKey.c_str();

// Use the secure version of getenv, ie. _dupenv_s to fetch environment variable.  
if (_dupenv_s(&pBuffer, &size, key) == 0 && pBuffer != nullptr)
{
std::string environmentVarValue(pBuffer);
free(pBuffer);
return environmentVarValue;
}
else
{
return "";
}
}

int getAQI(std::string city, std::string apiToken)
{
// Call the API to get the air quality index.
std::string aqiUrl = "https://api.waqi.info/feed/" + city + "/?token=" + apiToken;
auto aqicnResponse = cpr::Get(cpr::Url{ aqiUrl });

// Get the AQI from the response
if(aqicnResponse.text.empty())
{
cout << "Error: Response is empty." << endl;
return -1;
}
bsoncxx::document::value aqicnResponseBson = bsoncxx::from_json(aqicnResponse.text);
auto aqi = aqicnResponseBson.view()"data"]["aqi"].get_int32().value;
return aqi;
}

void saveToCollection(mongocxx::collection& collection, int aqi)
{
auto timeStamp = bsoncxx::types::b_date(std::chrono::system_clock::now());

bsoncxx::builder::stream::document aqiDoc = bsoncxx::builder::stream::document{};
aqiDoc << "timestamp" << timeStamp << "aqi" << aqi;
collection.insert_one(aqiDoc.view());

// Log to the console window.
cout << " TimeStamp: " << timeStamp << " AQI: " << aqi << endl;
}
```

### 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”`.

```
int main()
{
// Get the required parameters from environment variable.
auto mongoURIStr = getEnvironmentVariable("MONGODB_URI");
auto apiToken = getEnvironmentVariable("AQICN_TOKEN");
std::string city = "Delhi";
static const mongocxx::uri mongoURI = mongocxx::uri{ mongoURIStr };

if (mongoURI.to_string().empty() || apiToken.empty())
{
cout << "Invalid URI or API token. Please check the environment variables." << endl;
return 0;
}

// Create an instance.
mongocxx::instance inst{};
mongocxx::options::client client_options;
auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 };
client_options.server_api_opts(api);
mongocxx::client conn{ mongoURI, client_options };

// Setup Database and Collection.
const string dbName = "AQIMonitor";
const string timeSeriesCollectionName = "AQIMonitorCollection";

// Setup Time Series collection options.
bsoncxx::builder::document timeSeriesCollectionOptions =
{
   "timeseries",
{
  "timeField", "timestamp",
  "granularity", "minutes"
}
};

auto aqiMonitorDB = conndbName];
auto aqiMonitorCollection = aqiMonitorDB.has_collection(timeSeriesCollectionName)
? aqiMonitorDB[timeSeriesCollectionName]
: aqiMonitorDB.create_collection(timeSeriesCollectionName, timeSeriesCollectionOptions.view().get_document().value);

// Fetch and update AQI every 30 minutes.
while (true)
{ 
auto aqi = getAQI(city, apiToken);
saveToCollection(aqiMonitorCollection, aqi);
std::this_thread::sleep_for(std::chrono::minutes(30));
}

return 0;
}
```

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.

## 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.

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.

## 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!