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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With