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

后端 未结 2 1353
甜味超标
甜味超标 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:43

    An approach for full text search recommended by Firebase is to use Algolia https://firebase.google.com/docs/firestore/solutions/search

    Using Firestore’s queries with start at and end at with an ordered collection certainly work. If you have a need for full text search within your app elsewhere that requires spelling tolerance etc for example then moving to Algolia is worth considering instead of using compound firestore queries

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题