1

I am working on this project where I have to put data in maps inside in array for example: [{Rate: 1.3, Title: "Test",...}] What I would like to do to find documents which has maps inside their array with rating 1.3 and above without fetching all arrays and filtering it internally

I have tried to this

      Firestore.instance
          .collection("profiles")
          .where("questions", arrayContains: {"Rate": 1.3})
          .getDocuments(source: Source.serverAndCache)

this code actually requires me to put all map data so it could return true otherwise it will return false. What I want is just to filter data based on some of the map data

Mousa Alribi
  • 300
  • 3
  • 11

2 Answers2

3

The arrayContains operator checks for the complete element's presence in the array. So if your array contains an element {Rate: 1.3, Title: "Test"}, you need to:

.where("questions", arrayContains: {Rate: 1.3, Title: "Test"})

If you want to be able to test only for Rate, create an additional array Rates where you keep just those values. Then you can query with:

.where("rates", arrayContains: 1.3)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you Frank! The idea is I want to gather data in a map and still search for it. I have a lot of inputs and its all in maps dividing them into arrays will make things a bit more complicated. Do you have another suggestion regarding that? – Mousa Alribi Aug 27 '19 at 07:50
0

Thanks to SO's 50 point limitation, I can't add a comment to the last solution from 2019.

This does not work today:

.where("questions", arrayContains: {Rate: 1.3, Title: "Test"})

This will result in an error where Rate is undefined

My Firestore collection 'contracts' looks like this: Firestore Screenshot

Visual Code Code Screenshot

I have tried many other solutions in other threads (whereIn, arrayContainsAny), but I cant figure out how to query an array that holds a series of maps (0, 1, 2 etc).

Joost
  • 13
  • 3
  • Please put up the code, rather than screenshot of the code – Pathik Patel Apr 07 '22 at 07:50
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31476798) – David Apr 12 '22 at 20:39