Firebase:如何在过去n天内为大多数共享帖子构建数据? [英] Firebase: How to structure data for the most shared posts in the last n days?

查看:183
本文介绍了Firebase:如何在过去n天内为大多数共享帖子构建数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个博客,你想列出今天,最近7天和最后30天最喜欢或共享的帖子。



今天的解决方案相当简单:

  -mostSharedPostsForToday 
-2018-10-08
- $ postId
-numberOfShares

然后查询会观察 mostSharedPostsForToday / 2018- 10-08 对于今天由孩子 numberOfShares 定购的最多共享帖子。在过去n天内构建大多数共享帖子的数据?

我能想到的一个解决方案是编写一个云端函数来填充节点 mostSharedPostsForThePastNDays 每天。但是对我来说似乎很麻烦。是不是有一个更有效的方法?



编辑:正如在第一个答案的意见中指出,应支持分页,以节省流量。

解决方案

我要求你使用另一种方法。而不是使用日期作为您的节点,删除该节点,并为每个帖子添加 TIMESTAMP 像这样:

 -mostSharedPostsForToday 
- $ postId
- TIMESTAMP:2018-10-08
- numberOfShares:10



要知道您在一天内共享了多少个共享,您需要使用以下代码:

  rootRef.child( mostSharedPostsForToday)子(帖子ID).orderByChild( 时间戳)equalsTo( 2018年10月8日)。。 

如果您想要间隔,请使用以下代码:

  rootRef.child(mostSharedPostsForToday)。child(postId).orderByChild(TIMESTAMP)。startAt(2018-10-08)。endAt(二〇一八年十月一十五日 ); 

希望有帮助。


Let's assume there is a blog and you would like to list the most liked or shared posts for today, the last 7 days and the last 30 days.

The solution for today is rather easy:

-mostSharedPostsForToday
   -2018-10-08
       -$postId
           -numberOfShares

Then the query would observe mostSharedPostsForToday/2018-10-08 for today's most shared posts ordered by child numberOfShares.

But how to structure the data for the most shared posts in the past n days?

One solution I can think of is to write a cloud function that populates the node mostSharedPostsForThePastNDays on a daily basis. But it seems cumbersome to me. Isn't there a more efficient way?

Edit: As pointed out in the comments of the 1st answer pagination should be supported to save traffic.

解决方案

I recomand you to use another approach. In stead of using the date as your node, remove that node and add a TIMESTAMP for each post like this:

-mostSharedPostsForToday
   -$postId
       - TIMESTAMP: 2018-10-08
       - numberOfShares: 10

To know how many shared you have in a single day, you need to use the following code:

rootRef.child("mostSharedPostsForToday").child(postId).orderByChild("TIMESTAMP").equalsTo("2018-10-08");

And if you want an interval please use the following code:

rootRef.child("mostSharedPostsForToday").child(postId).orderByChild("TIMESTAMP").startAt("2018-10-08").endAt("2018-10-15");

Hope it helps.

这篇关于Firebase:如何在过去n天内为大多数共享帖子构建数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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