0

Let's say I have this database:

J-DSKFJASKDFJASKDJ:{
   username: "Burned"
},
J-KASDJFAKSDJFKDSJ:{
   username: "BurnedFirebase"
},
J-DFJKADSJFAKSDJFK:{
   username: "FirbaseFilteringIsVeryLackingThereforItSucks"
}

If I use this:

Query q = database.orderByChild("username").equalTo("Burned");

It will actually return only "Burned", although there is a BurnedFirebase in the database. That's because equalTo() doesn't work as a method such as contains().

Is there a method similar to contains()? If not, is there a workaround?

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • 1
    you can combine the startAt() and endAt() methods to limit the results to a specified range of values. – Umar Ata Dec 02 '16 at 14:01
  • @UmarAta thanks for that link. But Frank doesn't provide an example and I didn't understand how I could do that. I think to help future visitors, you should post an example before we close it as a duplicate. – Ali Bdeir Dec 02 '16 at 14:17
  • @UmarAta do I just use `startAt("myQueryText").endAt("myQueryText")`? – Ali Bdeir Dec 02 '16 at 14:23

1 Answers1

2

If your search is always at the beginning of the word (you're not searching for ned and expecting Burned to come up), you can use a high unicode character:

query.startAt("Burned").endAt("Burned\uf8ff")

From: https://firebase.google.com/docs/database/admin/retrieve-data#range-queries

...dinosaursRef.orderByKey().startAt("b").endAt("b\uf8ff")...

The \uf8ff character used in the query above is 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 a b.

Mathew Berg
  • 28,625
  • 11
  • 69
  • 90