Google Firestore: Query on substring of a property value (text search)

前端 未结 16 1557
抹茶落季
抹茶落季 2020-11-22 12:08

I am looking to add a simple search field, would like to use something like

collectionRef.where(\'name\', \'contains\', \'searchTerm\')

I tried

16条回答
  •  醉梦人生
    2020-11-22 12:40

    I just had this problem and came up with a pretty simple solution.

    String search = "ca";
    Firestore.instance.collection("categories").orderBy("name").where("name",isGreaterThanOrEqualTo: search).where("name",isLessThanOrEqualTo: search+"z")
    

    The isGreaterThanOrEqualTo lets us filter out the beginning of our search and by adding a "z" to the end of the isLessThanOrEqualTo we cap our search to not roll over to the next documents.

提交回复
热议问题