如何在不下载和上传数据的情况下复制具有多条记录的Firebase子项? [英] How to duplicate a Firebase child with many records without downloading and uploading the data?

查看:92
本文介绍了如何在不下载和上传数据的情况下复制具有多条记录的Firebase子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有近50万条记录的Firebase子项目。我想复制这个孩子,以创建数据的备份(在相同的Firebase中)。所以,如果我的孩子被称为 lines ,我想复制它,但使用一个新名称,如 lines_backup_02_0216 。一旦完成,我想原来的行保留原样。

一个 dataSnapshot 通过Javascript(下载数据),然后使用 set()来创建副本?例如:

  fb.child(lines)。once('value',function(snapshot){
(fb.child)(lines_backup_02_02_2016).set(snapshot.val());
});

对于500k条记录,我发现这比我等到目前为止所花的时间要长例如,我在输入这个问题的同时还在等待它完成)。

解决方案

尝试访问/查询节点500k的孩子记录是一个坏主意。也不太可能需要将这些500k行同步到用户。所以请努力将历史资料与历史资料分开。您目前创建备份副本的方法是一个很好的开始。当你将它们写到新的位置时,也可以删除节点。



为了获得这些数据将会有点牵扯 。它确实涉及到首先获得一个浅REST调用的子键列表:

  https://yours.firebaseio.com /path-to-list.json?shallow=true 

然后循环访问每个键小孩,然后用一个普通的JavaScript客户端:

  ref.child('path-to-list')。child(key ).once('value',function(snapshot){
ref.child('path-to-backup')。child(key).set(snapshot.val(),function(error){
if(!error){
//删除原来的项目,这样列表保持不变
snapshot.ref()。remove();
}
}) ;

});

这个循环不会产生问题,因为您不查询整个列表。相反,您直接访问特定的子节点,这是很好的。



一定要扼杀循环,因为否则你可能会用小的写入淹没你的数据库。


I have a Firebase child with nearly 500k records. I want to duplicate this child in order to create a backup of the data (within the same Firebase). So, if my child is called lines, I want to duplicate it but with a new name such as lines_backup_02_02_2016. Once this is done I want the original lines left as is.

Is there some way to do this without grabbing a dataSnapshot via Javascript (downloading the data) and then using set() to create the copy? For example:

fb.child("lines").once('value', function(snapshot) {
    fb.child("lines_backup_02_02_2016").set(snapshot.val());
  });

With 500k records I'm finding this takes longer than I've been able to wait so far (for example, I've typed this question while still waiting for it to finish).

解决方案

Trying to access/query a node with 500k child records is a bad idea. It's also not very likely that you need to synchronize those 500k lines to users. So please work hard on separating the active from the historical data. You current approach of creating a backup copy is a great start for that. Just also delete nodes while you're writing them to their new location.

To get this data is going to be "a bit involved". It indeed involves first getting a list of the child keys with a shallow REST call:

https://yours.firebaseio.com/path-to-list.json?shallow=true

Then you loop through the keys and access each child in turn, with a regular JavaScript client:

ref.child('path-to-list').child(key).once('value', function(snapshot) {
  ref.child('path-to-backup').child(key).set(snapshot.val(), function(error) {
    if (!error) {
      // Delete the original item, so that the list stays small
      snapshot.ref().remove();
    }
  });

});

This loop doesn't create problems, since you're not querying the entire list. Instead you are accessing specific child nodes directly, which is fine.

Be sure to throttle the loop, because you might otherwise swamp you database with small writes.

这篇关于如何在不下载和上传数据的情况下复制具有多条记录的Firebase子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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