0

Is it possible in Firestore to run a query where you get all the documents that have a field not equal to any elements of a string Array?

The idea is that I have a set of cards (such as on Tinder) and whenever a new card is loaded I send the card uid to the database as "already swiped" for that uid, When I want then to upload more cards I want to exclude the cards already swiped by the user, so what do I do? I query all the card uids that the user already swiped and exclude them for the query.

The only similar query I've seen is this one

whereField("capital", isNotEqualTo: false)

but it compares it only with one value.

the other similar one is:

statesRef.whereField("capital", notIn: ["Tallahassee", "Atlanta"])

but it takes 10 parameters maximum, so if potentially the users have swiped more than 10 cards it becomes unuseful

StackGU
  • 868
  • 9
  • 22
  • It sounds like you have a value and want to return all of the documents it's not in? If so then [Query with not-in](https://firebase.google.com/docs/firestore/query-data/queries#not-in) will do that. So `statesRef.whereField("capital", notIn: ["Tallahassee", "Atlanta"])` will return the states that do not have capitals of Tallahassee or Atlanta. If the string array is less than 10 elements, you're all set. However, `notIn` has a limit of 10 elements. In that case, you would do this for each string (or set of 10 strings) and combine the returned results. – Jay Apr 04 '21 at 14:37
  • Thanks for the comment, the idea is that I have a set of cards (such as on Tinder) and whenever a new card is loaded I send the card uid to the database as "already swiped", When I want then to upload more cards I want to exclude the cards already swiped by the user, so what do I do? I query all the card uids that the user already swiped and exclude them for the query. I think your approach works but it's unpractical because the limit of 10 is unrealistic when the uid swiped become thousands – StackGU Apr 04 '21 at 14:45
  • Gotcha. My comment above is a solution to the question as it stands. However, your comment adds additional, and valuable information that makes what I am suggesting possibly impractical. Your comment should really be part of the question and the objective needs to be more clear; describing the use case would really help us to help you determine an answer. Can you please update the question with more info? – Jay Apr 04 '21 at 16:59
  • Of course, I will do it right now :) – StackGU Apr 04 '21 at 18:09
  • Why don't you add a field to each card in Firestore that indicates it's status? isSwiped: true or false. When a user uses the card, set it to true. When you want unused cards, query for all that are false. – Jay Apr 05 '21 at 17:30
  • This approach doesn't work because each card can be swiped by all the users except for the current user id. The approach you suggested can make a card swipeable only once... – StackGU Apr 05 '21 at 17:40
  • 2
    I think my answer to [this question](https://stackoverflow.com/questions/57812558/firestore-how-to-get-around-array-does-not-contain-queries/57835092#57835092) may apply as a answer here as well - the goal in the question seems similar. – Jay Apr 05 '21 at 18:32
  • thank you VERY much for your answer I read it all over and it seems an interesting approach, now I'm trying to implement an order based on the timestamp of the creation of the card, it may not be the best approach but it seems easier and moreover I know firsthand which are the timestamps of the cards – StackGU Apr 05 '21 at 21:35

0 Answers0