Firebase Database Search Query

有些话、适合烂在心里 提交于 2020-01-05 04:33:14

问题


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?


回答1:


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().




回答2:


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) => {
     ....        
})


来源:https://stackoverflow.com/questions/41070244/firebase-database-search-query

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