Query over list in firebase

后端 未结 2 1635
走了就别回头了
走了就别回头了 2020-12-22 07:20

I\'ve the below custom model for firebase, I need to query for the cities with specific tags, I tried the below code but did not work :(

data class City(
            


        
相关标签:
2条回答
  • 2020-12-22 07:36

    My approach is to add some data to the searchable object on its onCreate trigger.

    // for structured data...
    // const myCity = { name: 'Baltimore', state: 'Maryland', country: 'USA' }
    // const names = [myCity.name, myCity.state, myCity.country]
    
    // or for strings, like names...
    const names = ["Benjamin Franklin", "Franklin Delano Roosevelt"]
    
    const searchArray = _.flatten(names.map(n => n.toLowerCase().split(' ')))
    const searchObject = searchArray.reduce((acc, n) => {
      acc[n] = true;
      return acc
    }, {})
    console.log(searchObject)
    // update the created object with searchObject
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js"></script>

    On the client (in JS), to do a search...

    // say you're searching for "Franklin"
    let searchTerm = "Franklin"
    
    const key = `searchObject.${searchTerm.toLowerCase()}`
    const query = db.collection("MyCollection").where(key, '==', true)
    
    0 讨论(0)
  • 2020-12-22 07:49

    Firestore queries only support equality and range operations. They don't support regular expressions. You will have to perform two queries and merge the result on the client.

    See:

    • How to perform compound queries with logical OR in Cloud Firestore?
    • Firestore Query Across Multiple Fields for Same Value
    0 讨论(0)
提交回复
热议问题