使用键节点对Firebase进行排序/过滤 [英] Firebase sorting/filtering with keys node

查看:68
本文介绍了使用键节点对Firebase进行排序/过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
"places": {
    "<push-key>": {
        "name": "Company X"
    },
    "<push-key>": {
        "name": "Restaurant Y"
    }
}
"stamps": {
    "<push-key>": {
        "value": 0,
        "date": 1487344650456
    }
},
"placeStamps": {
    "<place-push-key>": {
        "<stamp-push-key-1>": true,
        "<stamp-push-key-2>": true,
        "<stamp-push-key-3>": true
    }
}
}

我们是Firebase的新手,我们想知道是否有一种方法可以按日期对图章(数据库中的节点)进行排序.我们的流程如下:

We are new to Firebase and we were wondering if there was a way to sort our stamps (node in the database) by date. Our flow goes as following:

  • 我们使用places节点检索所需的位置.
  • 使用地点的键,我们从placeStamps/节点检索图章键.
  • 然后我们从图章节点一张一张地获取图章.

是否可以通过日期"或值"对这些对象进行排序/过滤?我们无法在客户端进行此操作,因为我们正在谈论数以百万计的邮票.

Is there a way to sort/filter these objects by for example the ‘date’ or ‘value’? We can’t do it client-side, because we are talking about millions of stamps.

推荐答案

要查询24小时以来的邮票:

To query the stamps since 24 hours ago:

var ref = firebase.database.ref("stamps");
var now = Date.now();
var yesterday = now - 24 * 60 * 60 * 1000;
var query = ref.orderByChild("Date").startAt(yesterday);
query.on("child_added", function(snapshot) {
    console.log(snapshot.key);
});

如果要通过时间戳取回某个地方的邮票,则有两个选择:

If you want to retrieve the stamps for a certain place by timestamp, you have two options:

  • 获取该地点的邮票,并通过时间戳客户端对其进行订购.检索多个项目并不像您预期​​的那样慢,因为Firebase会流水线处理请求.参见
  • retrieve the stamps for the place and order them by timestamp client-side. Retrieving multiple items is not as slow as you may expect, since Firebase pipelines the requests. See Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly.
  • add the timestamp to the stamp key for each place:

placeStamps": {
  "<place-push-key>": {
    "<stamp-push-key-1>": 1487344650456,
    "<stamp-push-key-2>": 1487344650457,
    "<stamp-push-key-3>": 1487344650458
  }
}

现在,您可以通过以下方式通过时间戳获取某个地方的邮票:

Now you can get the stamps for a place by timestamp with:

var ref = firebase.database.ref("placeStamps").child("<place-push-key>");
var now = Date.now();
var yesterday = now - 24 * 60 * 60 * 1000;
var query = ref.orderByValue().startAt(yesterday);

这篇关于使用键节点对Firebase进行排序/过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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