Reputation: 2132
My Firebase database data looks like this:
Version 9 Modular JavaScript SDK's
So for example if we take that current time is 1637149389
I want to delete all the data according to the picture below:
I tried using this code but the result is that my whole path to the data is deleted (including data not older then 5 seconds):
var timestamp = Math.round(Date.now() / 1000) - 5; //get Timestamp in Mili, convert to Seconds, and then set it 5 seconds to the past
var UID = 'uid1';
const ref = ref(
database,
'device_realtime/' + UID
);
var refQuery = query(
ref,
...[fb.orderByKey(), fb.endAt(timestamp.toString())]
);
remove(refQuery);
get(refQuery)
on the same
query my results are correct.Console:
Upvotes: 0
Views: 126
Reputation: 599581
To remove a node, you need to know the complete path to that node. Firebase doesn't support update/remove queries, where you pass the root reference and a condition to the database.
So you'll need to call remove()
on each individual child node that you want to remove.
Something like:
get(refQuery, (results) => {
results.forEach((snapshot) => {
remove(snapshot.ref);
})
})
Upvotes: 1