Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard query on Firebase?

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

https://foo.firebaseio.com/person.json?orderBy="name"&equalTo="Lun*"
like image 521
MoreScratch Avatar asked Apr 01 '16 20:04

MoreScratch


People also ask

Can we use SQL queries in firebase?

FireSQL is a library built on top of the official Firebase SDK that allows you to query Cloud Firestore using SQL syntax. It's smart enough to issue the minimum amount of queries necessary to the Firestore servers in order to get the data that you request.

What is a firebase query?

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. The Query class (and its subclass, DatabaseReference ) are used for reading data. Listeners are attached, and they will be triggered when the corresponding data changes.

How do you filter data in firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .


2 Answers

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.

like image 72
Mihai Fratu Avatar answered Sep 28 '22 01:09

Mihai Fratu


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

like image 27
Jay Avatar answered Sep 28 '22 03:09

Jay