Firebase RTDB:使用CLI中的旧数据更新/合并新数据 [英] Firebase RTDB: updating/merging new with old data from CLI
问题描述
我有多个(大)JSON文件,我想添加到Firebase实时数据库(RTBD)中.我使用的是Geofire,因此所有子节点都必须位于同一父节点下.我正在存储静态地理数据,但我想定期对其进行分段刷新.如果我无法分段刷新,总是保证我要更新的一个大JSON总是100%完成就变得有问题.
I have multiple (big) JSON files that I want to add to Firebase Real-time Database (RTBD). I'm using Geofire, so all the children nodes need to be under the same parent. I'm storing static geo-data but I want to periodically refresh it in pieces. If I can't refresh it in pieces, it becomes problematic to always guarantee that the one big JSON I am updating with is always 100% complete.
我想要的数据结构是这样的:
My desired data structure is like this:
{"parent" : {"child_a" : 1, "child_b" : 2}
我的第一个JSON如下所示
Where my first JSON looks like this
$ cat 1.json
{"parent": {"child_a" : 1 }}
和我的第二个这样的
$ cat 2.json
{"parent": {"child_b" : 2 }}
我尝试将firebase-import
与--merge
标志一起使用:
I've tried using firebase-import
with the --merge
flag:
$ firebase-import --database_url https://my_db.firebaseio.com/ --path / --json 2.json --service_account auth.json --merge
,还尝试将firebase-tools
与更新标志一起使用:
And also tried using firebase-tools
with the update flag:
$ firebase database:update / 2.json
但是两者都从数据库中删除了1.json的内容,并给了我
But both are removing the contents of 1.json from the DB, and giving me this
{"parent": {"child_b" : 2 }}
如何在不覆盖以前的JSON内容的情况下将多个JSON文件合并到同一节点?
部分答案:将set
与merge
结合使用,如此处所示,用于Fire store >.但是如何使用CLI和Fire base 做到这一点?
partial answer: Use set
with merge
, shown here for Firestore. But how can I do that with the CLI and Firebase?
推荐答案
基于 Doug 的评论,
通过从JSON中删除顶级父节点,然后将其添加到firebase-import
命令中,我可以分多个步骤将单个密钥添加到parent
节点,而不会覆盖现有子节点.
I can add individual keys to the parent
node in multiple steps, without overwriting the existing children, by removing the top-level parent node from my JSON, and then adding it to the firebase-import
command.
因此,从问题示例中来看,JSON文件中没有parent
节点,而import语句的--path
标志中也没有parent
节点
So from the example in the question, with no parent
node in the JSON files and also the parent
node in the --path
flag of the import statement
$ cat 1.json
{"child_a" : 1 }
$ firebase-import --database_url https://my_db.firebaseio.com/ --path /parent --json 1.json --service_account auth.json --merge
和
$ cat 2.json
{"child_b" : 2 }
$ firebase-import --database_url https://my_db.firebaseio.com/ --path /parent --json 2.json --service_account auth.json --merge
以我需要的方式为我提供数据:
gives me the data the way I need:
{"parent" : {"child_a" : 1, "child_b" : 2}
这篇关于Firebase RTDB:使用CLI中的旧数据更新/合并新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!