3

How would I query into Firestore to get all documents where an array contains a certain string?

For example my structure is...

/locations
    /{locationId}
          name:"Name of Location"
          description:"Description of Location"
          /lists
               0:"bigList"
               1:"smallList"
               2:"mediumList"
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
pjmanning
  • 1,241
  • 1
  • 20
  • 48
  • What exactly are you hoping to get from that structure, for example? – Doug Stevenson Dec 10 '17 at 22:19
  • @DougStevenson a specific string in the array. I noticed firestore has a `whereField(path: FieldPath, isEqualTo: Any)` I want display results based on if inside the lists array there exists a specific string – pjmanning Dec 10 '17 at 22:23
  • This query is currently not possible with your current structure. But if you change the values under lists to be keys, it **is** possible. See https://stackoverflow.com/questions/46849222/firestore-query-by-item-in-array-of-document. – Frank van Puffelen Dec 10 '17 at 22:41
  • @FrankvanPuffelen so if under lists it was `"bigList": true` ? How would the query look then? – pjmanning Dec 10 '17 at 22:46
  • 1
    Something along the line of `let query = locationsRef.whereField("lists.bigList", isEqualTo: true)`. You can ready more about this structure and the queries here: https://firebase.google.com/docs/firestore/solutions/arrays#top_of_page – Frank van Puffelen Dec 10 '17 at 23:06
  • Ahh yes the . notation couldn't figure out how to drill down to that. Thanks! – pjmanning Dec 10 '17 at 23:07

0 Answers0