I would like to create a search feature for my apps by using firebase real time database, which similar to facebook search.
I did some research and acknowledge that firebase real time is not able to search with "text contain". But i believe there must be some method to achieve it, like combine startAt or endAt.
I need some advise the best practice to do it.
The DB structure will look like:
Restaurant
-->RandomID
---->name: 'Good Restaurant'
---->address:
---->desc:
-->RandomID
---->name: 'Dixon Lee Restaurant'
---->address:
---->desc:
-->RandomID
---->name: 'MCD'
---->address:
---->desc:
Edited I not sure how the startAt and endAt work. Is it supposes to return 3 and above if i use startAt(3)? What if i use startAt with string? Like below problem i ecnounter:
firebase.database().ref('/restaurants').orderByChild('name').startAt('Good').on('value',snap=>{
console.log(snap.val())
})
The above query will return:

I am thinking to achieve "text contains" with startAt/endAt, which will find specific value of field that begin/end with a keyword. Not sure is this the usage for startAt/endAt.
You seem to be confusing startAt with a function that would accepts strings that start with a certain value. That's simply not how Firebase filtering work.
When you do ref('/restaurants').orderByChild('name').startAt('Good'), it does three things:
restaurants on their name value.Good (or anything lexicographically after Good)So you're getting the child nodes with name Good Restaurant and MCD.
You can use startAt and endAt to return a range of child nodes, i.e. all the ones starting with Good. To do this you create a range starting at Good and ending at Good\uF8FF:
ref('/restaurants').orderByChild('name').startAt('Good').endAt('Good\uF7FF')
Since \uF8FF is the last known Unicode character, this query returns items starting with e.g. Good, Goodie, Goodness, but not Gooey (since Gooe is after Good\uF7FF).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With