Firebase查询日期范围内的数据 [英] Firebase query for data within a date range

查看:114
本文介绍了Firebase查询日期范围内的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的firebase数据如下所示:

  {
lambeosaurus:{
vacationDates :2016-12-20 - 2016-12-25,
长度:12.5,
体重:5000
},
剑龙:{
vacationDates:2016-12-10 - 2016-12-20,
length:9,
weight:2500
}
}

我如何查询2016-12-20将要度假的所有恐龙即它应该返回lambeosaurus和剑龙)?或者我应该实际存储的数据不同?如果是这样,我应该如何存储数据以获得最佳性能?感谢。

解决方案

结合这两个日期并不会使数据库更易于查询。

如果您正在某个特定的日子在假期寻找恐龙,他们可能在此之前的任何时间去度假。 (除非有一个策略要求假期中的最大天数)所以结束日期可能是你想要查询的:

 <$ 
lambeosaurus:{
vacationStart:2016-12-20,
vacationEnd:2016-12-25,
长度:12.5,
体重:5000
},
剑龙:{
vacationStart:2016-12-10,
vacationEnd:2016-12-20,
length:9,
weight:2500
}
}
vacationEnd 的恐龙在 2016-12之后 如果 vacationStart 之前或 2016-12-20
$ b

function getHolidayingDinosaurs(day){
return firebase.database( )
.ref(dinousaurs)
.orderByChild(vacationEnd)
.startAt(day)
.once(value)
.then (快照)=> {
让holidaying = [];
snapshot.forEach((child)=> {
let val = child.val();
if(val.vacationStart< = day){
holidaying.push (val);
}
});
返回假期;
}); $(b)b

getHolidayingDinosaurs(2016-12-20)。then((holidaying)=> console.log(holidaying));

在客户端执行进一步的过滤并不是直接的选择,因为您只能查询Firebase使用一个单一的财产和合并日期并不特别有用,因为开始和结束可能是查询日期之前或之后的任何日期。



无论如何,查询使用 vacationEnd 可能会执行服务器上的大部分过滤 - 除非您有很多恐龙提前计划他们的假期。



如果上述方法导致客户端检索和过滤的信息过多,则可以加入一些额外的工作,并通过存储一些额外的数据来维护您自己的度假恐龙的映射:

 节假日:{
...
2016-12-19:{
stegosaurus:true
},
2016-12-20:{
lambeosaurus:true,
stegosaurus:true
},
2016-12-21 :{
lambeosaurus:true
},
...
}

Firebase的多位置更新可用于使维护映射更容易一些(在中有更多的多位置示例()(){6680611> this answer ):

  firebase.database 
dinosaurs / lambeosaurus:{
vacationStart:2016-12-20,
vacationEnd:2016-12-25,
length :12.5,
体重:5000
},
假期/ 2016-12-20 / lambeosaurus:true,
假期/ 2016-12-21 / lambeosaurus :true,
holidays / 2016-12-22 / lambeosaurus:true,
holidays / 2016-12-23 / lambeosaurus:true,
holidays / 2016-12 -24 / lambeosaurus:true,
holidays / 2016-12-25 / lambeosaurus:true
});

关于 holidays / 2016-12-20

My firebase data looks like this:

{
  "lambeosaurus": {
    "vacationDates" : "2016-12-20 - 2016-12-25",
    "length" : 12.5,
    "weight": 5000
  },
  "stegosaurus": {
    "vacationDates" : "2016-12-10 - 2016-12-20",
    "length" : 9,
    "weight" : 2500
  }
}

How do i query for all dinosaurs that will be away on vacation on 2016-12-20 (i.e it should return both lambeosaurus and stegosaurus)? Or should I actually store the data differently? If so, how should I store the data for optimum performance? Thanks.

解决方案

Combining the two dates doesn't do much in terms of making the database easier to query.

If you are searching for dinosaurs on holiday on a particular date, they could have gone on holiday anytime before that date. (Unless there is a policy that mandates a maximum number of days in a holiday.) So the end date is probably what you want to query:

{
  "lambeosaurus": {
    "vacationStart" : "2016-12-20",
    "vacationEnd" : "2016-12-25",
    "length" : 12.5,
    "weight": 5000
  },
  "stegosaurus": {
    "vacationStart" : "2016-12-10",
    "vacationEnd" : "2016-12-20",
    "length" : 9,
    "weight" : 2500
  }
}

Any dinosaurs with a vacationEnd on or after 2016-12-20 will be on holiday on that date if vacationStart is on or before 2016-12-20:

function getHolidayingDinosaurs(day) {
  return firebase.database()
    .ref("dinousaurs")
    .orderByChild("vacationEnd")
    .startAt(day)
    .once("value")
    .then((snapshot) => {
      let holidaying = [];
      snapshot.forEach((child) => {
        let val = child.val();
        if (val.vacationStart <= day) {
          holidaying.push(val);
        }
      });
      return holidaying;
    });
}

getHolidayingDinosaurs("2016-12-20").then((holidaying) => console.log(holidaying));

There isn't a straight-forward alternative to performing further filtering on the client, as you can only query Firebase using a single property and the combined dates aren't particularly useful as the start and end could be any dates on or before/after the query date.

Anyway, querying using vacationEnd is likely to perform most of the filtering on the server - unless you have a lot of dinosaurs that plan their holidays well in advance.

If the above approach results in too much information being retrieved and filtered on the client, you could put in some extra effort and could maintain your own mapping of holidaying dinosaurs by storing some additional data structured like this:

"holidays": {
  ...
  "2016-12-19": {
    "stegosaurus": true
  },
  "2016-12-20": {
    "lambeosaurus": true,
    "stegosaurus": true
  },
  "2016-12-21": {
    "lambeosaurus": true
  },
  ...
}

Firebase's multi-location updates can be used to make maintaining the mapping a little easier (there are more multi-location examples in this answer):

firebase.database().ref().update.({
  "dinosaurs/lambeosaurus": {
    "vacationStart" : "2016-12-20",
    "vacationEnd" : "2016-12-25",
    "length" : 12.5,
    "weight": 5000
  },
  "holidays/2016-12-20/lambeosaurus": true,
  "holidays/2016-12-21/lambeosaurus": true,
  "holidays/2016-12-22/lambeosaurus": true,
  "holidays/2016-12-23/lambeosaurus": true,
  "holidays/2016-12-24/lambeosaurus": true,
  "holidays/2016-12-25/lambeosaurus": true
});

A query on holidays/2016-12-20 would then return an object with keys for each holidaying dinosaur.

这篇关于Firebase查询日期范围内的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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