在Firebase/firestore中构造相关数据并管理更新的最佳方法? [英] Best way to structure related data in firebase/firestore and manage updates?

查看:51
本文介绍了在Firebase/firestore中构造相关数据并管理更新的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有 User Profile 对象.鉴于以下情况可能发生,在firestore中构建集合的最佳方法是什么?

If I have User and Profile objects. What is the best way to structure my collections in firestore given that the follow scenarios can take place?

  1. 用户只有一个个人资料
  2. 用户可以更新其个人资料
  3. 用户可以保存其他用户的个人资料
  4. 用户可以删除其保存的个人资料
  5. 同一个人资料不能保存两次

如果用户"和个人资料"是单独的集合,那么存储已保存的个人资料的最佳方法是什么?

If Users and Profiles are separate collections, what is the best way to store saved profiles?

想到的一种方法是,每个用户都有一个名为 SavedProfiles 的子集合.每个文档的ID是配置文件的ID.每个保存的个人资料仅包含对其所属个人资料的用户的引用.

One way that came to mind was that each user has a sub collection called SavedProfiles. The id of each document is the id of the profile. Each saved Profile only contains a reference to the user who's profile it belongs to.

另一种选择是执行相同的操作,但存储每个已保存配置文件的整个配置文件.

The other option was to do the same thing but store the whole profile of each saved profile.

第一种方法的好处是,当用户更新自己的配置文件时,无需更新任何已保存的配置文件,因为它只是存储的参考.但是,尝试读取用户保存的配置文件可能需要两次读取操作(这将是很常见的),其中一项操作是获取所有参考,然后使用这些参考查询所有配置文件(如果可能的话?).这似乎很昂贵.

The benefits of the first approach is that when a user updates their own profile there's no need to update any of the their profiles that have already been saved as it's only the reference that is stored. However, attempting to read a user's saved profiles may require two read operations (which will be quite often), one to get all the references then querying for all the profiles with those reference (if that's even possible???). This seems quite expensive.

第二种方法似乎是正确的方法,因为它解决了读取所有保存的配置文件的问题.但是更新多个保存的配置文件似乎是一个问题,因为每个用户的保存的配置文件可能是唯一的.我知道可以进行批量更新,但是是否有必要向数据库中的每个用户查询其保存的配置文件,并检查是否存在该更新的配置文件(如果存在)?我不太确定该走哪条路.我不太习惯NoSQL数据结构,自从我使用了子集合以来,似乎已经做错了事,因为建议将所有内容尽量保持非规范化,因此请让我知道该结构是否完整db也是错误的,这也是很有可能的...

The second approach seems like the right way to go as it solves the problem of reading all the saved profiles. But updating multiple saved profiles seems like an issue as each user's saved profiles may be unique. I understand that it's possible to do a batch update but will it be necessary to query each user in the db for their saved profiles and check if that updated profile exists, if so update it? I'm not too sure which way to go. I'm not super used to NoSQL data structures and it already seems like I've done something wrong since I've used a sub collection since it's advised to keep everything as denormalized as possible so please let me know if the structure to my whole db is wrong too, which is also quite possible...

请提供一些有关如何获取和更新配置文件/保存的配置文件的示例.

Please provide some examples of how to get and update profiles/saved profiles.

谢谢.

推荐答案

欢迎来到设计NoSQL数据库的难题.这里没有正确或错误的答案.这是最适合您的东西.

Welcome to the conundrum that is designing a NoSQL database. There is no right or wrong answer, here. It's whatever works best for you.

您已经确定,使用第二个选项将使查询变得容易得多.您可以轻松创建一个Cloud Function,以更新所有已修改的配置文件.

As you have identified, querying will be much easier with your second option. You can easily create a Cloud Function which updates any profiles which have been modified.

您的第一个选项将需要多次获取数据库.这实际上取决于您计划如何扩展规模以及您希望应用运行的速度.

Your first option will require multiple gets to the database. It really depends how you plan to scale this and how quick you want your app to run.

在获取所有数据的同时,选项1将带来缓慢的用户体验.选项2可以提供更快的用户体验,但是需要您的Cloud Function来更新每个保存的配置文件.但是,这是一项后台任务,因此不需要花费几秒钟.

Option 1 will be a slow user experience, while all of the data is fetched. Option 2 will be a much faster user experience, but will requre your Cloud Function to update every saved profile. However, this is a background task so wouldn't matter if it takes a few seconds.

这篇关于在Firebase/firestore中构造相关数据并管理更新的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆