My collection contains a document legends
with a nested array value
containing nested documents. I need to find the minimum quantity (single value) for each document inside the BASIC
array inside value
.
Here is a Mongo Playground that contains both the collection and my attempt. As you can see from the result the $min
operator doesn't return a single value but all the quantity
from the documents of the BASIC
array but, for instance, I need the minimum single value that for the meteoEventType
equals to RAIN
should be 0.01.
As $$thresholds.BASIC.quantity
returns a nested array, you need to flatten the array via $reduce
operator and combine arrays into one with $concatArrays
.
{
$addFields: {
rainMinThreshold: {
$let: {
vars: {
thresholds: {
$filter: {
input: "$value",
as: "v",
cond: {
$eq: [
"$$v.meteoEventType",
"RAIN"
]
}
}
}
},
in: {
$min: {
$reduce: {
input: "$$thresholds",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this.BASIC.quantity"
]
}
}
}
}
}
}
}
}