Reputation: 181
I'm looking at synching a node e.g. /a/b/c with node /data/c
There is data at node /a/b/c - so I just want to grab a snapshot of /data/c and put that data in to /a/b/c without deleting existing values.
Would an approach like :
Ref.update({
snapshotdata.val()
}).key;
Would this work? I'm not sure how firebase would parse the snapshot, if it takes the whole thing and overwrites or it would take individual values and adds.
Thanks in advance for any clarity.
Best Regards, Kieran
Upvotes: 0
Views: 473
Reputation: 26313
It depends. Snapshot values are simply JSON objects (or primitives, depending on the node). update()
will replace any nodes specified by the keys in the update, but not any that aren't. So if /a/b/c
looks like:
{
name: 'old name',
data: {
gonnabe: 'gone'
},
created: 12345679
}
and data/c
looks like:
{
name: 'new name',
data: {
nested: true
},
updated: 12345679
}
doing an db.ref('a/b/c').update(dataSnapshot.val())
will result in:
{
name: 'new name',
data: {
nested: true
},
created: 12345679,
updated: 12345679
}
Note that the nested field in data
was wiped out by the update, but the non-nested field created
was not.
If you want to do deep updates, you'll need to construct an update with slash-delimited fields for each deep node you want to update. So instead of the above, you might have:
{
"name": "new name",
"data/nested": true,
"updated" 12345679
}
This will be non-destructive to the nested data and only update values that are directly referenced, resulting in:
{
name: 'new name',
data: {
gonnabe: 'gone',
nested: true
},
created: 12345679,
updated: 12345679
}
Hope that helps!
Upvotes: 3