I am building a CRUD api for a post processing tool. I have some data that have a structure of:
{
_date: '3/19/2021',
monitor: 'metric1',
project: 'bluejays',
id1: 'test-pmon-2',
voltageCondition: 'HV',
testData: [],
id: null
}
So previous methods I had was I would maintain a separate MongoDB collection which stored high level data of jobs that are stored and these would displayed. This method worked however I would like just one collection for this group of data.
My current method iterates through all objects in the query. The query is relatively small and I set a limit on the return so there is no names really of speed and memory allocation. However, I would like to dev up a more efficient method. This is my current method:
jobHeaderList_2 = [];
queriedData.forEach(doc => {
if (jobHeaderList_2.length == 0) {
jobHeaderList_2.push({
id1: doc.id1,
project: doc.project,
monitor: [doc.monitor],
date: doc._date
})
}
else {
let index = jobHeaderList_2.map(obj => {
return obj.id1;
}).indexOf(doc.id1);
if (index == -1) {
jobHeaderList_2.push({
id1: doc.id1,
project: doc.projectId,
monitor: [doc.monitor],
date: doc._date
})
}
else if (jobHeaderList_2[index].monitor.includes(doc.monitor) == false) {
jobHeaderList_2[index].monitor.push(doc.monitor);
}
}
});
And this works fine, but I would like someone who is more experienced who can maybe help me with a better method of doing this. Basically, I want to group all objects with the same id1 into a single object that stores the monitor values in an array.
I do not want to change the data structure because it's efficient for the plotting that occurs elsewhere in the application.
jobHeaderList_2initialised? Is it empty when the loop starts? IsqueriedDataan array, or is it some Mongo query object?