{
name: "use_name",
grades: [
{class: "math": grade: 100},
{class: "english": grade: 90}
]
}
How do I write an aggregation pipeline to output:
{
name: "use_name",
grades: {class: "math": grade: 100},
}
The grades field has been reduced to the element where its grade property is the maximum of all elements.
The requirements, the aggregation pipeline cannot have $unwind
or $group
because it cannot have a stage where the stage needs to receive all incoming documents before outputting to the next stage, potentially exceeding the 100mb limit. And it must be fast.
2
Answers
Replace
$$value
in$reduce
until you find the max.Mongo Playground
I think this one is faster:
Mongo Playground
or this one: