I have a live database with the following structure:
"keys" : "randomUserId1" : "randomKey1" : timestamp1 "randomUserId2" : "randomKey2" : timestamp2 "randomKey3" : timestamp3 "randomUserId3" : "randomKey4" : timestamp4
The timestamp value is the creation time in milliseconds. I now want to use Firebase functions and javascript to delete all keys with timestamps that are too old. Timing is not that important, so the delete function may trigger on a suitable write elsewhere in the database.
I tried modifying the example method to delete old child nodes but can't understand how to make it work with the above database structure.
How to write js function to complete the above task?
I could of course add a key/value pair ("timestamp": timestamp) below "randomKey" if that would make things easier.
Firebase function (and using
firebase-sdk
) removes keys with timestamps older than the threshold:The link you provided is based on my deletion earlier than 2 hours
To make it work on your data structure, you can use
orderByValue()
instead oforderByChild("timestamp")
. so:To learn more, see the Firebase documentation on Sorting and Filtering Data.