Search code examples
firebasedictionarygoogle-cloud-firestoredocument

Query a Firestore document where a map key does not exist


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.


Solution

  • 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.