按日期过滤 Firebase 节点

Filter Firebase node by date

您好,有一个 Firebase 节点,每个子节点都设置了日期。在我的查询中,我想删除过去发生的所有事件。

Firebase

workouts
    -{key}
        - title:"My Workout"
        - date: "2017-04-1T10:30:27.250Z"
    -{key2}
        - title:"The Workout"
        - date: "2017-05-1T12:00:27.250Z"

JS(Ionic2、angularfire2)

this.allEvents = this.af.database.list('/events', {
  query: {
    orderByChild: 'date'
  }
});
  1. 遍历数组并将当前日期与过去日期进行比较。
  2. 如果它是过去发生的事件,请将其删除。来自 angularfire documentation:

$remove(recordOrIndex)

Remove a record from the database and from the local array. This method returns a promise that resolves after the record is deleted at the server. It will contain a Firebase reference to the deleted record. It accepts either an array index or a reference to an item that exists in the array.

var list = $firebaseArray(ref);
var item = list[2];
list.$remove(item).then(function(ref) {
  ref.key === item.$id; // true
});

您想结束.endAt(endTime) 来限制查询

来自 Firebase 文档。 https://firebase.google.com/docs/database/admin/retrieve-data#range-queries

 var ref = db.ref("workouts");
  ref.orderByChild("date").endAt(endTime).on("child_added", function(snapshot) {
    console.log(snapshot.key);
  });

然后你将遍历列表并删除一个一个