Complex MongoDB Merge

I would like to perform a complex merge:

e.g.

[
{
   "one.two.three": 4,
   "number.two": "B"
},
{
   "one.two.three": 7,
   "number.two": "A"
},
{
   "one.two.three": 10,
   "number.two": "B"
}
]

where the result is:

{
   "one.two.three": 10,
   "number.two": "A"
}

because those are the maximum values…I could have any N+ number of arbitrary KV pairs, so I can’t just sort on a specific field

I am not sure about

The value A is usually considered smaller than B.

It is not really common to have dots in field names.

I usually avoid working with dynamic and arbitrary keys as it makes life harder. I use the attribute pattern and then it becomes easy because a simple $group can be use.

You could always use $objectToArray to transform your data to a dynamic attribute pattern and the use $group as above. But if you frequently do this aggregation you might as well store the data using the pattern and save the extra step doing the $objectToArray.