how to fetch documents from one month old only fir

2020-05-03 10:49发布

问题:

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?

回答1:

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.