组合orderByChild和startAt的Firebase数据库查询不返回任何内容

时间:2018-04-05 10:40:12

标签: android firebase firebase-realtime-database

我正在尝试使用以下查询查询firebase数据库:

Query test = mDatabase.orderByChild("creation_date").startAt("-L9H3X-qZZt5WiAdsOKE");

我的数据如下:

-L9H2ea9Ygt0pD5f17Ze
    creation_date: 
        20180904
    post_id:
        -L9H2ea9Ygt0pD5f17Ze

-L9H3Dmw9gn9lUOV10hK
    creation_date: 
        20180403
    post_id:
        -L9H3Dmw9gn9lUOV10hK

-L9H3OiIzjegJJuuFSfB
    creation_date: 
        20180302
    post_id:
        -L9H3OiIzjegJJuuFSfB

-L9H3X-qZZt5WiAdsOKE
    creation_date: 
        20180404
    post_id:
        -L9H3X-qZZt5WiAdsOKE

-L9JmFch2lnftlMHHiQd
    creation_date: 
        20180405
    post_id:
        -L9JmFch2lnftlMHHiQd

-L9JueI5x9oFRhOX6a3G
    creation_date: 
        20180405
    post_id:
        -L9JueI5x9oFRhOX6a3G

我知道订购后它看起来像这样:

-L9H3OiIzjegJJuuFSfB
    creation_date: 
        20180302
    post_id:
        -L9H3OiIzjegJJuuFSfB

-L9H3Dmw9gn9lUOV10hK
    creation_date: 
        20180403
    post_id:
        -L9H3Dmw9gn9lUOV10hK

-L9H3X-qZZt5WiAdsOKE
    creation_date: 
        20180404
    post_id:
        -L9H3X-qZZt5WiAdsOKE

-L9JmFch2lnftlMHHiQd
    creation_date: 
        20180405
    post_id:
        -L9JmFch2lnftlMHHiQd

-L9JueI5x9oFRhOX6a3G
    creation_date: 
        20180405
    post_id:
        -L9JueI5x9oFRhOX6a3G

-L9H2ea9Ygt0pD5f17Ze
    creation_date: 
        20180904
    post_id:
        -L9H2ea9Ygt0pD5f17Ze

我希望查询返回最后四个元素,但它会返回任何内容。

我还在firebase安全规则中添加了一个索引。

任何人都可以帮我弄清楚我做错了吗?

1 个答案:

答案 0 :(得分:1)

您需要按孩子的价值排序:

Query test = mDatabase.orderByChild("creation_date").startAt(20180904);

这是-L9H3X-qZZt5WiAdsOKE随机密钥,它是子creation_date

的父节点