使用“包含数组"查询Cloud Firestore社交媒体结构 [英] Using "array-contains" Query for Cloud Firestore Social Media Structure

查看:68
本文介绍了使用“包含数组"查询Cloud Firestore社交媒体结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据结构,其中包含一个称为投票"的集合. 民意调查"有几个文档,这些文档具有随机生成的ID.在这些文档中,还有一个名为答案"的附加收集集.用户对这些民意调查投票,并将所有投票都写入答案"子集合.我在答案"节点上使用.runTransaction()方法,其想法是该子集合(对于任何给定的民意测验)都会不断地被用户更新和写入.

I have a data structure that consists of a collection, called "Polls." "Polls" has several documents that have randomly generated ID's. Within those documents, there is an additional collection set called "answers." Users vote on these polls, with the votes all written to the "answers" subcollection. I use the .runTransaction() method on the "answers" node, with the idea that this subscollection (for any given poll) is constantly being updated and written to by users.

我一直在阅读有关 Firestore的社交媒体结构.但是,我最近遇到了Firestore的一项新功能,即"array_contains"查询选项.

I have been reading about social media structure for Firestore. However, I recently came across a new feature for Firestore, the "array_contains" query option.

尽管上面的帖子参考文献讨论了社交媒体结构的后续"提要,但我的想法有所不同.我设想用户向我的主投票节点进行写入(投票),因此创建另一个后续"节点,并使用户写入该节点以更新投票数(使用云功能)似乎效率极低,因为我不得不不断地进行复制从计票的主节点开始.

While the post references above discusses a "following" feed for social media structure, I had a different idea in mind. I envision users writing (voting) to my main poll node, therefore creating another "following" node and also having users write to this node to update poll vote counts (using a cloud function) seems horribly inefficient since I would have to constantly be copying from the main node, where votes are being counted.

"array_contains"查询是否会成为社交媒体结构可伸缩性的另一个实用选择?我的想法是:

Would the "array_contains" query be another practical option for social media structure scalability? My thought is:

  1. 如果用户A跟随用户B,则在我的用户"节点中将直接数组子节点写入跟随者".
  2. 在用户B创建任何轮询之前,用户B的设备会从Firestore中读取关注者"数组,以获取所有关注用户的列表,并将其填充在客户端的Array对象中
  3. 然后,当用户B编写新的调查时,请将该关注者"数组添加到该调查中,以便用户B的每个新调查都将附加一个包含其后所有用户ID的数组.

"array_contains"查询有哪些限制?在Firebase中存储包含数千个用户/关注者的数组是否可行?

What are the limitations on the "array_contains" query? Is it practical to have an array stored in Firebase that contains thousands of users / followers?

推荐答案

"array_contains"查询是否会成为社交媒体结构可伸缩性的另一个实用选择?

Would the "array_contains" query be another practical option for social media structure scalability?

是的.这就是Firebase创建者添加此功能的原因.

Yes of course. This the reason why Firebase creators added this feature.

看到您的结构,我认为您可以尝试一下,但是可以回答您的问题.

Seeing your structure, I think you can give it a try, but to responde to your question.

"array_contains"查询有哪些限制?

What are the limitations on the "array_contains" query?

您存储什么类型的数据没有限制.

There is no limitations regarding what type of data do you store.

在Firebase中存储包含数千个用户/关注者的数组是否可行?

Is it practical to have an array stored in Firebase that contains thousands of users / followers?

关于实用性与否,与其他类型的限制有关.问题在于文档有限制.因此,在文档中可以放入多少数据方面存在一些限制.根据有关用法和限制的官方文档:

Is not about practical or not, is about other type of limitations. The problem is that the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:

文档的最大大小:1 MiB(1,048,576字节)

Maximum size for a document: 1 MiB (1,048,576 bytes)

如您所见,单个文档中的数据总数限制为1 MiB.当我们谈论存储文本时,您可以存储很多东西.因此,在您的情况下,如果您仅存储ID,我认为那将是没有问题的.但是恕我直言,随着您的数组越来越大,请注意此限制.

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much. So in your case, if you would store only ids, I think that will be no problem. But IMHO, as your array getts bigger, be careful about this limitation.

如果要在阵列中存储大量数据,并且这些阵列应由许多用户更新,则需要注意另一个限制.因此,每个文档每秒只能写入1次.因此,如果您遇到许多用户都试图一次将数据写入/更新到同一文档的情况,那么您可能会开始发现其中一些写入操作会失败.因此,也要注意此限制.

If you are storing large amount of data in arrays and those arrays should be updated by lots of users, there is another limitation that you need to take care of. So you are limited to 1 write per second on every document. So if you have a situation in which a lot of users al all trying to write/update data to the same documents all at once, you might start to see some of this writes to fail. So, be careful about this limitation too.

这篇关于使用“包含数组"查询Cloud Firestore社交媒体结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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