Wildcard query on Firebase?

后端 未结 4 370
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 05:22

Is it possible to do wildcard queries on Firebase? For example:

https://foo.firebaseio.com/person.json?orderBy=\"name\"&equalTo=\"Lun*\"
4条回答
  •  时光取名叫无心
    2020-12-10 05:29

    No. But kinda.

    You cannot do a wildcard query, however, you can structure your data that will allow for this.

    For example, say we want to find matches for users whose name starts with Ler

    Here's our structure

    users
      uid_0
        name: "Leroy"
    

    Store the decomposed data in another node: Remember, disk space is cheap.

    decomposed
      uid_0
        L: true
        Le: true
        Ler: true
        Lero: true
        Leroy: true
    

    then perform a query on the decomposed node for the value of true for children equal to Ler

    ref.queryOrderedByChild("Ler").queryEqualToValue(true).observeEventType(.ChildAdded, 
         withBlock: { snapshot in
                        print(snapshot.key)
                    })
    

    And the snapshot.key will be uid_0

提交回复
热议问题