0

I use orderByKey for requesting data by keys from Firebase. I can get several objects if keys are similar — start or end with the same string.

But how can I retrieve totally different keys with one request to Firebase, e.g. "n:1-2-3" and "n:2-3-4"? I use equalTo, but can I specify more than one key there?

I know how to do it with different requests — one request per one key, but it's not optimal.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Evgeny
  • 631
  • 6
  • 17
  • show a database sample example and explain more please, oh I think I understood you want more than one condition and no thats not possible. The most you can do is orderbykey().equalTo(key_here) – Peter Haddad Dec 17 '17 at 13:27
  • Ideologically, why can I request "animal_dog" and "animal_horse" with one request, but not "a123" and "a234"? – Evgeny Dec 17 '17 at 13:32
  • you can, u need to use startAt() – Peter Haddad Dec 17 '17 at 13:34
  • @PeterHaddad If I will use startAt("a") it will return me 1'000'000 results. I want to request only two "a123" and "a234". – Evgeny Dec 17 '17 at 13:37

1 Answers1

1

Currently firebase only supports one condition. Explained more in the below text:-

If lets say you have two keys that are a123 and a234 then to retrieve you can do this:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
ref.orderByKey().startAt("a").limitToLast(10).addValueEventListener(..){..}

The limitToLast() method is used to set a maximum number of children to be synced for a given callback

You can use limittolast or limittofirst, to limit the results that are obtained from the database. Also currently firebase only supports one condition. So you cannot have more than one orderbykey or orderbychild or the two together..

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Problem is they are not last, I don't know order. E.g. "a123" has position 3453, "a234" has position 4357623. I can't use startAt() in my case. My initial question was: can I read totally different keys, e.g. "qwe" and "rty". – Evgeny Dec 17 '17 at 13:43
  • as in my answer I said, logically to read two different keys you do multiple conditions but since you can only do one condition then no you cannot, the max you can do is `orderBykey().equalto(onekeyhere)` – Peter Haddad Dec 17 '17 at 13:44
  • I can accept the answer if you put info like "currently firebase only supports one condition" at the beginning of the text. It will help other guys faster. Thanks. – Evgeny Dec 17 '17 at 13:59