问题描述
这是当前的样本结构
Posts(Collection)
- post1Id : {
viewCount : 100,
likes : 45,
points : 190,
title : "Title",
postType : image/video
url : FileUrl,
createdOn : Timestamp,
createdBy : user20Id,
userName : name,
profilePic: url
}
Users(Collection)
- user1Id(Document):{
postsCount : 10,
userName : name,
profilePic : url
}
viewed(Collection)
- post1Id(Document):{
viewedTime : ""
}
- user2Id(Document)
最终目标是 -我需要获取当前用户未查看的getPosts,并且按点数字段以分页的降序排列.
The End goal is - I need to getPosts that the current user did not view and in points field descending order with paging.
可能的最佳解决方案是什么(例如更改结构,云功能,来自客户端的多个查询)?
What are the possible optimal solutions(like changing structure, cloud functions, multiple queries from client side)?
推荐答案
看到您的数据库结构,我可以说您快到了.根据您的评论,您在以下参考文献中托管:
Seeing your database structure, I can say you're almost there. According to your comment, you are hosting under the following reference:
Users(Collection) -> userId(Document) -> viewed(Collection)
作为文档,用户查看过的所有帖子,而您想获取用户未查看的所有帖子.因为Firestore中没有!=
(不等于)运算符,也没有arrayNotContains()
函数,所以唯一的选择是为要显示的每个帖子创建一个额外的数据库调用,并检查该特定帖子是否为已经见过还是没见过.
As documents, all the posts a user has seen and you want to get all the post that the user hasn't seen. Because there is no !=
(not equal to) operator in Firestore nor a arrayNotContains()
function, the only option that you have is to create an extra database call for each post that you want to display and check if that particular post is already seen or not.
要实现此目的,首先需要在名为postId
的帖子对象下添加另一个属性,该属性将实际的帖子ID保留为String.现在,每次您要显示新帖子时,都应该检查viewed
集合中是否已经存在该帖子ID.如果不存在,则在所需的视图中显示该帖子,否则不显示.就是这样.
To achieve this, first you need to add another property under your post object named postId
, which will hold as String the actual post id. Now everytime you want to display the new posts, you should check if the post id already exist in viewed
collection or not. If it dons't exist, display that post in your desired view, otherwise don't. That's it.
根据您的评论:
是的,要显示第一个帖子,需要两个数据库调用,一个要获取帖子,第二个要查看它是否被看到.
Yes, for the first post to appear, two database calls are need, one to get post and second to see if it was or not seen.
否,如上所述只有两个呼叫.
No, only two calls, as explained above.
不,这是NoSQL数据库的工作方式.
No, this is how NoSQL database work.
我不知道.还有另一种方法可以使用,但仅适用于用户数量有限且帖子查看次数有限的应用.此选项是将用户ID存储在每个post对象的数组中,并且每次您要显示帖子时,只需检查该数组中是否存在该用户id.
Not I'm aware of. There is another option that will work but only for apps that have limited number of users and limited number of post views. This option would be to store the user id within an array in each post object and everytime you want to display a post, you only need to check if that user id exist or not in that array.
但是,如果数以百万计的用户可以查看帖子,则在数组中存储数百万个id并不是一个好选择,因为这种情况下的问题是文档具有限制.因此,在文档中可以放入多少数据方面存在一些限制.根据有关用法和限制的官方文档:
But if a post can be viewd by millions of users, storing millions of ids within an array is not a good option because the problem in this case 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.因此,您几乎无法将所有内容存储在文档中.
As you can see, you are limited to 1 MiB total of data in a single document. So you cannot store pretty much everything in a document.
这篇关于用于获取看不见的趋势帖子的Firebase Firestore结构-社交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!