2

I am creating a simple chat application and I need a way to retrieve all of a user's messages. I have the following data structure:

Messages{
-LmPsSIlmS8c6ph5UoOi {
    formattedTime: "05:43 PM"
    message: "Hey there"
    receiverId: "GvaD3JOgp5Ro6TWZCa3I7RvK7682"
    senderId: "3BPkGaQq62gnQCbO5UHFIw4XVps2"
}

I would like to make a query that selects all the messeges where senderId = user.getUid() OR receiverId = user.getUid()

I've seen posts all around the internet that firebase doesn't do this but there has to be a way around it because I doubt you can ever build a large application without OR queries.

I've tried:

 myRef.child("Messages").orderByChild("senderId")
.startAt(user.getId()).endAt(user.getId()).orderByChild("receiverId")
.startAt(user.getId()).endAt(user.getId())
            .addValueEventListener(
                    new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {




                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

This hasn't worked, though I've searched far and wide for a simple solution without getting any answers. Kindly help

Patrice Andala
  • 184
  • 1
  • 14

2 Answers2

2

Unfortunatelly, logical OR queries aren't supported by Firebase realtime database. Such queries even aren't supported by Firestore, which is the next generation of the Firebase database, it's because of its NoSQL nature. You should create two different queries instead. See also this SO answer, which contains a bunch related references.

Valeriy Katkov
  • 33,616
  • 20
  • 100
  • 123
0

There are some query limitations for firebase realtime database obviously for performance reasons. Till now it is not yet possible to use logical OR for queries in firebase. The official firebase documentation doesn't mention anything that supports OR queries anyway.

However, Cloud Firestore, which is Firebase's newest database clearly states in its doc about the limitation. FYI, though not exactly the same, Cloud Firestore stores data in almost similar way of Firebase Realtime Database, but with additional features and new technology.

Cloud Firestore does not support the Logical OR queries. In this case, you should create a separate query for each OR conditions and merge the query results in your app.

As it is a limitation, you can't do it the straighter way. However, the apparent solution is already mentioned in the Cloud Firestore doc. In your case, I am assuming that you are using a list for collecting the messages. Or, whatever data structure you are using for storing messages, appending new data is not too hard, I guess.

So, create separate queries. Once make a query that selects all the messeges where senderId = user.getUid() and store them to the data structure. Then, again make a query that selects all the messeges where receiverId = user.getUid() and append the data structure with new data from the second query. I hope you understand the intention.

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • Seems a bit wasteful in terms of code efficiency but it worked – Patrice Andala Aug 18 '19 at 07:22
  • 1
    Considering the facilities firebase provide, we can ignore this little extra work. But sad news is, they are not gonna implement this feature in near future, as it is a huge performance blow for its NoSQL nature. – UkFLSUI Aug 18 '19 at 07:27
  • Is it a similar process in carrying out AND queries? – Patrice Andala Aug 18 '19 at 13:30
  • The documentation you quote is for Cloud Firestore, while the question is about the Firebase Realtime Database. While neither database supports OR queries, you can't just apply the documentation from one to a question about the other. – Frank van Puffelen Aug 18 '19 at 14:27
  • @FrankvanPuffelen Sorry, the functionality was almost same and the doc of realtime database doesn't state that clearly but that's the case actually, so I tried to write with firestore. Now, I've edited. – UkFLSUI Aug 18 '19 at 15:49