I want to clean up this userPublic
by deleting all of its child node which has isTesting == true
. I am using Firebase's cloud function. My approach would be :
const userPublic = admin.database().ref("/userPublic")
const testsInUserPublic = userPublic.orderByChild("isTesting").equalTo(true)
testsInUserPublic.once("value", dataSnapshot => {
// ???
})
Since I can only call
.remove()
on reference and not snapshot but to filter the child I want it returns snapshot, how can I get the reference from snapshot? (I would like to know the key XXX-XXX-XXX of each filtered child, so I can concatenate withuserPublic
and.remove()
them one by one)Also, even if I can get all the references that I want to remove I think deleting them one by one by calling
.remove()
then wait for promise, then call the next one does not sounds like an optimal way. Are there any way to remove all of them in one go?If it involves calling
.update()
on the topuserPublic
node, I would have to fetch everything, remove the one withisTesting
and then put the remaining back for update. This sounds like it is not efficient compared to the filtering way. As eventually the one with.isTesting
is only about 5% of all data. Or is this actually the approach everyone is using?