I am looking to delete a node after a certain time, let's say that I want to place the node an expiration date of 5 days and in those 5 days delete the node. How can I do it?
My data structure is like this:
Posts:
PushedKey:value
How can I delete this pushedKey after certain time?
I recommend using Cloud Functions for Firebase. You can use an external service for cron jobs, using it to trigger an HTTP function at a certain time interval. Granted, this would be the same interval for all of the nodes instead of a timer counting down five days for each individual node. But, for example, you could trigger the function once a day and remove all nodes that were written to the database 5 days ago.
Here's a similar example applied to removing unused accounts
Here's the documentation on Cloud Functions
Related
I am creating an app in which users earn points. Here is my data structure:
How can I update all points of different nodes by 5 every second and repeat this process?
You can use the cloud script function of Firebase as implementing this logic on client side will drain the battery as you need to update the value after every one second.
On Cloud script you can use crown job to implement this logic and it will not put any load on the application too.
I'm doing a query to fetch only 1 month old documents.
I store the creation time of the document itself
timestamp : 9 Apr, 2020 10:03:43 AM
Now, in my query , I want to get all the documents whithin the current month but I dont want to use currentDate from my client (so it cannot be changed) but also I dont want to query that document to find the timestamps of that document
Query
FirebaseFirestore.getInstance().collection("orders").whereEqualTo("shopId",shopId)
.whereEqualTo("status", 7).startAt().endAt().get().await()
I want to know an efficient server aproximation to set at .startAt().endAt() to query just the documents with status 7 in which has past 1 month
any ideas?
Firestore does not offer timeboxed queries that restrict a range of documents based on the sense of server time. You will have to trust that the client is sending the correct time.
The only control you have over time is using request.time in security rules. You could write a rule to allow queries that only fall within times based on the server timestamp, but it's still up to the client to specify the time correctly in the query. The rules will not be able to filter results based on time.
You might want to read more about how server timestamps work with security rules at the end of this article.
I have data structure like this:
Employees (Collection) > {EmployeeID} (Documents) > Chat (Collection) > {ChatId} (Documents).
In chat collection each document having 3 fields. 1. senderName, 2. sendTimestamp, 3. messageText.
I want to delete chats which are older than 7 days (from today).
I think it might be possible through cloud function but I am really basic user and don't know much about cloud functions. Please note that I don't want to make it automatically (cron job). I will do it manually on daily basis or whenever I wish.
I really searched a lot for this but its really hard. Please help me.
A big part of this task involves querying a sub collection. You can read more about this idea here: Firestore query subcollections
There are basically two options at the time of writing this:
Query the entire top level collection (Employees) something like db.collection('Employees').get(). Then you would have to loop through each employ object querying for their sub collection (Chat) based on their date range. Firestore query by date range for more reading on querying by a date in firestore. This could result in a large amount of reads depending on the number of Employee documents, but is the "easiest" approach in terms of not having to make changes to your data models/application.
Restructure your data to make the sub collection Chat a top level collection. Then you can do a query on this top level collection by the date. Less reads, but may not be as feasible depending on if this app is in production/willingness to make code changes.
A Function would definitely be able to accomplish this task either way you decide to approach it. One thing to note is that a Function executes using the Admin SDK, meaning it can basically ignore security rules set up on your Firestore.
This question already has answers here:
Delete firebase data older than 2 hours
(5 answers)
Closed 4 years ago.
I'm making a meeting app where user can create a group of people who are part of that meeting , also date and time of the meeting has to be set when group is created and store in the firebase database.
When the date and time of meeting is passed
Firebase should automatically delete child nodes of that group.
I'm thinking of cloud function , but don't know how to make this work.
On top of my head, you can do three things:
1) Cloud Functions: You correctly pointed it out that a cloud function is the correct way. However, a cloud function will need a trigger as far as I understand. You can run this trigger on a daily basis for instance to clear all meetings after due date. Potential way to run this trigger can be a cron job from a website like https://www.easycron.com/
2) Event Listener on a client: If you have a listener fixed on a client then you can put a check each time its run. So any client running their own event will clear it based on a check. It will not clear automatically after time passes but will clear once a relevant client listens for it.
3) Ignore meetings after due dates on a client. That way you save the old data as well. Might be costly in terms of bandwidth in the long term.
Also, if you have just started, I'd personally advice to use Firebase Firestore instead. I found it much better to use.
I am using firebase realtime database in my android app.I have details of every person along with a field feepaidforcurrentmonth boolean.I have 2 fragments feespaid and feenotpaid. When fee is paid for current month feepaidforcurrentmonth becomes true and gets updated in firebase database and the member is added to the list of feespaid fragment from feenotpaid.
My question is how do I schedule task in firebase/or in my app to set the value of field feepaidforcurrentmonth false every first day of month?How do I update it in firebase database?
This sounds like a task you should do in the backend rather than the frontend (your app).
You could use an external self written and hosted backend, e.g. python that's triggered by a cron tab.
Here's a blog post from firebase about the topic. They are using Google App Engine to trigger based on time / date.