Firestore查询和过滤排行榜的操作方法 [英] Firestore query and filtering how to do for leaderboard

查看:65
本文介绍了Firestore查询和过滤排行榜的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个具有波动性的任务应用程序,并展示了排行榜,其中排行榜上有15名最佳玩家,每天有15名,每周最佳15名,每月最佳15名.用户可以按周或月的日期进行过滤,并向他们显示结果列表,按任务最多的人排序.

I'm creating a task app with flutter and I'm showing a leaderboard with the 15 best players from day, 15 best of week and 15 best of month. The users can filter by day week or month and im showing a list of the results to them, ordered by people with most finished tasks.

现在,我打算对客户进行按月,按月和日过滤,而不是对日,周,月进行3个查询.这是我的查询:

Now instead of doing 3 queries with day, week, month, I planned to do a month query and filter for week and day by client. This is my query:

Timestamp get timeLimit {
    final limit = DateTime.now().subtract(const Duration(days: 30));
    return Timestamp.fromDate(limit);
  }

    getRanking() async {
        List<Data> _rankingResult = [];
    
    
        QuerySnapshot snapshot = 
        await FirebaseFirestore.instance
        .collection('tasks')
        .where('dateWhenProofed', isGreaterThanOrEqualTo: timeLimit)
        .limit(15) <- what about that?
        .get();
        
        snapshot.docs.forEach((document) {
          Data data = Data.fromJson(document.data());
          _rankingResult.add(data);
          _rankingResult.sort((a, b) => b.finishedTasksCount.compareTo(a.finishedTasksCount));
        });

    notifier.rankingList = _rankingResult;

  }

我想知道 .limit().对于每个过滤器,我只需要15个最佳结果,但是如果我们这里有1000个文档, limit()的工作原理又如何呢?它是从任务收集中检查整个Firestore文档,还是仅检查前15个文档?我的目标是从中获得15个最高价值.

Im wondering about the .limit(). I need only the 15 best results for each filter, but what if we have 1000 documents here, how exactly does the limit() work? Does it check the whole firestore documents from task collection or does it only check the first 15? My goal is to get the 15 highest values out of this to present.

我还想知道我们这里是否有15个结果,并且仅在客户端将其过滤到星期,我如何确保在那里也得到15个星期的结果?也许我不必限制月份查询了吗?如果有人对此有所了解,那就太好了.

Also im wondering if we have 15 results here and I filter it clientside to week only, how can I make sure to also get 15 results of the week there? Maybe I don't have to limit the month query then? Would be nice if someone has an idea about this.

我不确定100%是否肯定(有人应该确认),但是看起来像查询中的 limit()一样,我们正在截取排在前15位的数据.我认为我们可以通过添加 orderBy()参数来避免这种情况.现在查询将如下所示:

Well I'm not 100% sure (someone should confirm that) but it looks like with limit() in the query, we are cutting data which would be in the top 15 list. I think we can avoid that by adding the orderBy() parameter. Query now would look like this:

QuerySnapshot snapshot = 
    await FirebaseFirestore.instance
    .collection('tasks')
    .where('dateWhenProofed', isGreaterThanOrEqualTo: timeLimit)
    .orderBy('dateWhenProofed', descending: true) <--- added this line
    .limit(15)
    .get();

snapshot.docs.forEach((document) {
              Data data = Data.fromJson(document.data());
              _rankingResult.add(data);
              _rankingResult.sort((a, b) => b.finishedTasksCount.compareTo(a.finishedTasksCount));
            });

应用此后,我们可以确保获取所有数据.目前唯一不适合的方法是仅过滤到星期和一天,因为我们在查询中说, timeLimit 是30天.现在我们该如何做周和日数据过滤的最佳实践?

With this applied, we can make sure to get all the data. The only thing which doesnt fit right now is by filtering to week and day only now, because we said in the query, that the timeLimit is 30days. How can we do week and day data filtering best practise now?

推荐答案

您对.limit()的假设是正确的,这仅给出了前15个结果.您只需更改timeLinit变量即可获取每天的timeLimit:

You are right about your assumption with .limit(), this gives you only the top 15 results. You can simply change your timeLinit variable to get the timeLimit per day:

//today
final limit = DateTime.now();

//one week
final limit = DateTime.now().subtract(const Duration(days: 7));

这篇关于Firestore查询和过滤排行榜的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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