Greg Harner
Greg Harner

Reputation: 145

How can you create a Firestore compound query that returns all documents in an array of mapped objects?

I want to avoid returning documents from Firestore that I do not need. I have a collection of documents with the following properties:

membership {array}
  0 {map}
    contactGroupId: "1da9a73f89f8ca4c"
    contactGroupResourceName: "contactGroup/1da9a73f89f8ca4c"
  1 {map}
    contactGroupId: "1da9a73f89f8ca4c"
    contactGroupResourceName: "contactGroup/1da9a73f89f8ca4c"

I want to create a compound query that only returns documents if the contactGroupId is equal to 1da9a73f89f8ca4c. Something like ref.where(membership.{key}.contactGroupId, "==", "1da9a73f89f8ca4c"). Is this possible?

Upvotes: 0

Views: 25

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317750

It's not possible to filter documents using the contents of a map field inside an array. You will need to restructure your data if you want to perform this query. You have two options.

  1. Put all of the contactGroupId values into their own array field, and use an array-contains query to find them.
  2. Instead of using an array, use the contactGroupId value as the name of a map key that you can use in a query, like where(membership.1da9a73f89f8ca4c.present, '==', true). Your membership field would be structure like this:
membership (map)
  - 1da9a73f89f8ca4c
    - present (boolean)
    - contactGroupResourceName = "contactGroup/1da9a73f89f8ca4c"

Upvotes: 1

Related Questions