I have a collection of recipes that have servings array. Each serving has a numeric energy property. I need to select all recipes with at least one serving that has energy that falls into the given range.
For example I have 2 recipes:
{
title: 'Greek salad',
servings: [
{
energy: 100
},
{
energy: 150
}
]
},
{
title: 'Smashed potatoes',
servings: [
{
energy: 150
},
{
energy: 200
}
]
}
I am looking for a query that would match only Greek salad
given the range is 90..110
, and both recipes for 140..160
range.
The best I came up with:
db.recipes.find({$and: [
{'servings.energy' : {$gt: 90}},
{'servings.energy' : {$lt: 110}}
]}
matches 2 results, which is not what is expect.
PS. There is a 1M+ records in the collections and I'm looking for a query that would run entirely out of index. Maybe I could change the data structure somehow to satisfy my needs?