Import JSON file without overwriting the existing data in Firebase

烂漫一生 提交于 2020-01-13 14:58:20

问题


Can I know if there is any way I could import my JSON file into Firebase without overwriting the existing data?


回答1:


You can Import JSON at any location in the Firebase Database console. So not just at the root, but also at a specific path under it, e.g. /users, /users/charlinagnes, etc.

When you import JSON at a location, Firebase performs a setValue() operation at that location. So it overwrites the existing data at that location with the new JSON you provide.

There is no UI for performing any kind of merge operation. But luckily Firebase has an extensive API (it's a developer product after all) that allows you to write your own merge logic. Using the update() method will likely be instrumental to such merging.




回答2:


Comparing every data from your server to firebase is a little difficult. But you can put all your data in firebase without using its data. The solution is use push() to create a unique id to every data group or data so that your newly imported data will not hamper the previous data. There are two ways to invoke push in Firebase's JavaScript SDK.

Using push(newObject). This will generate a new push id and write the data at the location with that id. using push(). This will generate a new push id and return a reference to the location with that id. This is a pure client-side operation. Knowing #2, you can easily get a new push id client-side with:

var newKey = ref.push().key(); You can then use this key in your multi-location update.



来源:https://stackoverflow.com/questions/41929337/import-json-file-without-overwriting-the-existing-data-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!