如何计算 Firestore 中的独特投票?

How to count unique votes in Firestore?

我正在创建一个应用程序,用户可以在其中投票 posts 并创建自己的 posts。由于一个文档最多可以存储 1 Mb,我不知道要跟踪唯一投票和投票计数。

当我创建一个新用户时,我是这样创建的:

collection('users').document(user.uid).setData({
        'name': name + ' ' + surname,
        'email': user.email,
        'registerDate' : DateTime.now(),
        'isEmailVerified': user.isEmailVerified,// TODO: Email Verification
        'location': location,
        'photoUrl': user.photoUrl,
        'posts' : [],

      });

当我创建一个新的 post 时,我是这样创建的:

collection('posts').document(imageId).setData({
        'userId': user.uid,
        'user' : user.displayName,
        'title' : title,
        'post' : post,
        'location': location,
        'postDate' : DateTime.now(),
        'upVote' : 1,
        'downVote' : 0,
        'absVote' : 0,
        'votedBy' : [user.uid], // list of users who voted the post.

      });

我还将 post 添加到用户的 post 数组中。

我认为 'posts' 集合中的 'votedBy' 字段有误。该字段可以填写文档的最大大小,即 1 Mb。有没有更好的方法来跟踪选票?

我想我也无法将投票存储在用户集合中。如果用户投票过多posts,将再次超出限制大小。

实现无界列表的正确方法是在新的子集合中为每个项目使用一个文档:usersposts 下。

你的情况:

posts/:postId/users/:userId

Firestore 旨在大规模扩展集合中的文档数量,您不会遇到任何扩展问题。