Firebase数据库按更深的子级排序 [英] Firebase database sort by deeper child

查看:47
本文介绍了Firebase数据库按更深的子级排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑Firebase数据库的以下结构:

Considering the following structure of the Firebase database:

    • game1
      • $ playerUidA
        • 得分:50
        • 得分:10
        • $ playerUidC
          • 得分:20
          • 得分:30
          • .....

          我想运行一个查询,该查询将返回所有游戏节点,其中每个游戏节点(玩家)的子代将根据得分进行排序.游戏节点包含每个玩家的UID,每个UID节点包含玩家的分数.我还将存储其他数据,但是,出于本示例的考虑,我将仅使用得分.

          I want to run a query that will return all game nodes, where the children of each game node (the players) will be sorted based on the score. The game node contains the UID of each player, and each UID node contains the score of the player. I am storing other data as well, however, for the sake of this example I will be only using the score.

          我可以通过一个查询来做到这一点吗?就像是 rootRef.child("root").orderByChild("score")?不幸的是,这似乎不起作用.

          Can I do that with a single query? Something like rootRef.child("root").orderByChild("score")? Unfortunately that doesn't seem to work.

          还是实现这一目标的唯一方法是通过手动对客户端上的项目进行排序?

          Or the only way to achieve that is by manually sorting the items on the client?

          @Puf-希望您会回答:)

          @Puf - Hope you'll answer that :)

          推荐答案

          尽管这个问题比较老,但还是有一些人(像我一样)在上面绊脚石.特别是因为以问题作者的相似方式来构造数据库,以(例如)创建游戏排行榜系统非常直观.由于答案有点过时,因此我想添加一些内容.

          Although this question is rather old there might be people (like me) stumbling over it. Especially because it is pretty intuitive to structure the database in a similar way the author of the question did, to create (for example) a leaderboard system for a game. Since the answer is a bit outdated I wanted to add some things.

          前段时间,开发人员增加了深度嵌套孩子订购的可能性!(请参阅按指定的顺序进行排序子键)为此,您基本上必须做与作者完全相同的操作,并另外使用@adolfosrs给出的答案的第一部分.基本上,您必须做两件事:

          Some time ago the devs added the possibility to order by deeply nested children! (See Ordering by a specified child key) To do so you basically have to do the exact same thing the author did and additionally use the first part of the answer given by @adolfosrs. Basically you have to do two things:

          1. 使用 .indexOn (如@adolfosrs所述)
          2. 使用 OrderByChild()命令.
          1. Work with .indexOn (as described by @adolfosrs)
          2. Use the OrderByChild() command.

          要在@steliosf给出的示例上进行此工作,您必须执行以下操作:

          To make this work on the example given by @steliosf you would have to do the following:

          首先在数据库规则中设置.indexOn:

          First set the .indexOn in your database rules:

          {
            "rules": {
              "$gameId": {
                ".indexOn": "score",
                "$playerUid": {
                  ...  
                }
              }
            }
          }
          

          第二使用命令已使用过的问题的作者:

          Second use the Command the author of the question already used:

          rootRef.Child("root").OrderByChild("score")
          

          我建议您始终添加LimitToFirst()或LimitToLast()命令,以避免拉动整个数据库,这可能是很多数据(当然取决于数据库的大小).例如,要获得前10名得分,您可以使用:

          I would recommend that you always add a LimitToFirst() or LimitToLast() command to avoid that you pull the whole database which might be a lot of data (depending on the size of your database of course). To get for example the top 10 scores you could use:

          rootRef.Child("root").OrderByChild("score").LimitToLast(10)
          

          由于数据是按升序排列的,因此需要使用LimitToLast().

          Since the data is ordered in ascending order you need to use LimitToLast().

          这篇关于Firebase数据库按更深的子级排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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