如何删除Firebase节点中除最新的X个子项外的所有项目? [英] How to delete all but most recent X children in a Firebase node?
问题描述
行
,其中包含唯一ID子元素(来自 push()
操作),例如这: Firebase--
--lines
--K3qx02jslkdjNskjwLDK
--K3qx23jakjdz9Nlskjja
--K3qxRdXhUFmEJdifOdaj
--etc ...
我想成为能够删除行
除最近添加的200(或100,或其他)之外的所有子元素。基本上这是一个清理操作。现在我知道我可以通过抓住客户端的行
所有子项的快照,计算条目,然后使用 endsAt(totalChildren -numToKeep)
来获取相关数据并运行 remove()
。但是我想避免把所有这些数据都抓到客户端。
有没有其他的方法可以解决上面的问题?
用例是棘手的,是您正在计算物品,Firebase确实(有意)没有任何基于计数的操作。因此,您需要检索前N个项目,以知道哪个项目是N + 1。
ref.child (snapshot){
if(snapshot.numChildren()> MAX_COUNT){
var childCount = 0;
var updates = { ;
snapshot.forEach(function child(child){
if(++ childCount< snapshot.numChildren() - MAX_COUNT){
updates [child.key()] = null;
}
});
ref.child('lines').update(updates);
}
});
这里需要注意几点:
update()
优化此方法的一种方法(除了选择不同的/基于时间的截断策略)之外,还有一个单独的 line bids。
lineids
--K3qx02jslkdjNskjwLDK
--K3qx23jakjdz9Nlskjja
--K3qxRdXhUFmEJdifOdaj
所以你仍然保持每行的数据在 这个最后一个片段稍微有点牵扯,但是不必下载所有行数据 有许多变化可以选择,但是我希望这可以成为启动的足够灵感。 Given a Firebase node I want to be able to delete all children of Is there an alternative to my idea above? Keep the most recent N items, is one of the trickier use-cases to implement. If you have any option to change it into "keep items from the past N hours", I recommend going that route. The reason the use-case is tricky, is that you're counting items and Firebase does (intentionally) not have any count-based operations. Because of this, you will need to retrieve the first N items to know which item is N+1. A few things to note here: One way to optimize this (aside from picking a different/time-based truncating strategy) is to keep a separate list of the "line ids". So you'll still keep the data for each line in This last snippet is slightly more involved, but prevents from having to download all lines data by just downloading the line ids. There are many variations you can choose, but I hope this serves as enough inspiration to get started. 这篇关于如何删除Firebase节点中除最新的X个子项外的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! lines
,但也保留一个只是id的列表。然后删除多余的代码然后变成:
$ b $ $ $ $ $ $ $ $ $ $ $ ref.child('lineids')。once('value',函数(快照){
if(snapshot.numChildren()> MAX_COUNT){
var childCount = 0;
var updates = {};
snapshot.forEach(function子元素){
if(++ childCount< snapshot.numChildren() - MAX_COUNT){
updates ['lineids /'+ child.key()] = null;
updates [ /'+ child.key()] = null;
}
});
ref.update(updates);
}
});
lines
filled with unique-ID children (from push()
operations), such as this:Firebase--
--lines
--K3qx02jslkdjNskjwLDK
--K3qx23jakjdz9Nlskjja
--K3qxRdXhUFmEJdifOdaj
--etc...
lines
except the most recently added 200 (or 100, or whatever). Basically this is a cleanup operation. Now I know I could do this by grabbing a snapshot of all children of lines
on the client side, counting the entries, then using an endsAt(totalChildren-numToKeep)
to grab the relevant data and run remove()
. But I want to avoid grabbing all that data to the client.ref.child('lines').once('value', function(snapshot) {
if (snapshot.numChildren() > MAX_COUNT) {
var childCount = 0;
var updates = {};
snapshot.forEach(function (child) {
if (++childCount < snapshot.numChildren() - MAX_COUNT) {
updates[child.key()] = null;
}
});
ref.child('lines').update(updates);
}
});
update()
call to remove the extraneous lineslineids
--K3qx02jslkdjNskjwLDK
--K3qx23jakjdz9Nlskjja
--K3qxRdXhUFmEJdifOdaj
lines
, but also keep a list of just the ids. The code to then delete the extra ones then becomes:ref.child('lineids').once('value', function(snapshot) {
if (snapshot.numChildren() > MAX_COUNT) {
var childCount = 0;
var updates = {};
snapshot.forEach(function (child) {
if (++childCount < snapshot.numChildren() - MAX_COUNT) {
updates['lineids/'+child.key()] = null;
updates['lines/'+child.key()] = null;
}
});
ref.update(updates);
}
});