I have a document structure like this:
[
{
"country": "UK",
"shops": [
{"city": "London", "fruits": ["banana", "apple"]},
{"city": "Birmingham", "fruits": ["banana", "pineapple"]},
],
},
{
"country": "DE",
"shops": [
{"city": "Munich", "fruits": ["banana", "strawberry"]},
{"city": "Berlin", "fruits": ["kiwi", "pineapple"]},
],
},
]
In my python script, i have a dict mapping each fruit to a category value:
categories = {
1: ["apple"],
2: ["banana", "kiwi"],
3: ["pineapple", "strawberry"]
}
DESIRED OUTPUT:
Now i would like to get, using the mongo aggregation framework, for each document the max_category projected from the max value found in the shops sub-documents based on the category mapping.
[
{
"country": "UK",
"shops": [
{"city": "London", "fruits": ["banana", "apple"]},
{"city": "Birmingham", "fruits": ["banana", "pineapple"]},
],
"max_category": 3
},
{
"country": "DE",
"shops": [
{"city": "Munich", "fruits": ["banana", "apple"]},
{"city": "Berlin", "fruits": ["kiwi", "apple"]},
],
"max_category": 2
},
]
Thanks for your help!