0

I faced many limitations with Firestore:

  1. cannot use multiple not-in
  2. You can't combine not-in with in, array-contains-any, or or in the same query
  3. every array filter can only check up to 10 values.

So I basically thought about changing data structure and use maps since they can be queried via field.key.

the key field of every document is not dynamic but each key of the map is.

this is an example of my structure:

// the collection
animals
  // the document (random id)
  dog
    // the field (a map value)
    food: {
      1: true
      2: true
      3: true
    }

The question the query should answer is: "Give me every record where a specific value does not exist AND (other constraints)"

I am considering changing stack for this project because of this. Any good advice is also welcome.

Michael Mammoliti
  • 1,517
  • 13
  • 11

1 Answers1

1

All Firestore queries are based on a reference to the document being present in the index(es) used for the query.

If a document doesn't have a value for a certain field, it won't be present in an index on that field.

This implies that you can't query for documents that don't have a value for the field you want to query on.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    In the past when I've run into these problems, I have made a point to explicitly define a field as an empty string or as `NULL` at the start so it's easy to query. When they get a defined value it's easy to update them. – Nick Felker Aug 28 '23 at 14:54
  • Yup, that's indeed the solution: give every field a default value. That unfortunately also means you'll have to backfill all existing documents if you add a field to your data model later on. – Frank van Puffelen Aug 28 '23 at 15:10