firestore 集合中的字段数组中可以存储多少个 id

How many ids can be stored in a field array in firestore collection

我正在寻找一种文档设计来存储其他用户喜欢的用户 ID。我的第一个方法(也许不是最好的)是一个数组来存储所有喜欢某人的用户 ID。类似于:

/users/1
 - likedByUserIds: [1,2,3,4,5,...,1000,1000001,...]

我必须查询尚未喜欢的用户并先显示他们。那么在 firestore collection 的字段数组中可以存储多少个 id?它可以是来自 previous/past 个赞的多个用户 ID。

数组的元素个数不固定。您 运行 的限制是文档的最大总大小 (1MB),记录在案 here. The total size of the document is not going to be based on a single field. It will be based on everything you put in it. You can estimate the size of a document using information here

对于不受单个文档限制的数据列表,最好将项目作为单个文档存储在集合中。集合中的文档数量没有限制。

正如@Doug 所说,“一个文档的最大总大小:1MB”。还有一个 Firestore single-field 索引豁免限制为 200。single-field index 存储集合中包含特定字段的所有文档的排序映射。 single-field 索引中的每个条目都记录了特定字段的文档值以及文档在数据库中的位置。

此外,还有一个 single-field index exemptions,您可以通过创建 single-field 索引豁免来从自动索引设置中豁免一个字段。索引豁免覆盖 database-wide 自动索引设置。因为我们需要在每个可以处理对数据库的写入的进程的内存中保存该信息,并将传入的写入与此豁免列表进行比较。这需要内存管理和延迟影响。

如需进一步参考,您可以查看有关 Firestore single-field 索引豁免限制的相关

这是我找到的一个解决方案,可以避免在 firebase 中进行全面搜索以执行 get users that haven't liked yet

  • 我在 firestore 中的用户集合
  • 添加用户到redis add('users',userId)
  • 将喜欢的用户添加到redis zAdd('liked_users_${uid}',{value: userId})
  • 生成尚未点赞zDiffStore('not_liked_yet',['users','liked_users_${uid}']
  • 还不喜欢zRange('not_liked_yet', 0, -1)

刚刚好!