删除2小时以前的Firebase数据 [英] Delete firebase data older than 2 hours
问题描述
我想删除任何两个小时以前的数据。目前,在客户端,我遍历所有的数据,并在任何更旧的版本上运行删除。当我这样做时,db.on('value')函数每次被删除时都会被调用。而且,只有在客户端连接时才会删除事件,如果两个客户端连接在一起,会发生什么情况?
我可以在哪里设置删除旧数据的东西?我在JavaScript Date.now()创建的每个对象中都有一个时间戳记。 Firebase不支持使用动态参数进行查询,例如两小时前。然而, 可以针对特定值执行查询,例如2015年8月14日上午7:27:32之后。
意味着您可以定期运行代码片段来清理当前时间超过2小时的项目 :
var ref = firebase.database()。ref('/ path / to / items /');
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var old = ref.orderByChild('timestamp')。endAt(cutoff).limitToLast(1);
var listener = old.on('child_added',function(snapshot){
snapshot.ref.remove();
});
正如您将会注意到的那样,我使用 child_added
而不是 value
和I limitToLast(1)
。当我删除每个孩子时,Firebase将为新的最后项目触发一个 child_added
,直到截止点之后没有更多项目。
更新:如果您想在Firebase的Cloud Functions中运行此代码:
exports.deleteOldItems = functions.database.ref('/ path / to / items / {pushId}')
.onWrite(event => {
var ref = event.data.ref ();
var cutoff = now - 2 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild() 'timestamp')。endAt(cutoff);
return oldItemsQuery.once('value',function(snapshot){
//创建一个包含所有需要删除的子元素的映射
var更新= {};
snapshot.forEach(function(child){
updates [child.key] = null
});
//一次执行所有更新返回结果来结束函数
return ref.update(updates);
});
});
只要数据写在 / path / to / items
,所以子节点只会在数据被修改时被删除。
I would like to delete any data that is older that two hours. Currently, on the client side, I loop through all the data, and run a delete on any that is older. When I do this, the db.on('value') function is invoked every time something is deleted. Also, things will only be deleted when a client connects, and what might happen if two clients connect at once?
Where can I set up something that deletes old data? I have a time stamp inside each object created by a JavaScript Date.now().
Firebase does not support queries with a dynamic parameter, such as "two hours ago". It can however execute a query for a specific value, such as "after August 14 2015, 7:27:32 AM".
That means that you can run a snippet of code periodically to clean up items that are older than 2 hours at that time:
var ref = firebase.database().ref('/path/to/items/');
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var old = ref.orderByChild('timestamp').endAt(cutoff).limitToLast(1);
var listener = old.on('child_added', function(snapshot) {
snapshot.ref.remove();
});
As you'll note I use child_added
instead of value
, and I limitToLast(1)
. As I delete each child, Firebase will fire a child_added
for the new "last" item until there are no more items after the cutoff point.
Update: if you want to run this code in Cloud Functions for Firebase:
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.onWrite(event => {
var ref = event.data.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value', function(snapshot) {
// create a map with all children that need to be removed
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});
This function triggers whenever data is written under /path/to/items
, so child nodes will only be deleted when data is being modified.
这篇关于删除2小时以前的Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!