Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR clause in firebase java android

Does anyone know how I can do a common "OR" like in a where clause, in firebase?

I need to do that in the query, because I am sending the query to an adapter. So, i mean, I cannot add a listener and check one value and then another. I need to have the complete query pointing to that result in my query.

What I have is something like:

chat1:
   user1Id: "1"
   user2Id: "2"
   bothUsers: "1_2"

chat2:
   user1Id: "2"
   user2Id: "4"
   bothUsers: "2_4"

I need to get all the chats of the user whose id is "2". I am trying to do a query like:

userLogged = 2;
Query queryRef = firebase.orderByChild("user2Id").equalTo(userLogged);

However it will only get the chats when the user 2 is in the user2Id position. But in the example above, when the user 2 is in the user1Id (chat2) it won't get. And I need to get it. How can I do this?

like image 508
Rayane de Araújo Avatar asked Oct 19 '22 12:10

Rayane de Araújo


1 Answers

In a NoSQL database you will often have to model your data for the way your app needs it (see this article for a good explanation on NoSQL Data Modeling). So if you need a list of all chats for a specific user, store that list:

/userChats
  user1Id
    chat1: true
  user2Id
    chat1: true
    chat2: true
  user4Id
    chat2: true

This process is called denormalizing and it is covered in the article I linked above, in the Firebase documentation on structuring data and in this blog post.

like image 132
Frank van Puffelen Avatar answered Nov 03 '22 03:11

Frank van Puffelen