Rolanddev
Rolanddev

Reputation: 59

Firebase Cloud Firestore request

I have a MESSAGERECAP collection which includes a unique id for each message, the id of the receiver of the message, the id of the sender and the message itself. In my application, when the user clicks on a friend to chat with him, I want the chat activity to start with the list of messages they have both sent. I did this but obviously it does not give the desired result :

Query query = messageRef.orderBy("mssgId",Query.Direction.DESCENDING); 
// with messageRef a reference to the MESSAGERECAP collection

Here is an overview of my database

enter image description here

Upvotes: 0

Views: 161

Answers (1)

Rafael Lemos
Rafael Lemos

Reputation: 5819

You are getting the whole list because you are not filtering the data, just ordering it. If you check the Querying documentation for Firestore, also provided by @FrankVanPuffelen on the comments of your question, you can see that you have to use .where() to filter the data that you want to retrieve.

Also, as pointed out by @Jay in the comments, you can use Compound Queries to create a logical AND on your query to retrieve the data you want.

So if you do something like:

messageRef.where("senderId", "==", [currentUserId])
          .where("receiver_id", "==", [receiverUserId])
          .orderBy("mssgId",Query.Direction.DESCENDING)

When you execute this query you will get all the messages sent by the current user to the receiving user of the correponding id.

Upvotes: 1

Related Questions