Firestore Or Query
Imagine I have a collection in Firestore with objects of the following format: { /* ... */ ownerId: 'id-1', memberIds: [ 'id-2', 'id-3', 'id-4' ] } How can I q
Solution 1:
It's not possible in Firestore to execute an OR query like the one you describe.
One solution is to denormalize your data (a very common approach in the NoSQL world) and add, to the document, an extra field which "concatenates" the two other fields, as follows:
{
/* ... */
ownerId: "id-1",
memberIds: [
"id-2",
"id-3",
"id-4"
],
ownersAndMemberIds: [
"id-1",
"id-2",
"id-3",
"id-4"
]
}
Then you can easily query the doc with
return collection('whatever')
.where('ownersAndMemberIds', 'array-contains', 'id-1');
Of course it requires that you maintain this array aligned with the other fields, but this is not difficult. Either you do it from your front-end when you update one of the two "main" fields, or through a Cloud Function which is triggered on any change to the doc.
Post a Comment for "Firestore Or Query"