Reputation: 87
Can I know if there is any way I could import my JSON file into Firebase without overwriting the existing data?
Upvotes: 5
Views: 4118
Reputation: 598765
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.
Upvotes: 4
Reputation: 482
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.
Upvotes: 0