数组或子集合用于存储用户上载的事件 [英] Array or Subcollection for storing events user uploaded

查看:58
本文介绍了数组或子集合用于存储用户上载的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有本机响应的事件应用.我只是想寻求一些建议,以更好地在Firestore中构建数据模型,从而获得更好的性能和可扩展性.我有2个集合事件"和用户".

I'm creating an events app with react native. I just wanted some advice on which would be the better more performant and scalable way to structure my data model in firestore. I have 2 collections Events and Users.

用户创建一个进入Event集合的事件.然后,在我的应用程序中,用户可以转到主页并查看来自event集合的事件列表.我还希望在应用程序中有第二个页面,即用户个人资料"页面,用户可以在其中查看自己的事件列表,进行更新和删除.我的问题是哪个更好:

A user creates an event which goes into the Event collection, In my app users can then go onto the main page and view a list of events from the events collection. I also want to have a second page in the app a "users profile" page where users can view a list of their own events, update and delete them. My question is which would be better:

  1. 将事件的密钥存储在users/user1中的数组中
  2. 基本上将一个重复的事件存储在一个名为users/user1中的events的子集合中

我认为选项1可能更好,只是将对文档的引用存储在数组中,所以我没有事件的重复项,并且如果用户必须更新事件,则只需写1次即可是根据事件集合中的实际事件进行的.

I feel that option 1, might be better just to store a reference to the doc in an array, So I don't have duplicates of the event and if the user has to update the event, only 1 write has to be made to the actual event in the events collections.

该事件将来可能会涉及更多字段,例如注释字段等,因此我觉得只需要选择选项1,我就不必继续做双重工作,尽管我可能不得不读两次书,例如读取用户/用户1->(然后数组)事件:[事件:{dockey}],然后使用该键在事件集合中获取实际的事件doc.

the event is likely to have more fields come onto it in the future, like a comments field etc, so I feel by just going with option 1 I dont have to keep doing double work, although I might have to read twice i.e read users/user1- > (then array) events:[event:{dockey}], then use that key to get the actual event doc in the events collection.

感谢您的任何反馈和建议

Thank you for any feedback and advice

推荐答案

当您需要在这两个选项之间进行选择时,没有简单的是非题.数据复制是快速读取的关键,不仅在Firebase Realtime Database或Cloud Firestore中,而且在一般情况下,都是如此.每当您将相同数据添加到其他位置时,您都在复制数据以提高读取速度.不幸的是,作为回报,您有一个更复杂的更新和更高的存储/内存使用率.但您需要注意,在Firebase中,在Firebase实时数据库中进行额外的调用并不昂贵.相对于您而言,最佳的复制数据量是额外的数据库调用量,具体取决于您的需求以及您是否愿意放弃单点定义思维方式",也可以称其为非常主观的".

There is no simple right or wrong answer when you need to choose between these two options. Data duplication is the key to faster reads, not just in Firebase Realtime Database or Cloud Firestore, but in general. Any time you add the same data to a different location, you're duplicating data in favor of faster read performance. Unfortunately in return, you have a more complex update and higher storage/memory usage. But you need to note that extra calls in the Firebase Realtime Database are not expensive, in Firestore are. How much duplication data versus extra database calls is optimal for you, depends on your needs and your willingness to let go of the "Single Point of Definition mindset", which can be also called very subjective.

在完成了一些Firebase项目之后,我发现如果我复制数据,我的阅读代码将大大简化.但是,当然,编写代码会同时变得更加复杂.两者之间的权衡决定了您的应用程序的最佳解决方案.

After finishing a few Firebase projects, I find that my reading code gets drastically simpler if I duplicate data. But of course the writing code gets more complex at the same time. It's a trade-off between these two and your needs that determines the optimal solution for your app.

也请看看我在此 帖子 ,其中我详细介绍了Firestore中的 collections maps arrays .

Please also take a look at my answer from this post where I have explained more about collections, maps and arrays in Firestore.

这篇关于数组或子集合用于存储用户上载的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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