I'm working on a mobile app with flutter and I am trying to query from firebase a list of objects, the objects from firebase each have an array of strings that contains the categories of the object, and on flutter, I've got a list of strings that contains the categories I want to look at, I'd like to query all the objects from firebase that contains at least 1 of the string present in the array that I chose from flutter, but I haven't found yet a way to do so, also I saw that firebase limit the queries to 10 types, but I might want to have more than 10 types in my array. If someone could help me it would be great because I don't know where to start.
CodePudding user response:
Implementation:
First, this is the solution for your problem.
You need to use array-contains
. Example:
final objectsRef = db.collection("objects");
final myCategoryObjects =
objectsRef .where("categories", arrayContains: "category1");
More on this in this link documentation.
The 10 limit:
Note that the 10 limit does not apply in your case, because you are searching for only 1 category.
If you wanted to search for all objects that are "category 1" or "category 2", then the limit of 10 would apply to you. In this case, you have 2 solutions:
1- perform a local filter in your app (not in Firebase), or
2- add new fields to Firestore that are specific to each category separately. Example, field 1 is a bool "category1" (true or false)... field 2 is bool "category2" (true or false), and so on
Let me know if any follow-up.