Hello,
I have a complex data structure and having an extremely difficult time with an aggregation query to get the result I was hoping for.
Here is a sample of my data structure, I had to really obscure this due to sensitive content reasons. I will have hundreds of thousands of documents in this collection.
What I am looking to do is try and retrieve the lowest value_b
and the highest value_b
from element
Working with MongoDB Community 6.0
{
"data": {
"array_of_data": [
{
"element": {
"elementData": [
{ "value_a": 1, "value_b": 200},
{ "value_a": 2, "value_b": 2500 }
]
}
},
{
"element": {
"elementData": [
{ "value_a": 1, "value_b": 150},
{ "value_a": 2, "value_b": 5600 }
]
}
}
]
}
}
Looking for a result something like the following
{
"min": 150,
"max": 5600
}
I have tried quite a few options so far, but here is latest with the closest that I have gotten
[{
$project: {
'data.array_of_data.element.elementData.value_b': 1
}
}, {
$group: {
_id: '$data.array_of_data.element.elementData.value_b'
}
}, {
$addFields: {
minA: {
$min: '$_id'
},
maxA: {
$max: '$_id'
}
}
}, {
$addFields: {
low: {
$min: '$minA'
},
high: {
$max: '$maxA'
}
}
}, {
$project: {
low: 1
}
}]
I appreciate any suggestions that someone may have.