I’m seeking advice on the implementation of real-time calculation of synthetic variables from two existing variables stored in MongoDB, with Nodejs & React frontend integration. Below is a detailed overview of my current architecture and specific challenges:
Collections Schema:-
Devices Collection: Stores information about different datasources.
{
"_id": ObjectId("..."),
"name": "Datasource Name",
"description": "Description of the datasource"
}
Variables Collection: Stores information about variables, including their names, descriptions, and units of measurement.
{
"_id": ObjectId("..."),
"name": "Variable Name",
"deviceId":ObjectId("...")
}
Values (Time Series) Collection: Stores time-stamped values for variables associated with variables belonging to a datasource.
{
"_id": ObjectId("..."),
"ts": ISODate("..."),
"val": 123.45,
"metafield": {
"variableId": ObjectId("..."),
}
}
Consider a scenario where data from a device is captured (every 10 seconds) and stored with multiple variables. For instance:
{
"power-1": 98.2,
"power-2": 23.5,
"voltage-1": 56.77,
"voltage-2": 122.56,
"frequency-1": 8,
"frequency-2": 54.7
}
Users should be able to select two variables (e.g., power-1** and power-2) and define an arithmetic operation (e.g., ((power-1 + power-2) * 100) / 1000). The result, referred to as a synthetic variable (e.g., power), should be calculated in real-time and displayed in real-time to the user whenever the user logs in.
How can i achieve this using mongodb with the above mentioned schema?