Let's say I have the following DB:
pizzas = [{
name: "pizza1",
toppings: ['mushrooms', 'pepperoni', 'sausage']
},
{
name: "pizza2",
toppings: ['mushrooms', 'pepperoni']
},
{
name: "pizza3",
toppings: ['mushrooms', 'onions']
},
{
name: "pizza4",
toppings: ['mushrooms']
}]
Now I want to fetch the pizzas that have 'mushrooms', 'pepperoni', or 'onions' and any combination of those. Then the query could be:
pizzas.find({toppings: ['mushrooms', 'pepperoni', 'onions']})
This would return all four pizzas in my db. But here's the problem. What if I wanted pizzas with any combination of only those three toppings, i.e. a pizza can not contain a different topping like 'sausage'. For this query, I only want "pizza2", "pizza3", and "pizza4" to be returned. I could make a query like:
pizzas.find({$and: [{toppings: ['mushrooms', 'pepperoni', 'onions']}, {$not: {toppings: ['sausage']}}]
The problem is that this requires me to know all of the possible toppings to exclude. Is there a better way to construct this query?