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

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

    I'm sure Firebase will come out with "string-contains" soon to capture any index[i] startAt in the string... But I’ve researched the webs and found this solution thought of by someone else set up your data like this

    state = {title:"Knitting"}
    ...
    const c = this.state.title.toLowerCase()
    
    var array = [];
    for (let i = 1; i < c.length + 1; i++) {
     array.push(c.substring(0, i));
    }
    
    firebase
    .firestore()
    .collection("clubs")
    .doc(documentId)
    .update({
     title: this.state.title,
     titleAsArray: array
    })
    

    query like this

    firebase
    .firestore()
    .collection("clubs")
    .where(
     "titleAsArray",
     "array-contains",
     this.state.userQuery.toLowerCase()
    )
    

提交回复
热议问题