Database

[{"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"]}]},{"country":"DE2","shops":[{"city":"Munich","fruits":["banana","apple"]},{"city":"Berlin","fruits":["kiwi","apple"]}]}]

Query

db.collection.aggregate([{"$unwind":"$shops"},{"$set":{"max_category":{"$reduce":{"input":[{category:1,fruits:["apple"]},{category:2,fruits:["banana","kiwi"]},{category:3,fruits:["pineapple","strawberry"]}],"initialValue":null,"in":{"$cond":{"if":{$and:[{$gt:["$$this.category","$$value"]},{$gt:[{$size:{"$setIntersection":["$$this.fruits","$shops.fruits"]}},0]}]},"then":"$$this.category","else":"$$value"}}}}}},{"$group":{"_id":"$_id","country":{$first:"$country"},"max_category":{$max:"$max_category"},"shops":{"$push":"$shops"}}}])

Result