OrderBy查询-我可以由孙子"Vote"订购吗?存储在firebase中? [英] OrderBy query - Can I order by the grandchild "Vote" stored in firebase?

查看:31
本文介绍了OrderBy查询-我可以由孙子"Vote"订购吗?存储在firebase中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一种方法来通过子节点"votes"进行查询,以按"votes"的数量对数据进行排序.我非常确定,因为我将数据存储在Firebase中的方式无法查询,但我目前必须以这种方式存储.

I've been trying to find a way to query by the child node 'votes' to order the data by number of 'votes'. I'm pretty sure because of the way I have stored the data in firebase it cant be queried but I currently have to store it this way.

  {
     "45" : {
       "Biological and Ecological" : {
         "Votes" : 1
        }
    },
     "567" : {
       "Technology" : {
         "Votes" : 2
        }
    },
     "620" : {
       "Technology" : {
          "Votes" : 1
        }
  },
     "702" : {
       "Social and Behavioral" : {
          "Votes" : 1
        }

     }
 }

我正在尝试由孩子投票"订购,有没有办法做到这一点?下面是当前显示方法,我知道.OrderbyChild("Votes")不能正确定义路径,但是有办法实现吗?

I am attempting to order by the child "Votes" is there a way to do this? Below is the method in which it is currently displayed I am aware that .OrderbyChild("Votes") isnt defining the path correctly but is there a way to achieve this?

   Query query = FirebaseDatabase.getInstance().getReference("CountLikes/").orderByChild("Votes");

   query.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for(DataSnapshot child: dataSnapshot.getChildren()) {

                for(DataSnapshot grandchild: child.getChildren()) {
                    arrayList.add("Project " + child.getKey() + "         " + grandchild.getKey() + "        " + (String.valueOf(grandchild.child("Votes").getValue(Long.class)))); 

                    adapter.notifyDataSetChanged();


                }

                }}



        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });

推荐答案

这取决于您如何在RealtimeDatabase中实际设置数据模型.在这种情况下,您尝试排序的数据没有统一的路径(看起来是不同的投票类型).

This depends a bit on how you're data model is actually set up in the RealtimeDatabase. In this case, there is not a consistent path to the data you're attempting to sort by (different vote types it seems).

如果您有类似的东西:

CountLikes: [ // collection
    docId: { // document
        typeOfVote: { // map name
            "votes": number // vote count
        }
    }
]

如果将"votes"与"typeOfVote"分开,则您可以尝试按孩子订购的方式,因为目前您无法通过潜在的通配符按孩子订购(要获得比"Technology"更深的层次).

The way you are trying to order by the child will be possible if "votes" is split up from "typeOfVote" because right now you can't order by child through a potential wildcard (to get a level deeper than "Technology" for example).

CountLikes: [
    docId: {
        "votes": number,
        "type": typeOfVote
    }
]

这样,您的orderByChild投票"可能可能有效:)

With this, your orderByChild "votes" could probably work :)

FirebaseDatabase.getInstance().getReference("CountLikes/").orderByChild("votes")

(更新)弗兰克的答案(并阅读评论)显示了orderByChild的另一个示例/更多信息.

(update) This answer by Frank (and read the comments) shows another example/more info for orderByChild.

这篇关于OrderBy查询-我可以由孙子"Vote"订购吗?存储在firebase中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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