0

Data Structure I am inserting data in Firebase Realtime Database in a table with the above structure. The key of the data is auto-generated based on push. After several such entries are created, sometime due to certain conditions I may need to delete one of the entries. At the point of deleting the entry, I may know some of the values of the node that I want to delete like createdAt and createdForPostID. But I will not know the key as it was auto-generated using push feature of firebase database. A combination of createdAt and createdForPostID makes a unique combination and only one such entry should exist in the database.

What would be the most efficient way to identify the entry without having to retrieve the entire node at OUTBOUND?

The reason I am using push is because Firebase claims it to be efficient and not subject to write conflicts. I also rely on the auto-sorting by date/time offered by push.

If no efficient way can be found, then I will generate my own key using date/time stamp. But I am hoping that this is a problem that someone has solved before and hence can guide me.

Any suggestions are welcome.

Abhishek
  • 1,261
  • 1
  • 13
  • 30

2 Answers2

2

You'll need to run a query to find the items that match your conditions.

Since you seem to have multiple properties in your conditions, and the Firebase Database can only query on a single property, you'll need to combine the values into a single property as shown here.

Then you can run a query on that combined property and delete the items it returns:

var query = ref.orderByChild("createForPostID-createdAt").equalTo("20171229_124904-20171230_200343");
query.once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    child.ref.remove();
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Given Frank's answer I realised, I needed to create a unique property as per his suggestion because I will need it to do the future query. But then it seemed that I may be better off using that unique property as the key instead of using push

So it seems from an overall perspective, it might be more efficient to create your own key instead of push, if the app needs both create and delete functions. Reliance on push makes sense only if data is being created and deletion is not a big functionality of your app.

So, in conclusion, for Firebase data, the most efficient way to do both data create and delete needs creation of a unique key on your own.

Abhishek
  • 1,261
  • 1
  • 13
  • 30