'Firebase Realtime Database: Most Efficient way to delete an entry by its value? [Admin SDK]
I have an entry in real time database which looks something like this:
{
<RANDOM_FIREBASE_GENERATED_KEY>: value1,
<RANDOM_FIREBASE_GENERATED_KEY>: value2,
<RANDOM_FIREBASE_GENERATED_KEY>: value3,
}
I am creating these data points using push. Now I also want the flexibility to delete these datapoints using the value. [I can assume that these values are going to be unique]
Here's how I was planning to do it:
const values = await Server.rdb.ref('path').get();
const newValues = Object.keys(values).filter(element => element !== 'to_be_filtered_entity');
await Server.rdb.ref('path').set(newValues);
But this would mean that I need to perform the filtering load at my backend server which is not ideal. Can someone tell me an optimized approach to perform this operation?
Solution 1:[1]
As per one of the comments, I added
"connected-users": {
"$uid": {
".indexOn": ".value",
".read":"$uid === auth.uid && auth.token.email_verified === true",
".write":"$uid === auth.uid && auth.token.email_verified === true"
}
}
for the rules and then used:
await Server.rdb
.ref(`${rdb_paths.funfuse_connected_users}/${loc}`)
.orderByValue()
.equalTo(<value>)
and this solved my issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shivam Sahil |
