Wildcard query on Firebase?

≡放荡痞女 提交于 2020-01-09 10:22:00

问题


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

https://foo.firebaseio.com/person.json?orderBy="name"&equalTo="Lun*"

回答1:


I know it's been a while but I thought that others might be interested. You can "fake" a wildcard search for things like foo* (so basically you can search for values beginning with a specified string).

For iOS & Swift it would look like this:

dbReference.child("person").queryOrdered(byChild: "name").queryStarting(atValue: "foo").queryEnding(atValue: "foo\u{f8ff}").observe(.childAdded) { (snapshot: FIRDataSnapshot) in
    print("\(snapshot.key) - \(String(describing: snapshot.value))")
}

What this does is using a start and end values for name property where the end key is equal to the start + a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with foo.




回答2:


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




回答3:


You can do something like this.

Make sure you order your search field alphabetically.

Then you search for all names (starting at Lun and ending at Luo) where the last letter 'o' is calculated with the initial last letter 'n' + 1.

I guess you see the general idea here.

So it should return anything between 'Lun*' and stop at the first entry of 'Luo*'

https://foo.firebaseio.com/person.json?orderBy="name"&startAt="Lun"&endAt="Luo"



回答4:


Since Firebase doesn't support wildcard searching I decided to go with Apigee BaaS for our company.



来源:https://stackoverflow.com/questions/36365157/wildcard-query-on-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!