I am trying to create a relationship document in the firestore however I am struggling with making it sortable using the orderBy function. So far I am storing the following data:
I am now trying to figure out a method to enter the usernames of both users in such a way that I can sort the friends in the query. To get all friends without sorting I am using the following code:
firestore.collection("relationships").whereArrayContains("uids", myUID).whereEqualTo("status", "friends").limit(10).get()
Now I want to try to sort the friends, but getting all friends is not an option as that does not work with the pagination I am using so instead I am trying to use orderBy, but I can't think of a good way to store the names in the firestore.
as requested here is a screenshot of one of the dummy data entries I am using for testing:
Is there a way of querying my data so that the list of friends of my user is sorted by their name (so if I try to ask for 'person 6' here I would need to use name1 and vice versa). Even if I don't know ahead of time for each document if the user from whose perspective I am fetching the data will be the initiator or not? I believe this is not possible with the current schema, but maybe it is possible with a different one?
From the discussion we had in the comments it sounds like you want to order on one of two fields depending on a query in the same condition. That is not something that Firestore supports.
Typically this means that your data model doesn't match the requirements of your app.
In this case my immediate thought is that you should probably be using a subcollection for the users in the conversation, rather than an array field.
But it could also be that this use-case simply doesn't fit well with a NoSQL database (or with Firestore specifically). If you come from a background in relational databases, it might be good to read NoSQL data modeling techniques and watch Get to know Cloud Firestore.