Home > database >  How to query on firestore where field contains array of dictionary?
How to query on firestore where field contains array of dictionary?

Time:06-23

I am trying to fetch project data from FireStore and here is my structure on firestore:

enter image description here

And here is my code:

FirestoreReferenceManager.rootProjects.whereField(
            FirebaseKeys.ProjectDetails.inviteData,
            arrayContains: [
                FirebaseKeys.ProjectDetails.otherUserId: userID,
                FirebaseKeys.ProjectDetails.inviteStatus: FirebaseKeys.ProjectDetails.pending
            ])

If I don't have isInvoiceAccess data on firestore then above code is working fine but when I add that key on firestore then above query is not giving me any results and I can not add that key because it can be true or false.

If I use below code then I am getting the results:

FirestoreReferenceManager.rootProjects.whereField(
            FirebaseKeys.ProjectDetails.inviteData,
            arrayContains: [
                FirebaseKeys.ProjectDetails.otherUserId: userID,
                FirebaseKeys.ProjectDetails.inviteStatus: FirebaseKeys.ProjectDetails.pending,
                FirebaseKeys.ProjectDetails.invoiceAccess: true
            ])

So I want data from FireStore without adding isInvoiceAccess in my query.

CodePudding user response:

The arrayContains operation only checks whether there is exact match in the array, it cannot check for partial matches.

The common workaround for your use-case is to add an additional array field for each specific values/combination of values that you want to query on. So in your case, add a field invites_uid_and_status with just those two values for each, and then use that for this query.

  • Related