It looks like you are relying on your favorite food for every person who is first in the array. If so, there is an aggregation structure operator that you can use.
Here is the pipeline you can use:
db.people.aggregate( [ { "$unwind" : "$favourite_foods" }, { "$group" : { "_id" : { "name" : "$name", "height" : "$height" }, "faveFood" : { "$first" : "$favourite_foods" } } }, { "$group" : { "_id" : "$faveFood.name", "height" : { "$max" : "$_id.height" } } } ])
In this sample dataset:
> db.people.find().pretty() { "_id" : ObjectId("508894efd4197aa2b9490741"), "name" : "Russell", "favourite_foods" : [ { "name" : "Pizza", "type" : "Four Cheeses" }, { "name" : "Burger", "type" : "Veggie" } ], "height" : 6 } { "_id" : ObjectId("5088950bd4197aa2b9490742"), "name" : "Lucy", "favourite_foods" : [ { "name" : "Pasta", "type" : "Four Cheeses" }, { "name" : "Burger", "type" : "Veggie" } ], "height" : 5.5 } { "_id" : ObjectId("5088951dd4197aa2b9490743"), "name" : "Landy", "favourite_foods" : [ { "name" : "Pizza", "type" : "Four Cheeses" }, { "name" : "Pizza", "type" : "Veggie" } ], "height" : 5 } { "_id" : ObjectId("50889541d4197aa2b9490744"), "name" : "Augie", "favourite_foods" : [ { "name" : "Sushi", "type" : "Four Cheeses" }, { "name" : "Pizza", "type" : "Veggie" } ], "height" : 6.2 }
You get the following results:
{ "result" : [ { "_id" : "Pasta", "height" : 5.5 }, { "_id" : "Pizza", "height" : 6 }, { "_id" : "Sushi", "height" : 6.2 } ], "ok" : 1 }