减少Firestore Android中的读取次数 [英] Reduce number of read in Firestore Android

查看:61
本文介绍了减少Firestore Android中的读取次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想减少读取次数,以便从QuestionCollection(问题数组)获取问题的详细信息.

I want to reduce number of reads in order to get Question's detail from QuestionCollection (Array of Questions) .

我的应用就像堆栈溢出的一部分.

我想显示一个问题有多少顶,观点和评论.因此,为了处理这个问题,我创建了多个类似

I want to show how many likes, views and comments do a question have . So in order to handle this I created multiple collection like

QuestionCollection

  • ID
  • 标题
  • DateCreated

评论集合

  • ID
  • 评论
  • DateCreated

QuestionLike

  • QuestionID
  • 用户ID

QuestionViews

  • QuestionID
  • 用户ID

QuestionTAG

  • QuestionID
  • TagID

我认为 CommentCollection (评论)仅属于一个问题.因此,我正在考虑像数组或集合那样将QuestionCollection并入(但是需要另一个读取命中). 哪个更好?

I thought that CommentCollection (comments) only belongs to a question. So I am thinking of incorporating in QuestionCollection like an array(s) or collection(But it requires another read hit). which one is better ?

我想将两个变量(totalViews,totalLikes)添加到QuestionCollection.但是每次我必须检查该用户是否喜欢此问题或该用户查看该问题.

And I think of adding two variable's(totalViews,totalLikes) to QuestionCollection. But each time I must have to check whether this question is liked by this user or view by this user.

给我一​​个建议或其他选择,这样我可以在获取问题详细信息方面获得最少的点击量.

Give me a suggestion or an alternative so I can have the least hit to fetch question details.

修改

用于显示观看次数 我迭代并计数 QuestionViews集合,其中questionID == myQuestionID

For Showing View Count I iterate and count QuestionViews Collection where questionID == myQuestionID

用于显示喜欢计数 我迭代并计数 QuestionLikes集合,其中questionID == myQuestionID

For Showing Likes Count I iterate and count QuestionLikes Collection where questionID == myQuestionID

为什么不将其添加到QuestionCollection?因为我想显示观看次数最多和最喜欢的问题,例如stackoverflow.如果我将其添加到QuestionCollection中,我怎么能知道最喜欢和最常查看的问题.

why I don't add it to QuestionCollection? Because I want to show most Viewed and most like question like stackoverflow. If i add it to QuestionCollection How could I know the most like and most Viewed question.

推荐答案

我能想到的最有效的方案如下:

The most efficient schema I can think of, is the following:

Firestore-root
    |
    --- questions (collections)
    |     |
    |     --- questionId (document)
    |            |
    |            --- questionId: "02cubnmO1wqyz6yKg571"
    |            |
    |            --- title: "Question Title"
    |            |
    |            --- date: August 27, 2018 at 6:16:58 PM UTC+3
    |            |
    |            --- comments (colletion)
    |            |     |
    |            |     --- commentId
    |            |            |
    |            |            ---commentId: "5aoCfqt2w8N8jtQ53R8f"
    |            |            |
    |            |            ---comment: "My Comment"
    |            |            |
    |            |            ---date: August 27, 2018 at 6:18:28 PM UTC+3
    |            |
    |            --- likes (colletion)
    |            |     |
    |            |     --- likeId (document)
    |            |            |
    |            |            --- userId: true
    |            |
    |            --- views (colletion)
    |            |     |
    |            |     --- viewId (document)
    |            |            |
    |            |            --- userId: true
    |            |
    |            --- tags ["tagId", "tagId"]
    |
    --- tags (collections)
          |
          --- tagId (document)
                |
                --- tagId: "yR8iLzdBdylFkSzg1k4K"
                |
                --- tagName: "Tag Name"

其中commentslikesviewsquestionId文档下的标题.与在Firebase实时数据库中显示问题列表不同,在Firebase实时数据库中,您将已经下载了整个问题对象,而在Cloud Firestore中,这不再是问题.因此,为避免使用where方法进行查询,您可以简单地获得一个特定的问题,如下所示:

In which the comments, likes and views are colletions under questionId document. Unlike in Firebase real-time database where to display a list of question you would have been downloaded the entire question object, in Cloud Firestore this is not an issue anymore. So to avoid querying using a where methods, you can simply get a specific question like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference questionIdRef = rootRef.collection("questions").document(questionId);
questionIdRef.get().addOnCompleteListener(/* ... */);

由于单个问题可能有成百上千,甚至数千个甚至更多的评论,喜欢和观点,因此将数据存储到数组中将无济于事.根据官方文档:

Because you can have hundreds, thousands or even more comments, likes and views to a single question, storing the data into an array will not help you. According to the official documentation:

Cloud Firestore经过优化,可以存储大量小文件.

Cloud Firestore is optimized for storing large collections of small documents.

例如,要获取特定问题的所有评论,可以使用以下查询:

For example, to get all the comments of a specific question, you can use the following query:

Query query = rootRef.collection("questions/"+questionId+"comments").orderBy("date", Query.Direction.DESCENDING);

因为一个问题只能有几个标签,所以您可以简单地使用数组.要获取带有特定标签的所有问题,可以使用以下查询:

Because a question can have only a few tags, you can simply use an array. To get all questions with a specific tag, you can use the following query:

Query query = rootRef.collection("questions/"+questionId+"tags").whereArrayContains("tags", tagId);

还有一点要记住,即使tags对象作为数组存储在数据库中,document.get("tags")也会返回 ArrayList 而不是array.

One more thing to remember, even if tags object is stored in the database as an array, document.get("tags") will return an ArrayList and not an array.

如果您还想计算收藏集中的点赞次数或其他任何数量的文档,请从此

If you also want to count the number of likes or any other number of documents within a collection, please see my answer from this post.

根据您的评论:

如果我想向用户显示所有用户中最喜欢的问题.我该怎么办?

If I want to show the most liked question of all to a user. How could I do this?

您应该将点赞的次数添加为问题对象内的属性,然后您可以根据其查询数据库,如下所示:

You should add the number of likes as a property inside the question object and then you can query the database according to it like this:

Query query = rootRef.collection("questions").orderBy("numberOfLikes", Query.Direction.DESCENDING);

根据 ,您还可以存储在Firebase实时数据库中喜欢,您可以使用

According to this you can also store the number of likes in the Firebase real-time database and you can increase/decrease it using Firebase Transactions.

第二,如果我想显示所有与诸如数学之类的标签相关的问题(显示所有具有数学标签的问题)?

secondly if I want to show all the question related to a tag like mathematics (show all question which have mathematics tag) ??

如上所述,您可以利用whereArrayContains方法.您还可以将标签存储为字符串而不是ID.为此,您需要更改此结构:

As also mentioned above you can take atvantage of the whereArrayContains method. You can also store the tags as String and not as id. For that, you need to change this structure:

--- tags ["tagId", "tagId"]

--- tags ["mathematics", "chemistry"]

代码应如下所示:

Query query = rootRef.collection("questions/"+questionId+"tags").whereArrayContains("tags", "mathematics");

该语句(userId:true)节省了大量数据重复

the statement (userId: true) saves a lot of data repetition

是的.

这篇关于减少Firestore Android中的读取次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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