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

前端 未结 16 1647
抹茶落季
抹茶落季 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:31

    While Firebase does not explicitly support searching for a term within a string,

    Firebase does (now) support the following which will solve for your case and many others:

    As of August 2018 they support array-contains query. See: https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html

    You can now set all of your key terms into an array as a field then query for all documents that have an array that contains 'X'. You can use logical AND to make further comparisons for additional queries. (This is because firebase does not currently natively support compound queries for multiple array-contains queries so 'AND' sorting queries will have to be done on client end)

    Using arrays in this style will allow them to be optimized for concurrent writes which is nice! Haven't tested that it supports batch requests (docs don't say) but I'd wager it does since its an official solution.


    Usage:

    collection("collectionPath").
        where("searchTermsArray", "array-contains", "term").get()
    

提交回复
热议问题