最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Is there a way to store relationship data while still being able to sort on the username of the friends in the firesto

programmeradmin0浏览0评论

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:

  • uids: a list containing both UIDs in the relationships
  • compositeKey: a composite key of both UIDs which were sorted alphabetically before
  • initiator: the UID of the person that initiated the relationship
  • status: the status of the relationship (like 'friends', 'blocked' or 'requested')

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?

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:

  • uids: a list containing both UIDs in the relationships
  • compositeKey: a composite key of both UIDs which were sorted alphabetically before
  • initiator: the UID of the person that initiated the relationship
  • status: the status of the relationship (like 'friends', 'blocked' or 'requested')

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?

Share Improve this question edited Feb 7 at 5:18 Para1life asked Feb 6 at 19:18 Para1lifePara1life 251 silver badge7 bronze badges 7
  • 1 I have a feeling you're using arrays where you should be using a subcollection, but it's hard to be sure. Can you edit your question to include a screenshot from the Firestore console of one (or more) of these documents that you're querying? – Frank van Puffelen Commented Feb 6 at 19:24
  • I've added the picture, as I already said there is no name of the users yet. If it is impossible to sort on the name of the users friends with this type of structure that is fine too! If that is the case I'll just use 2 documents per relationship instead which should work if I am correct. I just wanted to make sure I wasn't missing anything with this approach. – Para1life Commented Feb 7 at 2:46
  • 1 I'm still thoroughly confused. What do you want to sort on from that screenshot? And: if the data is not in the document, you won't be able to sort on it. --- If you come from a background in relational databases, you may benefit from watching: Get to know Cloud Firestore. Querying in a NoSQL database is quite different from in SQL. – Frank van Puffelen Commented Feb 7 at 4:37
  • I think I've been doing a pretty poor job explaining so far so I'll try to be as detailed as possible. I currently don't have the names of the two users stored, but before I had them both stored under 'name1' and 'name2' with name1 being the initiator. I now want to see if it is possible to get all friends of a user sorted by the name of the friend. The problem I am running into is that I don't know if the user will be name1 or name2 ahead of time as that is based on who is the initiator of the relationship. I've also attached a picture. The question doesn't fit so I continue under the picture – Para1life Commented Feb 7 at 5:19
  • 1 There is no way to sort on an OR of two fields. – Frank van Puffelen Commented Feb 7 at 15:27
 |  Show 2 more comments

1 Answer 1

Reset to default 1

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论