0
  • "ingredients1":["banana","apple", "milk"]
  • "ingredients2":["orange","apple", "water"]
  • "ingredients3":["banana","kiwi", "milk"]
  • "inventory":["banana","kiwi","milk","apple"]

Hello, these are the arrays I need to work on and need to find a way to check which ingredients arrays are covered by inventory array.

The query builder looks like that: https://ibb.co/gTMcbL6

Thanks ahead.

I tried this query and it returned null but I was expecting to see the ingredient arrays which are covered by the inventory array.

  • What do you mean by "covered by the inventory array"? Please provide an example of a query and the expected result. – Alex Mamo Apr 28 '23 at 12:21
  • Assuming ingredients are required to make desserts and the inventory is the ingredients I have. I need to get the list of the desserts I can make with the ingredients I have. – Alper Gökgöz Apr 28 '23 at 12:45

1 Answers1

0

If you want to check for multiple ingredients that would require an array-contains-all operation, which does not currently exist on Firestore.

A workaround would be to store the ingredients as a map, instead of as an array. So something like:

ingredients-map: {
  banana: true,
  apple: true, 
  milk: true
}

With that maps, and the right indexes on it, you can then perform a query with multiple equality checks to find the recipes with all the right ingredients.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I cant understand the query format here. With this structure would I not need to query with ingredient-map.banana == true and so on? Can I use the IN function with maps? Because I will need to search for example 1000 recipes to see which desserts I can make with the ingredients I have. As I understand array_contains function checks if the array in my document has the value/s I query and nothing more but I need the vice versa(if the array/map is covered by the array/map on the right side) – Alper Gökgöz Apr 29 '23 at 12:51
  • "With this structure would I not need to query with ingredient-map.banana == true and so on?" Yes. --- "Can I use the IN function with maps?" No. --- I recommend giving this data model a try based on what I explained and the questions I linked (many of which contain code samples). If you can't get it to work, post back with your updated data model, the code that you tried, and the output/error you got. – Frank van Puffelen Apr 29 '23 at 14:32
  • Alright, I'll do that, thanks for now and in advance. – Alper Gökgöz Apr 30 '23 at 15:41