How to search document(s) from a collection based on a Field in Firestore database?

后端 未结 2 1352
甜味超标
甜味超标 2020-12-22 06:09

I\'m working on a React Native application and I\'m fetching profiles from a firebase collection. And I want to add a search functionality where when I enter even the first

2条回答
  •  天命终不由人
    2020-12-22 06:46

    You should use a combination of orderBy(), startAt() and endAt(), see the documentation here: https://firebase.google.com/docs/firestore/query-data/order-limit-data?authuser=0

     var searchString = 'Sh'   //Example of value
     firebase
          .firestore()
          .collection('yourCollectioName')
          .orderBy('username')
          .startAt(searchString)
          .endAt(searchString + '\uf8ff')
          .get()
      .then(...)
    

    The character \uf8ff used in the query is after most regular characters in Unicode, therefore the query matches all values that start with searchString.

提交回复
热议问题