Javascript Firebase 按日期范围查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27908318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase query by date range
提问by Paul
My firebase data looks something like this:
我的 Firebase 数据如下所示:
{
"lambeosaurus": {
"date" : 1420753443,
"length" : 12.5,
"weight": 5000
},
"stegosaurus": {
"date" : 1429053443,
"length" : 9,
"weight" : 2500
}
}
I am looking for a way to filter the dates using a custom date range. I've seen some examples online where they have queried for date, but the timestamp has always been the key which isn't possible in my case.
我正在寻找一种使用自定义日期范围过滤日期的方法。我在网上看到了一些他们查询日期的例子,但时间戳一直是关键,这在我的情况下是不可能的。
The relevant section is the documentation can be found here: https://www.firebase.com/docs/web/guide/retrieving-data.html#section-queries
相关部分是可以在此处找到的文档:https: //www.firebase.com/docs/web/guide/retrieving-data.html#section-queries
It's important that real time updating is still supported. I also only want to determine the number of all the items that fit the criterion. I am not interested in the data point for each node.
重要的是仍然支持实时更新。我也只想确定符合条件的所有项目的数量。我对每个节点的数据点不感兴趣。
回答by Keenan Lidral-Porter
Looks like you can use the orderBychild()method to order by child key or property (i.e. weight, length, or date). And if you chain that using startAt()& endAt(), we can construct the query you're looking for:
看起来您可以使用该orderBychild()方法按子键或属性(即weight、length、 或date)进行排序。如果您使用startAt()&链接它endAt(),我们可以构建您正在寻找的查询:
var startDate = 14200000000;
var endDate = 14300000000;
ref.orderByChild("date").startAt(startDate).endAt(endDate)
.on("child_added", function(snapshot){
console.log("got the data!", snapshot);
});

