Firebase从中键向上和向下查询数据 [英] Firebase querying data up and down from the middle key

查看:104
本文介绍了Firebase从中键向上和向下查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有日期的数据列表。我通常查询按日期排序的数据。但是当我需要从中间键来回取回某些数据时,我正处于这种情况。我有数据按日期排序( orderByChild(date)),如下所示:

 key1:{
date:2016-07-25
},
key2:{
date :2016-07-26
},
key3:{
date:2016-07-27
},
key4:{
date:2016-07-28
},
key5:{
date:2016-07-29




$ block $ $ $ b $注意:日期可以是在将来也是如此。每个日期可以有多个条目。如果我知道 key3 ,那么这两个日期之间可能会有差距。 ,我如何从上面的 key3 (即 key2 )从列表的末尾检索数据(即 KEY5 )?例如,


  • 用key3 - > key2键入key5(直到结束)。

  • with key4 - > key3 to key5(until end)。


如何查询它与钥匙,并由孩子订购日期。我有一个 key2 。从这我需要获取列表。



我试图获取结果的部分,一个上,一个更低。但是,当我使用 orderByChild(date),我不能使用 endAt() :您可以结合 startAt() endAt()方法将结果限制在)。



有没有什么办法可以根据键检索列表?

解决方案

我已经改变了键的定义方式。



而不是 push(),我用date和push()的组合来设置键。新密钥的格式如下: YYYYMMDD< PUSH_KEY> 有了这种格式,我能够查询数据与2个单独的查询。




$ b

  ref.orderByKey() .endAt(key).limitToLast(1)

和一个以关键字开始的


$ b $ pre $ ref $ order $ key $
$ $ $ $ $ $ $ $ $
$ b

这给了我从中间键两端得到的结果。



谢谢@puf和大家的意见和帮助。

I have a list of data having date. I usually query that data sorted by date. But I am in a situation when I need to retrieve some data back and forth from the middle key. I have data sorted by date (orderByChild("date")) like this:

{
    "key1":{
        "date": "2016-07-25"
    },
    "key2":{
        "date": "2016-07-26"
    },
    "key3":{
        "date": "2016-07-27"
    },
    "key4":{
        "date": "2016-07-28"
    },
    "key5":{
        "date": "2016-07-29"
    }
}

Note: Dates can be in future also. There can be multiple entries for each date. There can be a gap between two dates.

If I know the key3, How can I retrieve data from one above key3 (i.e. key2) to end of the list (i.e. key5)?

So for example,

  • with key3 -> key2 to key5 (till end).
  • with key4 -> key3 to key5 (till end).

How can I query it with key, and order by child "date". I have a key2. And from that I need to fetch the list.

I have tried to fetch results in parts, by one for upper and one for lower. But, when I use orderByChild("date"), I can not use endAt() with keys (Docs says: you can combine the startAt() and endAt() methods to limit the results to a specified range of values).

Is there any way I can retrieve the list based on key? In a single go or in parts?

解决方案

I have changed the way keys are defined.

Instead of push(), I have set the key with the combination of date and push().

Like, the new key is in the following format YYYYMMDD<PUSH_KEY>. With this format, I am able to query data with 2 separate queries.

One ending with the key:

ref.orderByKey().endAt(key).limitToLast(1)

and one starting with the key:

ref.orderByKey().startAt(key)

This gives me the results from both the ends from the middle key.

Thank you @puf and everyone for the comments and help.

这篇关于Firebase从中键向上和向下查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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