This might be relevant to what you’re inquiring about. Though, it is an old bit of code and a little messy. The gist of it is, I have a firebase database that is split into two main groups.
- Images
- Keywords that can be added to images
this bit of code has keywordToRemove (deleting a Keyword from the app), and imagesAttached (an array of firebase keys that belong to each image that the Keyword was attached to). Every time I add a Keyword to an image, I add that images firebase key to the Keywords ‘image keys’ array with a value of true. This function evaluates the Keyword, and deletes said Keyword from each image it was previously attached to.
Christ that makes no sense reading it, but, it works. Maybe you can glean something from the sample code.
the ‘Galleries’ ref is a reference to specific galleries of images according to users and dates.
the ‘Keywords’ ref is a reference to app-wide keywords that can be attached to any image.
‘this.masterKeys.userKey’ is the specific user’s id in fireabse database.
‘dateKey’ (your first snapshot resulting in snapshot.key) is each gallery of images (held in place by a date)
From Component
deleteKeyword(keyword, i){
let keywordKey = keyword[0] //this equates to the keyword's firebase key
let imagesAttached = Object.keys(keyword[1]) //this equates to the array of firebase keys belonging to images the keyword is assigned to
let len = imagesAttached.length - 1; //ignore this.
imagesAttached.splice(len, 1); //ignore this
this.keyService.deleteKeyword(keywordKey, imagesAttached)
this.keywords = this.keyService.returnMainKeywords();
}
In Provider (keyService)
deleteKeyword( keywordToRemove, imagesAttached ) {
//keywordToRemove is actually the Keyword's firebase key.
let specificImage = "";
let UserGallery = firebase.database().ref('Galleries').child(this.masterKeys.userKey);
for (let i = 0; i < imagesAttached.length; i++) {
specificImage = imagesAttached[i];
UserGallery.on("child_added", (snapshot) => {
let dateKey = snapshot.key;
UserGallery.child(dateKey).child(specificImage).child(keywordToRemove).remove();
})
}
firebase.database().ref('Keywords').child(this.masterKeys.userKey).child(keywordToRemove).remove()
}
Again, this is dated, I did a poor job of naming, and it honestly makes little sense to me at this moment, but it works, and is fairly quick. It’s from my initial attempts at doing anything semi-complicated with firebase, the format needs to be flattened, etc., but if an idea comes from it, very well.
Further, It is not writing to, but deleting. I believe the concept will carry over, if what I provided has any relevance to your goal.