简体   繁体   English

跟踪集合中文档的计数器变量的最佳方法

[英]Best way to keep track of counter variables for documents in a collection

My project has a feature that allows following/followers similar to a lot of social.networks.我的项目有一个功能,允许关注/关注者类似于很多 social.networks。 Because I will likely get the count of these numbers very often, I figured it would be best to create a separate measure for the follower/following count so I don't have to.get() all of them to get a count.因为我可能会经常得到这些数字的计数,所以我认为最好为关注者/关注计数创建一个单独的度量,这样我就不必 to.get() 所有这些来得到一个计数。 I am currently deciding between 3 options to keep track of this counter variable.我目前正在选择 3 个选项来跟踪这个计数器变量。

Option 1 (Keep counter & collection separate)选项 1 (将柜台和收款分开)

[User Collection]           [Followers Collection]
(uid)                       (uid)
  -username                   -uid_1
  -follower_count             -uid_2

Option 2 (Put counter and data in separate collection, make a sub-collection for the data)选项2 (将计数器和数据放在单独的集合中,为数据做一个子集合)

[User Collection]           [Followers Collection]
(uid)                       (uid)
 -username                   -count
                             -[Followers]
                               -uid_1
                               -uid_2

Option 3 (Keep it all in one collection and use arrays to get all data + count in one read -- Worried about this leading to document going over 1Mib limit)选项 3 (将所有内容保存在一个集合中并使用 arrays 获取所有数据+一次读取计数——担心这会导致文档超过 1Mib 限制)

[User Collection]
(uid)
 -username
 -following
    -[uid_1, uid_2]                             

Is there a glaring issue in doing it one of these ways?使用其中一种方法是否存在明显的问题? Or would all lead to the same costs/querying time at the end of the day?或者在一天结束时都会导致相同的成本/查询时间?

Any time you have an array that can grow unbounded, it should probably be represented a documents in a subcollection.任何时候你有一个可以无限增长的数组,它可能应该表示为子集合中的文档。 That's just how Firestore works.这就是 Firestore 的工作原理。 You can have an array if you want, but as you said, you risk going over the limit, and you have a very big problem that's even more difficult and expensive to solve than the cost of the reads and writes.如果你愿意,你可以有一个数组,但正如你所说,你有超过限制的风险,而且你有一个非常大的问题,解决起来比读取和写入的成本更加困难和昂贵。

Whether or not you use a collection or subcollection is entirely up to you and how it best suits your queries.您是否使用集合或子集合完全取决于您以及它如何最适合您的查询。 Since we don't know your queries or your anticipated security rules, it's not really possible to say which one is better.由于我们不知道您的查询或您预期的安全规则,所以真的不可能说哪个更好。 Pick the one that meets your needs.选择满足您需求的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过一次查询获取 4 个文档的最佳方法? - Best way to get 4 documents with one query? Firebase Firestore:有没有办法为集合中的所有文档强制执行必填字段? - Firebase Firestore: Is there a way to enforce required fields for all documents in a collection? 有没有办法向 firestore 集合中的所有文档添加新字段? - Is there a way to add new field to all of the documents in a firestore collection? 获取Collections及子文档集 - Get Collections and sub collection of documents 有没有办法将从子集合 Firebase 中检索到的文档存储到一个数组中以显示其信息? AngularFirestore - Is there a way to store the retrieved documents from sub-collection Firebase into an array for displaying its info? AngularFirestore 从 Azure Cosmos DB 集合中下载/选择*所有*文档的最快方法是什么 - What's the fastest way to download/select *all* documents from an Azure Cosmos DB collection 在 Firebase Remote Config 中定义 bool 变量的最佳方法是什么 - What is the best way to define bool variables in Firebase Remote Config 删除 Firestore 集合中的所有文档 - Deleting all documents in Firestore collection Firestore Group Collection 文件的唯一性 - Firestore Group Collection documents uniqueness Firebase 不从某些集合的集合中获取文档 - Firebase not fetching documents from collection for some collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM