'Operate multiple in operations in Firestore. Any hack possible?
I want to query on multiple fields based on multiple in
conditions as shown below:
db
.collection(firebasePaths.funfuse_verified_users)
.orderBy('uid')
.where('uid', 'not-in', notToIncludeAccounts)
.orderBy('skills')
.where('skills', 'in', skills)
.orderBy('interests')
.where('interests', 'in', interests)
Firestore limitation states that only one in
is allowed per query but I was interested in knowing if by some hacky way can I make this work? One thing I thought of is to fetch the two results and then take the intersection of those but this would mean a lot of load on my backend.
Is there some other way that I am not aware of which could be used for such scenarios? I want to filter the database based on skills and interest arrays.
Edit 1: Added Document Example Example of the document will be something like this:
{
"name": "Shivam",
"online": true,
"bio": "",
"skills": [
"Supply Chain",
"Inventory Management",
"Legal",
"Digital Marketing",
"Web"
],
"interests": [
"Healthcare"
],
"imageLoc": "profile-pics/profile-photo",
"isImageAvailable": true,
"uid": "",
"username": "tttt"
},
Expected query:
db
.collection(firebasePaths.funfuse_verified_users)
.orderBy('uid')
.where('uid', 'not-in', ['1','2'])
.orderBy('skills')
.where('skills', 'in', ["Supply Chain","Inventory Management"])
.orderBy('interests')
.where('interests', 'in', ["Supply Chain","Inventory Management"])
Something like this.
Solution 1:[1]
There is nothing hidden in the API to allow the use-case, so any workaround will have to come from changes to the data model.
The best I can think of is to add an additional field that combines the skills
and interests
values, and filter on that.
If you just add all the individual values ("skills_and_interests": ["Supply Chain", "Inventory Management", "Legal", "Digital Marketing", "Web", "Healthcare"]), you can do the equivalent of an OR operation on that with in
. If you want to be able to distinguish skills from interesting, you could prefix the values ("skill_Supply Chain"
, "interest_Healthcare"
).
If you want to filter on combined values, you'd have to add the combinations of skills and interests (e.g. "Supply Chain_Healthcare", "Inventory Management_Healthcare"
), so that you can then do an AND like operation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank van Puffelen |