What is the difference between “equalTo” and “startAt & endAt” in firebase, and when should I use “equalTo” or “startAt and endAt”?
When I’m trying to find a record by email or phone number, I’ve been using the two codes below, sometimes the first code works fine and sometimes it doesn’t, and the second code does the same.
What is the difference between the following code and when you should use “equalTo” or “startAt and endAt”?
ref.orderByChild("email")
.equalTo(str)
and
ref.orderByChild("email")
.startAt(str)
.endAt(str+"\\uf8ff")
Solution
ref.orderByChild("email").equalTo(str)
The above means that the email must be equal to the value of str
. This is the same as saying WHERE email= '[email protected]'
ref.orderByChild("email").startAt(str).endAt(str+"\\uf8ff")
It’s like saying WHERE email like ca%
This will return all emails that start with “ca"
public Query startAt (String value)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.
public Query endAt (String value)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.
The
\uf8ff
is simply the last character in unicode, so acts as an end guard.
Check this query: