senty
senty

Reputation: 12847

Retrieving Multi-Query with Firebase

I am trying to create a conversation-room node for my users. I have the node that has from and to users' ids. Before creating a new room, I need to check both cases, where 'from' is host and to is opponent' & 'to' is host and 'from' is opponent.

The node looks like:

 - Rooms
    - "id1"
      - from: 2
      - to: 1

What I want to do is basically:

Select all from rooms where from = 1 and to = 2 OR from = 2 and to = 1

Is there a way to check both cases in a single query? I got confused understanding the nature of complex querying data with Firebase.

Upvotes: 1

Views: 586

Answers (1)

Ymmanuel
Ymmanuel

Reputation: 2533

There is no direct way to query by two different children in firebase the only way to query by children would be:

 myRefToRooms.orderByChild('from').equalTo($user_id)

That said you can create in each room a query child that is created with the two uid:

 rooms:{
  $room_id:{
    from:$user_from,
    to:$user_to,
    query:$userfrom_$userto
  }
 }

This way you can get all the values that match both criteria

myRefToRooms.orderByChild('query').equalTo($user1_$user2)

Also you can use the startAt() and the endAt() methods in the query to not only find an exact match but all the rooms with the same "from user" or all the rooms with the same "to user"

If you don't like this approach you would need to structure your database in a different way to create simple queries

Upvotes: 2

Related Questions