Hi,
I am trying to find an efficient way to group and yet preserve the data. I need to find the epoch timestamp when the value is at peak.
Issue 1: I only want to addToSet the value that is greater than the last added value to the set. That way I do not run into memory constraints and the array does not grow very big. Is there a better way to do. Currently I am adding the values that are > 0.
Issue 2:
$cond expects to provide an else statement. I am providing $$REMOVE but this results in an array with no elements. Is there a way to avoid it.
{
'$group': {
'_id': {
'devid': '$devId',
'type': '$type',
'searchDate': '$searchDate'
},
'maxVal': {
'$max': '$value'
},
'data': {
'$addToSet': {
'value': {
'$cond': [
{
'$gt': [
'$value', '0'
]
}, '$value', '$$REMOVE'
]
},
'epochtime': {
'$cond': [
{
'$gt': [
'$value', '0'
]
}, '$epochtime', '$$REMOVE'
]
}
}
}
}{
'$project': {
'_id': 1,
'data': {
'$arrayElemAt': [
'$data', {
'$indexOfArray': [
'$data.value', {
'$toString': '$maxVal'
}
]
}
]
}
}
}