Jigar Patel
Jigar Patel

Reputation: 1568

Firebase querying data like multiple where in android

{
  "Events" : {
    "16" : { 
      "eventDate" : "2021-02-02",
      "eventId" : "16",
      "eventName" : "First Test",
      "eventTime" : "09:39:00"
    },
    "22" : {
      "eventDate" : "2021-02-02",
      "eventId" : "22",
      "eventName" : "Test22 Exam",
      "eventTime" : "09:39:00"
    },
    "26" : {
      "eventDate" : "2021-02-02",
      "eventId" : "26",
      "eventName" : "Webinar",
      "eventTime" : "09:39:00"
    }
  }
}

If I'm storing my Events like this, and I want to get the node where EventId is equal to 16 and 26. How can I do that?

The above is the child of issue, which is a child of root.

I Tried with equalTo but get only single record. I need to get 16 and 26 both.

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("Events").orderByChild("eventId").equalTo("16"); 

Upvotes: 0

Views: 64

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598668

Firebase doesn't support ORs in its queries. The only way to get multiple nodes based on the values of a specific property would be to use a startAt and endAt to select a range of values, but in your case that would also return the node with eventId equal to 22.

Given that the keys of the nodes are the same as your eventId properties, the fastest way I can think of to retrieve multiple nodes is by using a separate addListenerForSingleValueEvent() or get() call for each key:

reference.child("Events").child("16").get(...)
reference.child("Events").child("26").get(...)

Retrieving multiple child nodes like this is quite fast in Firebase, as all requests are sent over a single, existing socket connection. For more on this, see Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly.

Upvotes: 1

Related Questions