如何删除Firebase节点中除最新的X个子项外的所有项目? [英] How to delete all but most recent X children in a Firebase node?

查看:166
本文介绍了如何删除Firebase节点中除最新的X个子项外的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个Firebase节点,其中包含唯一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

所以你仍然保持每行的数据在 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);
}
});

这个最后一个片段稍微有点牵扯,但是不必下载所有行数据

有许多变化可以选择,但是我希望这可以成为启动的足够灵感。


Given a Firebase node lines filled with unique-ID children (from push() operations), such as this:

Firebase--
  --lines
    --K3qx02jslkdjNskjwLDK
    --K3qx23jakjdz9Nlskjja
    --K3qxRdXhUFmEJdifOdaj
    --etc...

I want to be able to delete all children of 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.

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.

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);
  }
});

A few things to note here:

  • this will download all lines
  • it performs a single update() call to remove the extraneous lines

One way to optimize this (aside from picking a different/time-based truncating strategy) is to keep a separate list of the "line ids".

lineids
  --K3qx02jslkdjNskjwLDK
  --K3qx23jakjdz9Nlskjja
  --K3qxRdXhUFmEJdifOdaj

So you'll still keep the data for each line in 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);
  }
});

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屋!

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