Firestore search array contains for multiple values

前端 未结 5 1083
北海茫月
北海茫月 2020-11-30 04:52

If I have a simple collection such as:

Fruits:
  Banana:
   title: \"Banana\"
   vitamins: [\"potassium\",\"B6\",\"C\"]
  Apple:
   title: \"Apple\"
   vitam         


        
5条回答
  •  清歌不尽
    2020-11-30 05:29

    There's no way to do multiple array-contains queries in Firestore. You can, however, combine as many where conditions as practically possible. Therefore, consider this:

    [Fruits]
        
            title: "Banana"
            vitamins:
                potassium: true
                b6: true
                c: true
    
    Firestore.firestore().collection("Fruits").whereField("vitamins.potassium", isEqualTo: true).whereField("vitamins.b6", isEqualTo: true)
    

    However, this still doesn't allow a clean OR search. To query for fruits with vitamin-x or vitamin-y, you'd have to get more creative. Furthermore, this approach should not be used if there are a lot of possible values and they need to be combined with compound queries. That's because Firestore does not permit more than 200 composite indices and each composite index would need to specify the exact vitamin. Which means you'd have to create an individual composite index for vitamins.b6, vitamins.potassium, etc. and you only have 200 total.

提交回复
热议问题