Firestore: Multiple 'array-contains'

后端 未结 2 1638
难免孤独
难免孤独 2020-12-01 11:15

I am trying to filter data with multiple where() methods using the array-contains operator, however I am having the following error:



        
相关标签:
2条回答
  • 2020-12-01 11:36

    One solution would be to store the 'tags' in an Object as its property names.

    node_sku = {
      "sku1": true,
      "sku2": true,
      ...
      "skun": true
    }
    

    Then you can create the AND WHERE clauses like this:

    list.forEach( (val) => { 
      ref = ref.where(`node_sku.${val}` , '==' , true);
    });
    

    This approach is an answer to the question "without having to query them individually". However, as soon as you want ordering and pagination, you will run into another invisible brick wall of compound indices.

    0 讨论(0)
  • 2020-12-01 11:45

    As you've found you can only have a single array-contains operation in a query. The idea of allowing multiple is currently not supported, but could be valid. So I recommend you file a feature request.

    The only solution I can think of for your use-case right now, is to also include the combinations. So if you want to allow testing for the existing of two SKUs, you explode the array to also contain each combination (merging keys in lexicographical order):

    product1 
      - node_sku ['sku1', 'sku2', 'sku3', 'sku1_sku2', 'sku1_sku3', 'sku2_sku3' ]
    

    For two that might still be reasonable, but the combinatory explosion will make it infeasible beyond that.

    0 讨论(0)
提交回复
热议问题