0

I'm building a Flutter app, and I'm trying to get documents in a users collection where 1)the boolean value 'finishedOnboarding' is true, and 2) the id 'userId' does not appear in the array 'stingrays'. My current query looks like this:

  Stream<List<User>> getUsers(
    String userId,
  ) {
    Query<Object?> query =
        userCollection.where('finishedOnboarding', isEqualTo: true).limit(10);

    return query
        .where('stingrays', whereNotIn: [userId])
        .limit(10)
        .snapshots()
        .map((snap) {
          return snap.docs.map((doc) => User.fromSnapshot(doc)).toList();
        });
  }

Running this, firebase tells me to create a compound index. Using the provided link, it generates the following:

compound index

Unfortunately, even with this index, the query does not work. It does not throw an error, and users are filtered based on finishedOnboarding, but I still am returned users where userId can be found in the stingrays array. I'm new to compound indexing so this is new to me. Any ideas on what's up with this query?

Edit: to give so more detail, heres an example situation. For context, im making a type of dating app, where ideally the stingrays array acts as a sort of memory to check if a user has swiped on that individual. In this case, i loaded the query, and a list of 4 users is returned, all with finishedOnboarding:true, and stingrays[] as empty. document screenshot before

However, after running through all 4 users and reloading the query, it still returns all 4 users, despite the user id now existing in the stingrays[] array. Screenshot afterward

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Ruder Buster
  • 219
  • 2
  • 14
  • 2
    Can you show an example of a document that is returned by the query, that shouldn't be? Ideally by printing the offending combination of field values from the `snapshots` that you get back. – Frank van Puffelen Jul 25 '22 at 01:26
  • @FrankvanPuffelen I added an edit to clarify the problem, does that make more sense? – Ruder Buster Jul 25 '22 at 04:49

1 Answers1

2

The whereNotIn cannot be used to query against a field of type array. A not-in query returns only documents where the given field does not match any of the comparison values that exist in the stingrays list. So this field should contain a single value and not an array, in order to be able to return results.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • If im understanding this correctly, youre saying where-not-in does not work for searching through an array? Is there a query tool that does this? – Ruder Buster Jul 25 '22 at 15:29
  • Yes, that's entirely correct. You might consider checking [this](https://stackoverflow.com/questions/59595938/firestore-possible-to-query-by-array-not-contains) and [this](https://stackoverflow.com/questions/52085868/firestore-get-documents-where-value-not-in-array). – Alex Mamo Jul 25 '22 at 15:47
  • I see. Is it possible to do it the other way then? If all stingray arrays contain certain id's already in them, can i query based on if a value exists in the array? – Ruder Buster Jul 25 '22 at 17:07
  • 1
    Yes, you can check for the existence of an item in an array, using `array-contains`. – Alex Mamo Jul 25 '22 at 18:54
  • Hey Ruder. Can I help you with other information? – Alex Mamo Jul 26 '22 at 07:44