0

i have the following mongoose schema:

// UnitDataLogSchema Schema
var UnitDataLogSchema = new mongoose.Schema({
  serialnumber: {
    type: String
  },
  timestamp: Number,
  temperatures: []
});

so i will get a collection with the following data:

{ "_id" : 1, "timestamp": 100, "temperatures" : [1,2,3] }
{ "_id" : 1, "timestamp": 200, "temperatures" : [4,5,6] }
{ "_id" : 1, "timestamp": 300, "temperatures" : [7,8,9] }
{ "_id" : 1, "timestamp": 400, "temperatures" : [10,11,12] }
....

how can i aggregate this collection so i can have this final object:

{
  zonetemperatures:[
                    [ [100,1],[200,4],[300,7],[400,10] ],...
                    [ [100,2],[200,5],[300,8],[400,11] ],...
                    [ [100,3],[200,6],[300,9],[400,12] ],...
                   ]
} 

with this i will have an array that each element is a serie in the Highstock chart.

and i can easily set data in the chart like this:

for (let index = 0; index < zonetemperatures.length; index++) {
  chart.series[index].setData(zonetemperatures[index]);

}
chart.redraw();
Rui Sebastião
  • 855
  • 1
  • 18
  • 36

2 Answers2

0

I usually perform data transformations on the front-end to keep the back-end there for querying. With that said, one solution on your front-end is as follows:

// dataCollection is a json array of data returned by your back-end
// make sure it isn't empty, wrap it in an if possibly...
var seriesLength = dataCollection[0].temperatures.length;

for(var i = 0; i < seriesLength; i++) {
    var series = [];

    for(int j = 0; j < dataCollection.length; j++)
        series.push(dataCollection[j].temperatures[i]);

    chart.series[i].setData(series);
}

chart.redraw();
TheBeardedOne
  • 292
  • 3
  • 6
  • that is another question that i have, should be the back or the front end to execute the data transformation (i agree that doing it in the front end is the best choice), but with the power (and complex for me) of aggregation framework of mongodb i was tempted to do this in the server side... – Rui Sebastião Feb 01 '18 at 23:39
  • 1
    What you might be looking for is unwind: https://stackoverflow.com/questions/21509045/mongodb-group-by-array-inner-elements As for front-end vs back-end, I would do this depending on the load of the back-end; the client does not tend to do much anyway. – TheBeardedOne Feb 01 '18 at 23:51
0

Try this..

db.unit_data.aggregate([{
        $unwind: "$temperatures"
    },
    {
        $project: {
            "zonetemperatures": ["$timestamp", "$temperatures"]
        }
    },
    {
        "$group": {
            _id: null,
            zonetemperatures: { $push: "$zonetemperatures" }
        }
    }
])
Kapil Barad
  • 716
  • 7
  • 17