用于获取用户朋友列表的Firestore模型
我已经使用了Firestore很长时间了。我现在正在构建一个应用程序,其中可扩展性和低成本很重要。 (我正在使用Flutter)
我的应用程序具有具有用户配置文件的用户,他们也可以添加朋友并与他们交谈(例如Instagram或Facebook)。 我有一个问题建立这个朋友系统。
我为这个朋友系统的模型目前看起来像这样:
- 用户收集。每个文档ID = auth的用户ID,这些文档包含数据,姓名,用户名,个人资料图片等
- 。每个文档ID = auth的用户ID。对于每个用户,这些文档包含一个名为:Friends的字段,这是一个与他的每个朋友用户ID的数组。 该模型看起来像:
Friends collection:
- uid:
- friends_list: [friend_uid1, friend_uid2, ...]
这就是我的“后端”外观。 现在,我想向用户展示他的朋友列表。我该怎么做?
我想要一个看起来像Instagram的列表,其中一个不错的UI显示了我的每个用户朋友配置文件图片,名称,最后一条消息等。
我找不到使用Firestore和Queries进行此操作的直截了当的方法。
假设我这样做:
- 将我所有的朋友用户ID在数组中获取。
- 使用.get()为每个文档获取所有用户文档。
这在Firestore中是不可行的,因为它将消除我拥有的所有查询能力(例如,只能对名称为“ X”的用户查询),我将不得不获取所有用户并在我的前端进行查询(或在云功能中,同一件事,而不是可扩展的)。
如果我这样做是这样做的:
- 使用对朋友集合中的所有用户的查询获取所有文档,friends_list包含我的用户ID。
- 仅从这些文档中保存DocumentID,然后手动获取所有朋友用户数据。
这带来了另一个问题。在Firestore中,没有获取所有字段的情况就无法获取文档,因此我使用的第一个查询实际上只能获得我的朋友的ID还可以获取文档ID +数据),这不好。
如果我喜欢这样做:
- 当您添加朋友时,而不是仅保存其UID,而是保存其UID +数据。
- 现在,我可以轻松地向用户展示他的朋友列表,并在前端进行一些查询。
这里的问题是,现在我的一个朋友更新了他的个人资料照片,我需要在他所有朋友的每个文档中更新它,这对于仅需进行一些简介更新而言,这是非常昂贵的。
还有一个问题的问题是要查看更多数据,也许我还有另一个带有聊天的集合,我想与朋友一起聊天的最后一条消息,现在我也必须获取聊天室,这很难查询我之前提到的所有问题随附的数据。
总之:我看不到在Firestore内部执行此类系统的好方法。似乎任何基本应用程序都应该具有的简单系统,但是我看不出如何以不做大量读取或读取更多数据(或敏感数据)的方式进行操作。
您将为这样的朋友系统做什么样的模型?
I have been using Firestore for a very long time. I am building an app now where scalability and keeping low costs is important. (I am using flutter)
My app has users, which have user profiles, also they can add friends and talk to them (like instagram or facebook).
I have a problem building this friends system.
My model for this friends system currently looks like this:
- Users collection. Each document id = user id from auth, those docs contain data like name, username, profile picture, etc.
- Friends collection. Each document id = user id from auth. For each user, those docs contain a field called: friends, which is an array with each of his friends user ids.
The model looks like:
Friends collection:
- uid:
- friends_list: [friend_uid1, friend_uid2, ...]
This is how my "backend" looks.
Now I want to show my user a list of his friends. How do I do that?
I want a list that looks like instagram, with a nice UI showing each of my user friend profile pic, name, last message, etc.
I can not find a straight forward way to do this with Firestore and queries.
Let's say I do it like this:
- Get all my friends user ids in an array.
- Get all their user documents using .get() for each document.
This is not doable in firestore cause it would eliminate all the querying power I have (such being able to query only for users with name "x"), I would have to fetch all users and do the query on my front-end (or in a cloud function, same thing, not scalable).
If I do this like:
- Get all document using a query for all users in the Friends collection, where friends_list contains my user id.
- Save from those documents only the documentID and fetch all the friends user data manually.
This comes with another problem. In Firestore there is no way of fetching a document without fetching all of its fields, so the first query which I use to get the ids only of my friends would actually give me their id + their friend list instead (cause when I query, it also gets the document id + the data), which is not good.
If I do it like:
- When you add a friend, instead of just saving its uid, save its uid + data.
- Now I can easily show my user his friends list nicely and do some querying on front-end.
The problem here is that now if one of my friends updates his profile photo, I need to update it in every document of all of his friends, which is very write expensive for just a little profile update.
There is also the problem of watching for more data, maybe I have another collection with Chats, and I want to show the last message of my chat with a friend, now I have to fetch the chat rooms too, which is more hard to query data that comes with all the problems that I mentioned before.
In conclusion: I don't see a good scalable way to do this kind of system within Firestore. It seems a simple system which any basic app should have, but I do not see how I can do it in a way that does not make lots of reads or read more data (or sensitive data) than it should.
What kind of model would you do for a friends system like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在减少NOSQL数据库的典型缺点。
在
像其他人评论一样,答案确实取决于您的应用程序。这是您必须做的评估。就像根据应用程序的用例,哪个选项更便宜。
例如,如果您选择第三个选项并存储朋友的用户数据,则需要填充列表。这意味着您必须实施措施以保持复制数据的完整性,每当用户更新其信息。
然后,您可以查看应用程序的使用情况,并确定用户更改信息的频率,而如果您不复制数据以找到最便宜的应用方法,则需要检索完整用户的频率。
You're decribing a quintessential drawback of NoSQL Databases.
A similar example is actually given in the Get to Know Cloud Firestore series.
Like others have commented, the answer really depends on your application. And this is the assessment you'll have to do. Like which of the options is cheaper depending on the use case of the app.
For example, if you go with your third option and store the friend's user data that you'll need to populate the list. This means you'll have to implement measures to keep the integrity of the copied data whenever the user updates their information.
You can then look at the usage of your app and determine how often users change their information vs how often you would need to retrieve full users if you don't copy the data to find the cheapest method for your application.