1

I am trying to search my database using a string, such as "A". I was just watching this Firebase tutorial Common SQL Queries converted for the Firebase Database - The Firebase Database For SQL Developers #4 and it explains that, in order to search the database for a string (in a certain location), you must use:

firebase.database().ref.child("child_name_here")
    .queryOrdered(byChild: "child_name_here")
    .queryStarting(atValue: "value_here_uppercase")
    .queryEnding(atValue: "value_here_uppercase\\uf8ff")

You must use two \\ in the ending value as an escape character in order to get one \.

When I try this with my Firebase database, it does not work. Here is my database:

{
    "Schools": {
        "randomUID": {
            "location" : "anyTown, anyState",
            "name" : "anyName"
        }
    }
}

Here is my query:

databaseReference.child("Schools")
    .queryOrdered(byChild: "name")
    .queryStarting(atValue: "A")
    .queryEnding(atValue: "A\\uf8ff") ...

When I go to print the snapshot from Firebase, I get back.

If I get rid of the ending .queryEnding(atValue: "A\\uf8ff"), the database returns all of the schools in the Schools node.

How can I search the Firebase database using a String?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • Firebase Database queries are case-sensitive. In your sample JSON there is no school name starting with `A`, only a school name starting with `a` (lowercase). – Frank van Puffelen Dec 10 '16 at 05:45
  • You may know this, but for clarity, that technique will only search for strings *starting* with the character 'A' and not any substrings. Firebase cannot search for substrings directly so other self-made methods must be used. – Jay Dec 10 '16 at 13:57

2 Answers2

0

queryStarting() and queryEnding() can be used for number. For example: you can get objects with someField varying from 3 to 10.

for searching string: you can search whole string using queryEqualToValue().

Chandrika
  • 194
  • 12
0

This shows all customers that match Wick. (It's not swift but may give you an idea)

// sample
let query = 'Wick' 
clientsRef.orderByChild('name')
.startAt(query)
.endAt(query + '\uf8ff')
.once('value', (snapshot) => {
     ....        
})
itstrevino
  • 116
  • 2