0

Ok, so I've been searching online and cannot find a solution for my issue. I've used the following queries, but still no luck:

        .whereField("Features.tagName", arrayContains: tagName)
        .whereField("Features", arrayContains: "tagName: \(tagName)")
        .whereField("Features", arrayContains: "tagName: \(tagName)")
        .whereField("Features", arrayContainsAny: [tagName])
        .whereField("Features", in: [tagName])
        .whereField("Features.tagName", arrayContainsAny: [tagName])

I thought I'd add these to show if I'm on the right track and to hopefully avoid any confusion or the event of recommending any of these queries as a solution.

I am pointing to the right collection as I've declared a global constant which I can call and have used for other queries/methods.

My database is structured as so:

Post - 
 PostID -
   username
   date
   features[]

However, it must be noted my features array is structured like this:

features: [
[0] tagName: "Blue",
[1] tagName: "Yellow"
]

I cannot seem to return anything. I am only querying by one tag. I've got a strong feeling it's because of tagName. But I've used interpolation or well tried to, but still not luck.

Anyone have a clue where I'm going wrong?

Asperi
  • 228,894
  • 20
  • 464
  • 690
SwiftUser
  • 555
  • 6
  • 17
  • Will the `features` array contain anything else beside `tagNames` ? – Emmanuel Sep 04 '20 at 22:44
  • 1
    @Emmanuel I know where the problem lies. I have used CodingKeys/identifiable to allow for users to select tags. And I have a key called `tagName` if I remove this prefix from the array prior to uploading, I can query the index. However, this is a challenge in it's self. However, to answer your question, `features` will ONLY contain `tagName`. – SwiftUser Sep 04 '20 at 22:48
  • What are you expecting with this code `.whereField("Features.tagName"`? More specifically what is `Features`? Your array is `features`? Also this *[0] tagName: "Blue",* really doesn't look like an regular firestore array - it looks like like an array of maps? – Jay Sep 05 '20 at 14:01
  • Also, I believe you cannot run a query on an object field within an array, but as mentioned is not exactly clear what that structure is composed of. Can you clarify? – Jay Sep 05 '20 at 14:39

1 Answers1

0

As mentioned in your comment the recommended way to do it is by removing the tagName since all of the array elements will have the key tagName and leaving the structure as:

features: [
  "Blue",
  "Yellow"
]

Then you can query it:

db.collection("Post")
  .whereField("features", arrayContains: "Blue")

However if changing the structure isn't possible you can query it as:

db.collection("Post")
  .whereField("features.tagName", isEqualTo: "Blue")
Emmanuel
  • 1,436
  • 1
  • 11
  • 17
  • Sadly, that ain't working. I've tried that solution before. – SwiftUser Sep 04 '20 at 23:39
  • did you performed your query by using the same casing as the declared on your collection? Remember that queries are case sensitive – Emmanuel Sep 04 '20 at 23:49
  • exactly the same, even added the semi-colon to see if that would work. – SwiftUser Sep 04 '20 at 23:50
  • where did you added the semi-colon? could you edit your post to show your collection/document structure and a precise example of how you're querying it? I'm asking because in the posted example your using `Features` with capital F and the structure description has `features` – Emmanuel Sep 04 '20 at 23:57
  • I hard coded a value on FireStore to test if I am pointing to the right data, I used the `arrayContains` sure enough I can retrieve the data. The features was a typo. It's literally the `tagName` that's killing me but with CodingKeys/Codable I NEED a tagName. I'll try and manipulate the array prior to uploading. I added the semi-colon after `.tagName` and added spacing, no spacing etc. – SwiftUser Sep 05 '20 at 00:03
  • Can you verify this `.whereField("features.tagName", isEqualTo: "Blue")` works? As of last year it wasn't possible. See [this post](https://stackoverflow.com/questions/54600915/firestore-how-to-query-data-from-a-map-of-an-array) – Jay Sep 05 '20 at 14:35
  • @SwiftUser Take a look at [this question and answer](https://stackoverflow.com/questions/62679230/query-a-map-containing-an-id-saved-in-cloud-firestore-with-dart-flutter/62679324#62679324) where a Firebaser states **Firestore doesn't let you query for values of maps inside an array** – Jay Sep 05 '20 at 14:47
  • @Jay I got the isEqual from the official [documentation](https://firebase.google.com/docs/firestore/solutions/arrays?hl=es#swift_2), also this [answer](https://stackoverflow.com/a/54987884/5495197) from Doug is using it (and it's newer that the one you linked) – Emmanuel Sep 05 '20 at 18:55
  • 1
    @Jay I looked at some order posts, and did what was recommended. Created a separate array that only takes string with no field name. – SwiftUser Sep 05 '20 at 19:13
  • @Emmanuel Yes, I looked over these articles prior to asking and still found no luck. Eventually, I just settled on the method I mentioned above – SwiftUser Sep 05 '20 at 19:13
  • @Emmanuel It's not the `isEqual` that's the issue and if you look closely, that's not what Doug is doing in his answer. He is working with a straight array and this is an array of mapped objects so it's quite different. Take another look – Jay Sep 05 '20 at 22:44
  • @Jay I looked at it again and to me it seems the same structure `vitamins` being the field, and inside there's a map `potassium` being the key and `true` the value, is there something I'm missing? – Emmanuel Sep 07 '20 at 21:50
  • In Doug's code, it's an array of values `0: some value, 1: another value` etc. In this question it's an array of maps `0: tagName: Yellow, 1: tagName: Green` and as I mentioned above you cannot query on an array of maps. – Jay Sep 08 '20 at 17:29
  • @Jay in the question it's using an array, but in the [answer](https://stackoverflow.com/a/54987884/5495197) provided even Doug mentioned that it's a map: `Consider instead using a map structure instead of an array` – Emmanuel Sep 08 '20 at 17:39
  • A map structure is NOT an array and is NOT an array of maps, which is what this question contains. It's a totally different beast. The map structure Doug suggested was more appropriate for that use case. It's not appropriate for this use case as the OP needs to query an array. – Jay Sep 08 '20 at 17:48
  • Thank you for the clarification Jay – Emmanuel Sep 08 '20 at 17:53