Dear community,
I am new to MongoDB and I am a bit overwelhmed by the myna operators when using aggregation.
Considering the following simplified document structure stored in a MongoDB collection:
[
{ 'reference': 'ReferenceStr',
'subgroup': [ { '#': 1, 'param': 123},
{ '#': 2, 'param': 456},
{ '#': 3, 'param': 789}
],
'group': [ {'#': 1, 'start': 1, 'end': 2},
{'#': 2, 'start': 3, 'end': 3} ],
}
]
Knowing a condition on $group.# (let’s say $group.#=1), I want to use the associated value of $group.end (here 2) to get the value of $subgroup.param corresponding to $subgroup.#=$group.end
I would like to return: {‘reference’: ‘ReferenceStr’, ‘var’: 456}
So far I have only:
db_collection.aggregate([
{ '$project' : {
'reference' : '$reference',
'var' : {
...
}
}
}
])
Are $indexOfArray and $arrayElementAt my only hope? I get lost in the structure. Is there something more straight forward?
Any help would be very much appreciated, please.